I am trying to write a simple grammar which accepts statements of the form
7 = var int
9 = abc float
But with the following LEX and YACC code, the generated parser issues syntax error (Calls yyerror)
LEX :---
[0-9]+ { yylval.num = atof(yytext); return NUM; }
"int" return INT;
"float" return FLOAT;
[a-z]+ { yylval.str = strdup(yytext); return ID; }
\n /* Ignore end of lines */
[ \t]+ /* Ignore white spaces and tabs */
YACC:---
%%
commands: /* empty */
| commands command
;
command:
int_exp
|
float_exp
;
int_exp: exp INT
;
float_exp: exp FLOAT
;
exp : NUM '=' ID
;
%%
You need to define a token for '='
in your lex file and use its terminal symbol name instead of '='
in your grammar definition.
[0-9]+ { yylval.num = atof(yytext); return NUM; }
"int" return INT;
"float" return FLOAT;
[a-z]+ { yylval.str = strdup(yytext); return ID; }
\n /* Ignore end of lines */
[ \t]+ /* Ignore white spaces and tabs */
"=" return ASSIGN;
exp : NUM ASSIGN ID
;