many errors added

master
serr 2025-04-01 22:17:41 +03:00
parent 75960d3523
commit 16ab2f9260
3 changed files with 20 additions and 9 deletions

View File

@ -6,6 +6,7 @@ extern char* path;
int line_number = 1; int line_number = 1;
int column_number = 1; int column_number = 1;
int error_count = 0;
char* read_line(int line_number) { char* read_line(int line_number) {
@ -37,7 +38,7 @@ void yyerror(const char *s) {
fprintf(stderr, "%s", read_line(line_number - 1)); fprintf(stderr, "%s", read_line(line_number - 1));
for (int i = 0; i < column_number - 1; ++i) fprintf(stderr, "%c", ' '); for (int i = 0; i < column_number - 1; ++i) fprintf(stderr, "%c", ' ');
fprintf(stderr, "%c\033[0m", '^'); fprintf(stderr, "%c\033[0m", '^');
exit(-1); error_count++;
} }
%} %}

View File

@ -6,6 +6,10 @@ void yyerror(const char *s);
extern int yylex(); extern int yylex();
extern char *yytext; extern char *yytext;
extern FILE *yyin; extern FILE *yyin;
extern int error_count;
void yyerrok(void);
void yyclearin(void);
bool debug = false; // debug mode bool debug = false; // debug mode
char* path; char* path;
@ -34,6 +38,7 @@ char* path;
// Program - последовательность утверждений // Program - последовательность утверждений
program: program:
| program statement | program statement
| program error { yyerrok; }
; ;
// Утверждение - либо блок {...}, либо выражение с ; в конце, // Утверждение - либо блок {...}, либо выражение с ; в конце,
@ -77,6 +82,7 @@ statement:
{ if (debug) printf("\033[1;34mELSE BLOCK ENDED\033[0m\n"); } { if (debug) printf("\033[1;34mELSE BLOCK ENDED\033[0m\n"); }
| func_decl | func_decl
| error SEMICOLON { yyerrok; }
; ;
func_decl: func_decl:
@ -141,7 +147,6 @@ expr:
int main(int argc, char **argv) { int main(int argc, char **argv) {
if (argc > 1) { if (argc > 1) {
// debug mode
if (argc > 2) { if (argc > 2) {
path = argv[2]; path = argv[2];
if (!strcmp(argv[1], "-d")) { if (!strcmp(argv[1], "-d")) {
@ -151,16 +156,21 @@ int main(int argc, char **argv) {
path = argv[1]; path = argv[1];
} }
// default mode
FILE *f = fopen(path, "r"); FILE *f = fopen(path, "r");
if (!f) { if (!f) {
perror("\033[91mFail open file\033[0m"); perror("\033[91mFail open file\033[0m");
return 1; return 1;
} }
yyin = f; yyin = f;
} }
yyparse(); 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; return 0;
} else {
fprintf(stderr, "\033[91m\nParsing completed with %d error(s)\033[0m\n", error_count);
return 1;
}
} }

View File

@ -4,7 +4,7 @@ func FUNC_1(a, b, c) {
print x; print x;
} }
x = x + 1; x = x + 1
a = a + 1; a = a + 1;
} }
@ -17,14 +17,14 @@ func FUNC_2() {
x = x + 1; x = x + 1;
} while(x < 5); } while(x < 5);
x = 5; x = 5
} }
func main() func main()
{ {
// 123123132 // 123123132
/* test */ /* test */
x = 2 + 1 % 1; x = 2 + 1 % 1;;
if (!x && 1 || (x + (-100))) { // comment if (!x && 1 || (x + (-100))) { // comment
x = 3; x = 3;