diff --git a/analyzers/c_analyzer/c_analyzer.l b/analyzers/c_analyzer/c_analyzer.l index 4a9eedc..a357e3e 100644 --- a/analyzers/c_analyzer/c_analyzer.l +++ b/analyzers/c_analyzer/c_analyzer.l @@ -6,6 +6,7 @@ extern char* path; int line_number = 1; int column_number = 1; +int error_count = 0; char* read_line(int line_number) { @@ -37,7 +38,7 @@ void yyerror(const char *s) { fprintf(stderr, "%s", read_line(line_number - 1)); for (int i = 0; i < column_number - 1; ++i) fprintf(stderr, "%c", ' '); fprintf(stderr, "%c\033[0m", '^'); - exit(-1); + error_count++; } %} diff --git a/analyzers/c_analyzer/c_analyzer.y b/analyzers/c_analyzer/c_analyzer.y index 714770d..40b6fa8 100644 --- a/analyzers/c_analyzer/c_analyzer.y +++ b/analyzers/c_analyzer/c_analyzer.y @@ -6,6 +6,10 @@ void yyerror(const char *s); extern int yylex(); extern char *yytext; extern FILE *yyin; +extern int error_count; + +void yyerrok(void); +void yyclearin(void); bool debug = false; // debug mode char* path; @@ -34,6 +38,7 @@ char* path; // Program - последовательность утверждений program: | program statement + | program error { yyerrok; } ; // Утверждение - либо блок {...}, либо выражение с ; в конце, @@ -77,6 +82,7 @@ statement: { if (debug) printf("\033[1;34mELSE BLOCK ENDED\033[0m\n"); } | func_decl + | error SEMICOLON { yyerrok; } ; func_decl: @@ -141,7 +147,6 @@ expr: int main(int argc, char **argv) { if (argc > 1) { - // debug mode if (argc > 2) { path = argv[2]; if (!strcmp(argv[1], "-d")) { @@ -151,16 +156,21 @@ int main(int argc, char **argv) { path = argv[1]; } - // default mode FILE *f = fopen(path, "r"); if (!f) { perror("\033[91mFail open file\033[0m"); return 1; } yyin = f; - } + yyparse(); - printf("\033[92m\nGood code\033[0m"); - return 0; + + if (error_count == 0) { + printf("\033[92m\nParsing completed successfully. Good code!\033[0m\n"); + return 0; + } else { + fprintf(stderr, "\033[91m\nParsing completed with %d error(s)\033[0m\n", error_count); + return 1; + } } \ No newline at end of file diff --git a/code.txt b/code.txt index 5bae4ee..4703fe2 100644 --- a/code.txt +++ b/code.txt @@ -4,7 +4,7 @@ func FUNC_1(a, b, c) { print x; } - x = x + 1; + x = x + 1 a = a + 1; } @@ -17,14 +17,14 @@ func FUNC_2() { x = x + 1; } while(x < 5); - x = 5; + x = 5 } func main() { // 123123132 /* test */ - x = 2 + 1 % 1; + x = 2 + 1 % 1;; if (!x && 1 || (x + (-100))) { // comment x = 3;