%{ #include #include "syntree.h" #define YYDEBUG 1 extern int yylex(); extern int yyerror(char* s); %} %left '+' '-' %left '*' '/' %union { int num; char chr; nodeType* ptr; } %token PRINT %token NUM %token ID %type E %% S : E { print($1); freeNode($1); } E : E '+' E { $$ = binOp('+', $1, $3); } | E '-' E { $$ = binOp('-', $1, $3); } | E '*' E { $$ = binOp('*', $1, $3); } | E '/' E { $$ = binOp('/', $1, $3); } | '(' E ')' { $$ = $2; } | NUM { $$ = con($1); } | ID { $$ = id($1 - 'a'); } ; %% int main() { yydebug = 0; yyparse(); return 0; }