%{ #include %} %token DIGIT %% line : expr '\n' {printf("%d\n", $1);} /* $1 is first arg on RHS */ ; expr : expr '+' term {$$ = $1 + $3;} /* $$ is attribute value of LHS */ | term ; term : term '*' factor {$$ = $1 * $3;} /* $$ is attribute value of LHS */ | factor ; factor : '(' factor ')' {$$ = $2;} | DIGIT ; %% yylex() { int c; c = getchar(); if (isdigit(c)) { yylval = c - '0'; return DIGIT; } return c; }