Page 34

Write a program to remove all comments from a C program. Don't forget to handle quoted strings and character constants properly. C comments do not nest.

#include <stdio.h>

/* Declare state machine states */
#define NORMAL 0
#define IN_SINGLELINE 1
#define IN_MULTILINE 2
#define IN_STRING 3
#define IN_CHAR 4

/* Note that I do not remove the line, only the comment body 
 * The output will contain blank lines in place of some comments */

int main()
{
	/* Declare useful variables */
	int c, prev_c;
	int state, prev_state;
	state = prev_state = NORMAL;

	while ((c = getchar()) != EOF) {
		if (state == IN_SINGLELINE) {
			if (c == '\n') {
				putchar(c);
				state = NORMAL;
			}
		} else if (state == IN_MULTILINE) {
			if (prev_c == '*' && c == '/') {
				state = NORMAL;
			}
		} else {
			if (state != NORMAL || (c != '/' && prev_c != '/')) {
				/* Print the character unless we're in normal mode and handling entering
				 * a comment */
				putchar(c);
			}

			if (state == IN_STRING) {
				if (prev_c != '\\' && c == '"') {
					state = NORMAL;
				}
			} else if (state == IN_CHAR) {
				if (prev_c != '\\' && c == '\'') {
					state = NORMAL;
				}
			} else { // state == NORMAL
				if (c == '"') {
					state = IN_STRING;
				} else if (c == '\'') {
					state = IN_CHAR;
				} else if (prev_c == '/') {
					if (c == '/') {
						state = IN_SINGLELINE;
					} else if (c == '*') {
						state = IN_MULTILINE;
					} // else invalid
				}
			}
		}

		prev_c = c;
	}
}