int main( ) { int c; extern int brace, brack, paren; while ((c = getchar()) != EOF){ if (c == '/'){ if((c = getchar()) == '*') in_comment(); else search(c); }else if(c == '\'' || c == '"') in_quote(c); else search(c); if(brace < 0){ printf("Unbalanced braces\n"); brace = 0; }else if(brack < 0){ printf("Unbalanced bracets\n"); brack = 0; }else if(paren < 0){ printf("Unbalanced parentheses\n"); paren = 0; } } if(brace > 0) printf("Unbalanced braces\n"); if(brack > 0) printf("Unbalanced brackets\n"); if(paren > 0) printf("Unbalanced parenthese\n"); return 0; }
/* C语言程序初步的语法检查 */ main(void) { int c; extern int brace, brack, paren; while ((c = getchar()) != EOF) { if (c == '/') { if ((c = getchar()) == '*') in_comment(); /* 评论内 */ else search(c); } else if (c == '\'' || c == '"') in_quote(c); /* 引用内 */ else search(c); if (brace < 0) { /* 输出错误 */ printf("Unbalanced braces\n"); /* 大括号 */ brace = 0; } else if (brack < 0) { printf("Unbalanced brackets\n"); /* 方括号 */ brack = 0; } else if (paren < 0) { printf("Unbalanced parentheses\n"); /* 圆括号 */ paren = 0; } } if (brace > 0) /* 输出错误 */ printf("Unbalanced braces\n"); if (brack > 0) printf("Unbalanced brackets\n"); if (paren > 0) printf("Unbalanced parenthess\n"); }
main() { int c, d; extern int brace, brack, paren; brace = brack = paren = 0; extern nml; while( ( c = getchar() ) != EOF ) { if ( c == '\n' ) ++nml; else if ( c == '/' ) { if ( ( c = getchar() ) == '*' ) in_comment(); else if ( c == '\n' ) ++nml; else search( c ); } else if ( c == '\'' || c == '"' ) in_quote( c ); else search( c ); if ( brace < 0 ) { printf("Unbalance barces at line %d.\n", nml); brace = 0; } else if ( brack < 0 ) { printf("Unbalance brackeks at line %d.\n", nml); brack = 0; } else if ( paren < 0 ) { printf("Unbalance parentheses at line %d.\n", nml); paren = 0; } } if ( brace > 0 ) printf("Unbalance barces.\n"); if ( brack > 0 ) printf("Unbalance brackets.\n"); if ( paren > 0 ) printf("Unbalance parentheses.\n"); }
void rcomment(int c) { int d; if (c=='/') { if ((d=getchar())=='*') in_comment(); else if (d=='/') { putchar(c); rcomment(d); } else { putchar(c); putchar(d); } } else if (c=='\'' || c=='"') echo_quote(c); else putchar(c); }
/*rcomment: czytaj każdy znak, usuń komentarze*/ void rcomment(int c) { int d; if (c == '/') if ((d = getchar()) == '*') in_comment(); /*początek komentarza*/ else if (d == '/') { putchar(c); /*drugi znak / */ rcomment(d); } else { putchar(c); /*to nie jest komentarz ;)*/ putchar(d); } else if (c == '\'' || c == '"') echo_quote(c); /*to początek stałej*/ else putchar(c); /*to nie jest komentarz*/ }
/* rcomment: read each character, remove the comments */ void rcomment(int c) { int d; if (c == '/') if ((d = getchar()) == '*') in_comment(); /* beginning comment */ else if (d == '/') { /* another slash */ putchar(c); rcomment(d); } else { putchar(c); /* not a comment */ putchar(d); } else if (c == '\'' || c == '"') echo_quote(c); /* quote begins */ else putchar(c); /* not a comment */ }
/* 阅读每个字节,移除注释 */ void rcomment(int c) { int d; if (c == '/') if ((d = getchar()) == '*') in_comment(); /* 开始注释 */ else if (d == '/') { putchar(c); /* 另一个斜杠 */ rcomment(d); } else { putchar(c); /* 不是注释 */ putchar(d); } else if (c == '\'' || c == '"') /* 引号中的内容原样输出 */ echo_quote(c); /* 引用开始 */ else putchar(c); /* 不是注释 */ }
/*rcomment:处理读入的每个字符,删除注释,首先是搜索起始标志*/ void rcomment(int c) { int d; /*用来储存c后面读入的一个字符*/ if(c=='/') /*当遇到一个/时,需要进行后续的判断*/ { if((d=getchar())=='*') /* 再读入一个字符,如果是*号,d已经读入,后续不需读入,而且必须输出 */ in_comment(); /* 说明进入注释,调用in_comment()函数寻找结束标志,删除注释 */ else if (d=='/') /* 如果又出现一个/ */ { putchar(c); /*则c一定不是属于注释,把c原样输出*/ rcomment(d); /*****递归调用rcomment函数,继续对d进行检测,而实际上只是c==‘/’的情况******/ } else /* 第一个/出现后,之后读入的一个字符,不是/也不是* */ { putchar(c); /*c,d均不属于注释,c,d均原样输出*/ putchar(d); } } else if(c=='\''||c=='"') /*出现单引号或双引号*/ echo_quote(c);/*调用echo_quote函数处理*/ else /*其他情况表示即不是/也不是引号*/ putchar(c);/*原样输出*/ }
int findclose(int c) { int d; while((d = getchar())!=EOF) { if(d == '{') { findclose('}'); } else if(d == '[') { findclose(']'); } else if(d == '(') { findclose(')'); } else if(d == '/') { if ((d = getchar()) == '*') in_comment(); } else if(d == '\'' || d == '"') { echo_quote(d); } else if(c == d) { return 0; } else if(d == '}' || d == ']' || d == ')') { printf("find %c, unexpected %c\nexit.\n", c, d); _exit(-1); } } if(c != 255) { printf("not match, %c\n", c); _exit(-1); } printf("ok\n"); return 0; }