Debian Codegraph @

documentation/manual/demos/errorcalc/parser/grammar

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