%{ #include int yylex(void); void yyerror(char *); int sym[26] %} %token INTEGER VARIABLE %left '+' '-' %left '*' '/' %left '(' ')' %% program: program expr '\n' { printf("%d\n", $2);} | ; expr: 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 ')' {$$ = $2;} | expr '/' expr { if($3==0) yyerror("divided by zero\n"); else $$ = $1 / $3; } ; %% void yyerror(char *s){ fprintf(stderr, "%s\n", s); } int main(void){ yyparse(); return 0; }