Skip to content

Commit e0876af

Browse files
committed
Updating tokens and parser
1 parent 7fdd432 commit e0876af

2 files changed

Lines changed: 41 additions & 33 deletions

File tree

analysis/parser.y

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
void yyerror(const char* s);
66
int yylex(void);
7+
8+
extern int yylex(void);
9+
extern FILE *yyin;
710
%}
811

912
%union {
@@ -27,7 +30,6 @@ int yylex(void);
2730
%token OR AND NOT
2831
%token ERROR
2932

30-
// Precedence declarations (lowest to highest)
3133
%nonassoc OR
3234
%nonassoc AND
3335
%left EQ NEQ
@@ -216,8 +218,4 @@ type:
216218
| VOID_TYPE { $$ = $1; }
217219
;
218220

219-
%%
220-
221-
void yyerror(const char* s) {
222-
fprintf(stderr, "Error: %s\n", s);
223-
}
221+
%%

analysis/tokens.l

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
%}
66

77
%%
8+
9+
"==" { return EQ; }
10+
"!=" { return NEQ; }
11+
">=" { return GTE; }
12+
"<=" { return LTE; }
13+
">" { return GT; }
14+
"<" { return LT; }
15+
816
"let" { return LET; }
917
"function" { return FUNCTION; }
1018
"if" { return IF; }
@@ -24,41 +32,43 @@
2432
"true" { yylval.boolean = 1; return BOOL; }
2533
"false" { yylval.boolean = 0; return BOOL; }
2634

27-
"==" { return EQ; }
28-
"!=" { return NEQ; }
29-
">=" { return GTE; }
30-
"<=" { return LTE; }
31-
">" { return GT; }
32-
"<" { return LT; }
33-
34-
"=" { return ASSIGN; }
35-
"+" { return PLUS; }
36-
"-" { return MINUS; }
37-
"*" { return MUL; }
38-
"/" { return DIV; }
39-
"%" { return MOD; }
35+
"=" { return ASSIGN; }
36+
"+" { return PLUS; }
37+
"-" { return MINUS; }
38+
"*" { return MUL; }
39+
"/" { return DIV; }
40+
"%" { return MOD; }
4041

41-
"(" { return LPAREN; }
42-
")" { return RPAREN; }
43-
"{" { return LBRACE; }
44-
"}" { return RBRACE; }
45-
";" { return SEMI; }
46-
":" { return COLON; }
47-
"," { return COMMA; }
48-
".." { return DOTDOT; }
42+
"(" { return LPAREN; }
43+
")" { return RPAREN; }
44+
"{" { return LBRACE; }
45+
"}" { return RBRACE; }
46+
";" { return SEMI; }
47+
":" { return COLON; }
48+
"," { return COMMA; }
49+
".." { return DOTDOT; }
4950

50-
"!("([^)\n]*)")" {
51-
yytext[yyleng - 1] = '\0'; // remove final ')'
52-
yylval.str = strdup(yytext + 2); // skip '!("'
53-
return BANG_EXPR;
51+
"!("[^)]*")" {
52+
yylval.str = strdup(yytext+2);
53+
yylval.str[strlen(yylval.str)-1] = '\0';
54+
return BANG_EXPR;
5455
}
5556

56-
"!"[^\n;]* { yylval.str = strdup(yytext + 1); return BANG_LINE; }
57+
"!"[^ \t\n\r=;]+ {
58+
yylval.str = strdup(yytext+1);
59+
return BANG_LINE;
60+
}
5761

5862
[a-zA-Z_][a-zA-Z0-9_]* { yylval.str = strdup(yytext); return IDENTIFIER; }
5963
[0-9]+ { yylval.integer = atoi(yytext); return INT; }
6064
\"([^\"\n]*)\" { yylval.str = strdup(yytext); return STRING; }
6165

62-
[ \t\r\n]+ ; // Skip whitespace
66+
[ \t\r\n]+ ; /* Skip whitespace */
67+
6368
. { return ERROR; }
69+
6470
%%
71+
72+
int yywrap() {
73+
return 1;
74+
}

0 commit comments

Comments
 (0)