/* performs rudimentary error checking on C programs, warning of * unbalanced parenthesis, brackets, and braces (single qoutes, double * qoutes, escape sequences, and comments are handled appropriately) */ #include int lparens; int rparens; int lbracks; int rbracks; int lbraces; int rbraces; void check(int c) { switch (c) { case '(': lparens++; break; case ')': rparens++; break; case '[': lbracks++; break; case ']': rbracks++; break; case '{': lbraces++; break; case '}': rbraces++; break; case '\'': while ((c=getchar()) != '\'' && c != EOF) if (c == '\\') getchar(); break; case '\"': while ((c=getchar()) != '\"' && c != EOF) if (c == '\\') getchar(); break; case '/': if ((c=getchar()) == '*') { while ((c=getchar()) != EOF) { if (c == '*') { if ((c=getchar()) == '/') break; } } } else { check(c); } break; case '\\': getchar(); break; } } int main(void) { int c; while((c=getchar()) != EOF) check(c); if (lparens != rparens) printf("mismatched parenthesis: %d, %d\n", lparens, rparens); if (lbraces != rbraces) printf("mismatched braces: %d, %d\n", lbraces, rbraces); if (lbracks != rbracks) printf("mismatched brackets: %d, %d\n", lbracks, rbracks); return 0; }