many errors added
parent
75960d3523
commit
16ab2f9260
|
@ -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++;
|
||||
}
|
||||
|
||||
%}
|
||||
|
|
|
@ -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");
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue