I am new to Lex & Yacc programming and just getting started with grammar. I cam across this program on the net and have been trying to understand it. Following are the Lex and Yacc code-snippets for a simple calculator :
Lex rule:
%%
[a-z] {
yylval = *yytext - 'a';
return VARIABLE;
}
[0-9]+ {
yylval = atoi(yytext);
return INTEGER;
}
[-+()=/*\n] { return *yytext; }
[ \t] ;
. yyerror("invalid character");
%%
YACC grammar:
%%
program:
program statement '\n'
|
;
statement:
expr { printf("%d\n", $1); }
| VARIABLE '=' expr { sym[$1] = $3; }
;
expr:
INTEGER
| VARIABLE { $$ = sym[$1]; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
;
%%
Can anyone please help me understand step by step how the input expression x = 3 + 4
will be processed/parsed ?
As per my understanding, while processing the input, 'x
' will return VARIABLE whereas 3
, 5
and 4
will be returned as INTEGER by Lex. However, in Yacc, as per the grammar, since a VARIABLE could be deduced as expr, the expression will become: expr = expr '+' expr
So how will this be reduced to get VARIABLE '=' expr { sym[$1] = $3; }
?
Any help is appreciated.
Thanks