user12752171
user12752171

Reputation:

Reading input and printing specific output

I wish to write a program in C that reads input and only prints certain output (in particular, it omits comments).

Clarification: I would like it to behave exactly like a C processor. I believe C processors replace comments with spaces, so once I detect the end of a comment, I can just replace it with a space.

This means that this program should properly deal with trigraphs, escaped quotes, and other things that might affect comments.

The function ignores any comment characters in its input. For instance, except for input within strings, it ignores all input in the form of a C comment (e.g., input between /* and */ and between // and a newline (\n) character).

Note that in the second case, the newline character is printed whereas in the first case, neither /* nor */ are printed.

Within a string, however (e.g. "hello //world"), the program will print whatever comes after the comment specifiers (in this case, it would print "hello //world" even though "world" is preceded by a single-line C-comment specifier).

So far, I have tried to consider several cases. I have considered defining two variables sngl_line and multi_line to determine when input is between a // and a \n (i.e., sngl_line is true) or when input is between /* and */ (i.e., multi_line is true). I have written a program that ignores comments outside of strings, but I find dealing with trigraphs especially problematic. Perhaps I should deal with them later?

I would also like my program to produce errors (e.g., if there is a nonterminating double quote or comment, it prints an appropriate error message). Note: I have successfully written a program that ignores comments as expected (except maybe not when trigraphs are present in strings). So I do not wish to see any working C code; just a few general tips as to how to produce the right errors will suffice.

Upvotes: 1

Views: 194

Answers (1)

chux
chux

Reputation: 153338

Use a state machine

Process the input as if your are in one of the following states:

  1. Normal
  2. Inside ""
  3. Inside ''
  4. Inside a /* */ comment
  5. Inside a // end-of-line comment

From each state there are a few rules of how to transition to another state.

E.g. From // comment state, stay there until end of line, then go to normal.
From normal, if next characters are //, jump to // comment state.
From normal, if next characters are /*, jump to /* comment state.

It is really a most rewarding programming task so I won't list more.


It looks like OP's data does not exercise the '' state.

Upvotes: 1

Related Questions