%{ #include #include "Expr.h" int yylex(void); void yyerror (char const * s) { fprintf(stderr, "%s\n", s); } %} %union { long int intval; char * string; Expr expr; } %token TOK_NUMBER %token TOK_ID %token TOK_IF TOK_THEN TOK_ELSE %type expr term factor %start input %% factor : TOK_NUMBER { $$ = exprInt($1); } | TOK_ID { $$ = exprVar($1); } | '(' expr ')' { $$ = $2; } term : factor | term '*' factor { $$ = exprBin("*", $1, $3); } | term '/' factor { $$ = exprBin("/", $1, $3); } expr : term | expr '+' term { $$ = exprBin("+", $1, $3); } | expr '-' term { $$ = exprBin("-", $1, $3); } input : /* empty */ | input line /* line-by-line processing */ line : '\n' {} /* empty lines allowed */ | expr '\n' { printf(" = %ld\n", exprEval($1)); } %% int main ( void ) { return yyparse(); }