Comments are treated as whitespace. Each comment is replaced by one space charac
ID: 3638726 • Letter: C
Question
Comments are treated as whitespace. Each comment is replaced by one space character, or by several newline characters, depending on how the comment was written. Note that the interior of character constants (like 'a') and string literals (like "a") do not contain comments, so this pass is more complex than the first three. You need to do some elements of tokenization here, but only to recognize the beginning and end of character constants, string literals, and the two forms of comments.
The string literal "/* xyz */" looks like it contains a comment, but it doesn't, so don't change the string. It goes to the output of this pass as-is.
The two forms of comment are
/* extending to the first */
// extending to the end of line
Note that this pass occurs after source line continuation, so the comment
// foo
bar
will already have been modified to
// foo bar
The /**/ form can extend across source code lines. It should be replaced with one space character if all on one line, or with one or more newline characters if on multiple lines (as a partial attempt to preserve the original number of lines).
Comments are not recognized within other comments. The first example (a nested comment) is illegal in Standard C and the second is one comment not two, The third example is not terminated (it might be terminated on a later line).
/* /* something */ */
// /* something */
/* // something
The structure of this pass is more complicated than the first three, so here it is in more detail, using the program schema presented earlier.
If c0 is " (double-quote), copy characters up to (and including) the next ". Note that if the character sequence " appears, just copy it; this does not end the string literal.
You don't need to do anything special for a wide-character string literal, which looks like L"...".
Strings that were continued across lines with backslash-newline should have been fixed in Pass 3.
Two adjacent strings, such as "abc" "def", will be concatenated in a later pass (not part of this project).
If c0 is ' (single-quote), copy characters up to (and including) the next '. Note that if the character sequence ' appears, just copy it; this does not end the character constant.
You don't need to do anything special for a wide-character constant, which looks like L'...'.
If c0 is / and c1 is /, read characters up to (and including) the next newline, and output only one space character and a newline.
If the comment is not terminated (you found // but not a following newline before end-of-file) then
fprintf(stderr, "warning: unterminated // comment, fixed, Pass 4 ");
and keep going (you should be at the end of the file at this point).
If c0 is / and c1 is *, read characters up to (and including) the next */ sequence, and output only one space character (if you had not seen a newline along the way) or n newlines (if you found n newlines before the close of the comment, for n > 1).
Note that the /* */ form is replaced by either one space character, or one or more newline characters, and each case is treated as whitespace in later compiler steps.
If the comment is not terminated (you found /* but not */ before end-of-file) then
fprintf(stderr, "warning: unterminated /* */ comment, fixed, Pass 4 ");
and keep going (you should be at the end of file at this point).
Remember, ' does not start or end a character constant, and " does not start or end a string literal.
int c0, c1;
c0 = getc(stdin); c1 = getc(stdin);
while (c0 != EOF) {
int shift = 1;
... make a decision using c0 and c1, change some variables, maybe call putc()
if (shift == 1)
{ c0 = c1; c1 = getc(stdin); }
else /* shift == 2 */
{ c0 = getc(stdin); c1 = getc(stdin); }
}