Skip to content
Newer
Older
100644 36 lines (28 sloc) 770 Bytes
244e3b1 @boyland Added Calc example
boyland authored
1 /*
2 * A Parser for bison's "calc" example language.
3 */
4
5 %token EOF
6 %token<Double> NUM
7
8 %type <Double> exp
9
10 %left '-' '+'
11 %left '*' '/'
12 %left UMINUS
13 %right '^'
14
15 %%
16 program :
17 | program action '\n'
18 ;
19
20 action : { }
21 | exp { println($1); }
22 ;
23
24 exp: NUM { $$ = $1; }
25 | exp '+' exp { $$ = $1 + $3; }
26 | exp '-' exp { $$ = $1 - $3; }
27 | exp '*' exp { $$ = $1 * $3; }
28 | exp '/' exp { $$ = $1 / $3; }
29 | '-' exp %prec UMINUS { $$ = -$2; }
30 | exp '^' exp { $$ = math.pow ($1, $3); }
31 | '(' exp ')' { $$ = $2; }
32 ;
33 %%
34
35 def yyerror(s:String) = println(s);
36
Something went wrong with that request. Please try again.