/* paragrafo 1.9 pagina 31 esercizio 1.23 Rimuove i commenti da un programma in C. Gestisce le stringhe tra virgolette e le costanti carattere tra apici. */ #include <stdio.h> #define SI 1 #define NO 0 int toggle(int); int remark_remove(void); int main() { int c1, c2; int car_flag = NO; int str_flag = NO; int esc_flag = NO; if ((c1 = getchar()) != EOF) while ((c2 = getchar()) != EOF) { if (c1 == '"' && car_flag == NO) if (esc_flag == NO) str_flag = toggle(str_flag); else esc_flag = NO; else if (c1 == '\'' && str_flag == NO) if (esc_flag == NO) car_flag = toggle(car_flag); else esc_flag = NO; else if (c1 == '\\' && (str_flag == SI || car_flag == SI)) esc_flag = toggle(esc_flag); else esc_flag = NO; if (c1 == '/' && c2 == '*' && car_flag == NO && str_flag == NO) c1 = remark_remove(); else { putchar(c1); c1 = c2; } } if (c1 != EOF) putchar(c1); if (c1 != '\n') putchar('\n'); return 0; } int toggle(int status) { if (status == NO) return SI; else return NO; } int remark_remove() { int star, slash; if ((star = getchar()) != EOF) while ((slash = getchar()) != EOF) if (star == '*' && slash == '/') return getchar(); else star = slash; return '\a'; }