#include #define MAXBUFF 100 void ignore_line(void); int ignore_multiline(void); int pass_char_const(char fst); int pass_string(char fst); int flush_buff(char buff[], int bi); int is_blank(char c); /* remove comments from input; remove empty lines and trailing spaces * left by removed comments; Check for the following errors: * - invalid string literals * - invalid char constants * - non-terminating multi line comments */ int main() { int a, b; int bi = 0; // blank buffer index char blank_buff[MAXBUFF]; // blank buffer for (a = getchar(); (b = getchar()) != EOF; a = b) if (a == '/' && b == '*') { bi = 0; if (ignore_multiline()) { printf("\nerror : no terminator for multiline comment\n"); return 1; } b = getchar(); } else if (a == '/' && b == '/') { bi = 0; ignore_line(); putchar('\n'); b = getchar(); } else if (a == '\'') { bi = flush_buff(blank_buff, bi); putchar('\''); if (pass_char_const(b)) { printf("\nerror : invalid character constant\n"); return 1; } b = getchar(); } else if (a == '"') { bi = flush_buff(blank_buff, bi); putchar('"'); if (pass_string(b)) { printf("\nerror : invalid string literal\n"); return 1; } b = getchar(); } else if (is_blank(a)) { if (bi >= MAXBUFF) bi = flush_buff(blank_buff, bi); blank_buff[bi] = a; ++bi; } else if (a == '\n') { flush_buff(blank_buff, bi); blank_buff[0] = a; bi = 1; } else { bi = flush_buff(blank_buff, bi); putchar(a); } if (a != EOF) putchar(a); return 0; } /* ignore line of input */ void ignore_line() { int c; while ((c = getchar()) != EOF && c != '\n') ; } /* ignore input until comment termination occurs * return 1 if error otherwise 0 */ int ignore_multiline() { int a, b; for (a = getchar(); (b = getchar()) != EOF; a = b) if (a == '*' && b == '/') return 0; return 1; } /* pass the tail of a character constant from input to ouput * return 1 if error otherwise 0 */ int pass_char_const(char fst) { int c; putchar(fst); if (fst == '\\') { if ((c = getchar()) == EOF) return 1; putchar(c); } if ((c = getchar()) == EOF || c != '\'') return 1; putchar(c); return 0; } /* pass the tail (wihtout fst element) of a * C string literal from input to output. * 'fst' is the first character of the string tail * return 1 if error otherwise 0 */ int pass_string(char fst) { int snd; putchar(fst); for (; (snd = getchar()) != EOF; fst = snd) { putchar(snd); if (fst == '\n') break; if (snd == '"' && fst != '\\') return 0; } return 1; } /* print buffer 'buff' and return 0 */ int flush_buff(char buff[], int bi) { printf("%.*s", bi, buff); return 0; } int is_blank(char c) { return c == ' ' || c == '\t' || c == '\b'; }