views:

449

answers:

3

I am not understanding this question...can you tell me what to do in a short message...or give me some links to search....please, it's urgent

You are required to implement a C preprocessor. The Preprocessor is to be implemented as a command-line tool, the input to which is a C source file (.c extension) and the output is the preprocessed file (.i extension). The tool also takes several options.

$ cppr <options> file.c

On successful processing, file .i is produced.

<options> may be:

Preprocessor options-
      -Aassertion -C -dD -dM -dN -Dmacro[=defn] -E -H
      -idirafter dir -include file -imacros file 
      -iprefixfile -iwithprefix dir -M -MD -MM -MMD 
      -nostdinc –P -Umacro –undef
Directory options-
      -Bprefix -Idir -I-

Implement any two of the above. This has to be decided during requirements phase.

These are the options defined by the gcc compiler. Refer to the manpage of gcc to understand the options.

You must implement the following features at a minimum:

  1. Stripping off of comments
  2. #ifdef and #endif
  3. #define for constants (not macros)
+2  A: 

Your c textbook should describe what the standard c preprocessor does, but you can also try man cpp.

Then write a program to perform a limited subset of these tasks (i.e. process #ifdef / #endif pairs, and simple #defines).

Your program should parse its command line, accept at least two of the options listed above, and handle them in the way explained in the gcc manpage.

dmckee
+2  A: 

Here's the gcc documentation on preprocessor options, which might be of some help to you. It's fairly long but most of it deals with options that you don't need to bother with, so you can look through and pick out the relevant sections.

David Zaslavsky
+2  A: 

It is not easy to answer not knowing what exactly you don't understand, but I'll try anyway, using my very limited C experience.

What is a preprocessor? A preprocessor is a program that does some kind of processing on the code file before it is compiled. You can, for example, define a symbolic constant with a preprocessor directive:

#define PI 3.14159

Then you can use this value with a meaningful name across your code:

area = r * r * PI;
...
circumference = 2 * r * PI;

What the preprocessor does here is replace all occurrences of PI with the numeric value you specified:

area = r * r * 3.14159;
...
circumference = 2 * r * 3.14159;

You can also include code depending on whether or not a constant has already been defined somewhere else in your code (this is typically used in projects with multiple files):

#define WINDOWS

...

#ifdef WINDOWS
    /* do Windows-specific stuff here */
#endif

The lines between #ifdef and #endif will only be included if the constant WINDOWS is defined before.

I hope that by now you have some idea about what your program should do.

Tips on implementing the "minimum features"

Here I'm going to give you some ideas on how to write the minimum features your professor requires. These are just off the top of my head, so please think about them first.

Stripping off of comments

While reading the input, look for "/*". When you encounter it, stop writing to the output, then when you find "*/", you can start writing again. Use a boolean flag to indicate whether you are inside a comment (AFAIK, there is no bool type in C, so use an int with 0 or 1, or, more ideally, two symbolic constants like INSIDE_COMMENT and OUTSIDE_COMMENT).

#define for constants (not macros)

If you encounter any line beginning with #, obviously you should not write it out. If you find a #define directive, store the symbolic name and the value somewhere (both strings), and from then on, look for the name in the input, and write out the value instead each time it is found. You can set a maximum length for the constant name, this is I think 6 chars in C, and always check 6 characters from the input. If the 6 characters begin with a known constant name, write out the value instead.

#ifdef and #endif

Create a boolean flag to indicate whether you are inside an #ifdef, much like with comments. When finding #ifdef, check if you are already storing the constant name, and write to the output depending on that.

I hope this helps.

EDIT: also read the comment by gs!

Botond Balázs
Two things: there could be nested #ifdefs and watch out for /* which are inside of strings, they shouldn't be matched.
Georg
C99 has bool. The full syntax of comments is excruciating - backslash newline is deleted, for example. Can't show that in a comment, of course, and you don't normally write comments like that.
Jonathan Leffler