Debian Codegraph @

documentation/manual/demos/calc/parser/grammar

raw
1 %baseclass-preinclude cmath
2 %stype double
3
4 %token NUM
5 %left '-' '+'
6 %left '*' '/'
7 %left NEG // negation--unary minus
8 %right '^' // exponentiation
9
10 %%
11
12 input:
13 // empty
14 |
15 input line
16 ;
17
18 line:
19 '\n'
20 |
21 exp '\n'
22 {
23 std::cout << "\t" << $1 << '\n';
24 }
25 ;
26
27 exp:
28 NUM
29 |
30 exp '+' exp
31 {
32 $$ = $1 + $3;
33 }
34 |
35 exp '-' exp
36 {
37 $$ = $1 - $3;
38 }
39 |
40 exp '*' exp
41 {
42 $$ = $1 * $3;
43 }
44 |
45 exp '/' exp
46 {
47 $$ = $1 / $3;
48 }
49 |
50 '-' exp %prec NEG
51 {
52 $$ = -$2;
53 }
54 |
55 // Exponentiation:
56 exp '^' exp
57 {
58 $$ = pow($1, $3);
59 }
60 |
61 '(' exp ')'
62 {
63 $$ = $2;
64 }
65 ;
66