%{ #include #include void yyerror( const char *msg); /* called for parser syntax error */ int yylex( void); /* declare to avoid gcc warnings */ %} %token INTEGER %start expression %% primary_expression: INTEGER { $$ = $1; /* don't need this, $$=$1 is the default action */ } | '(' expression ')' { $$ = $2; } ; multiplicative_expression: primary_expression | multiplicative_expression '*' primary_expression { $$ = $1 * $3; } ; additive_expression: multiplicative_expression | additive_expression '+' multiplicative_expression { $$ = $1 + $3; } ; expression: additive_expression { printf( "%d\n", $1); } ; %% int main( int argc, char *argv[]) { return yyparse(); }