%{ #include #include #include #include "ast.h" #define YYSTYPE struct ast * #include "y.tab.h" int yylex(void); void yyerror(struct ast **result, const char *str) { fprintf(stderr, "error: %s\n", str); } int yywrap() { return 1; } %} %token INTEGER PLUS MINUS TIMES DIVIDE BOPEN BCLOSE SEMICOLON %parse-param { struct ast **result } %% start : exprs { *result = $1; } ; exprs : { $$ = NULL; } | exprs expr SEMICOLON { $$ = ast_cons($2, $1); } ; expr : expr PLUS fact { $$ = ast_binop($1, plus, $3); } | expr MINUS fact { $$ = ast_binop($1, minus, $3); } | fact ; fact : fact TIMES basic { $$ = ast_binop($1, times, $3); } | fact DIVIDE basic { $$ = ast_binop($1, divide, $3); } | basic ; basic : INTEGER | BOPEN expr BCLOSE { $$ = $2; } ;