diff options
author | bd <bdunahu@operationnull.com> | 2025-01-28 16:22:49 -0500 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2025-01-28 16:22:49 -0500 |
commit | ce7189464a302872634d949cf06e9071b625bfcb (patch) | |
tree | adadc344f518642a009a2c12215e90bda420fc67 /src | |
parent | 9e09767e23a4edb6b31540195bfe885f83e080d7 (diff) |
Fix various lexer/parser bugs, pass all tests for binary ops
Diffstat (limited to 'src')
-rw-r--r-- | src/frontend/lexer.l | 13 | ||||
-rw-r--r-- | src/frontend/parser.y | 9 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/frontend/lexer.l b/src/frontend/lexer.l index 5b72086..40f6196 100644 --- a/src/frontend/lexer.l +++ b/src/frontend/lexer.l @@ -9,7 +9,8 @@ %option yylineno DIGIT [0-9] -ALPHA [[:alpha:]_] +PREID [a-zA-Z_] +PSTID [a-zA-Z0-9_] /*** rules section ***/ %% @@ -30,12 +31,18 @@ ALPHA [[:alpha:]_] "void" {return VOID;} "return" {return RET;} -{DIGIT}+ {yylval.ival = atol(yytext); return NUMBER;} -{ALPHA}+ { +{PREID}{PSTID}* { yylval.sval = strdup(yytext); return WORD; } +{DIGIT}+ {yylval.ival = atol(yytext); return NUMBER;} + +{PSTID}+ { + printf("Error at line %d: invalid identifier \"%s\"\n", yylineno, yytext); + return YYerror; +} + [[:space:]]+ {/* discard */} . { printf("Error at line %d: unrecognized symbol \"%s\"\n", yylineno, yytext); diff --git a/src/frontend/parser.y b/src/frontend/parser.y index aa58f64..bd5d127 100644 --- a/src/frontend/parser.y +++ b/src/frontend/parser.y @@ -66,10 +66,6 @@ exp: term { add_child($$, $1); add_child($$, $3); } -| un_op exp { - $$ = create_expr($1); - add_child($$, $2); - } ; term: factor { @@ -90,6 +86,10 @@ term: factor { add_child($$, $1); add_child($$, $3); } +| un_op exp { + $$ = create_expr($1); + add_child($$, $2); + } ; factor: NUMBER { @@ -113,4 +113,3 @@ un_op: COMP { void yyerror(Node **root, const char *msg) { printf("** Line %d: %s\n", yylineno, msg); } - |