%{ #include /* Required to compile with C++ */ #include #include extern int yyparse(); /* Required to compile with C++ */ extern void yyerror(char*); /* Required to compile with C++ */ extern int yylex(void); /* Required to compile with C++ */ %} %token DIGIT %% line: expr '\n' { printf("%d\n", $1); } ; expr: expr '+' term { $$ = $1 + $3; } | term ; term: term '*' factor { $$ = $1 * $3; } | factor ; factor: '(' expr ')' { $$ = $2; } | DIGIT ; %% int yylex(void) { int c; c = getchar(); if (isdigit(c)) { yylval = c - '0'; return DIGIT; } return c; } void yyerror(char *s) { fprintf(stderr, "%s\n", s); } int main() { yyparse(); return 0; }