%{ #include #include "tree.h" extern FILE* yyin; int yylex(); int yyerror(const char *s); %} %union { int num; char chr; node *n; } %token NUMBER MATHOP LBRACE RBRACE %type expr %% start : expr { printTree($1, 0); freeNode($1); } | expr start { printTree($1, 0); freeNode($1); } ; expr : NUMBER { $$ = newNode($1 + '0', NULL, NULL); } | LBRACE expr MATHOP expr RBRACE { $$ = newNode($3, $2, $4); } ; %% int yyerror(const char *s) { printf("Error: %s\n", s); return 0; } int main(int argc, char* argv[]) { FILE* input = fopen(argv[1], "r"); if (input == NULL) { yyerror("reading input file!"); return 1; } yyin = input; yyparse(); }