%{ #include #include "tree.h" extern char *yytext; extern EXP *theexpression; void yyerror() { printf("syntax error before %s\n",yytext); } %} %union { int intconst; char *stringconst; struct EXP *exp; } %token tINTCONST %token tIDENTIFIER %type program exp %start program %left '+' '-' %left '*' '/' %% program: exp { theexpression = $1;} ; exp : tIDENTIFIER {$$ = makeEXPid($1);} | tINTCONST {$$ = makeEXPintconst($1);} | exp '*' exp {$$ = makeEXPtimes($1,$3);} | exp '/' exp {$$ = makeEXPdiv($1,$3);} | exp '+' exp {$$ = makeEXPplus($1,$3);} | exp '-' exp {$$ = makeEXPminus($1,$3);} | '(' exp ')' {$$ = $2;} ; %%