-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathcalculator-assoc.g
46 lines (34 loc) · 1.06 KB
/
calculator-assoc.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Precedence and associativity rules:
- If the token's precedence is higher, the choice is to shift:
E -> E + E •
E -> E • * E (choose to shift on `*` since its precedence is higher than of `+`)
- If the rule's precedence is higher, the choice is to reduce:
E -> E * E • (choose to reduce since precedence of the production is higher than of `+`)
E -> E • + E
- If they have equal precedence, the choice is made based on the associativity of that precedence level:
E -> E * E • (choose to reduce since precedence is the same `*` is left-associative)
E -> E • * E
This case we want `id * id * id` to be left-associative, i.e.
`(id * id) * id`, not right-associative, that would be `id * (id * id)`.
*/
{
"lex": {
"rules": [
["id", "return 'id'"],
["\\*", "return '*'"],
["\\+", "return '+'"]
]
},
"operators": [
["left", "+"],
["left", "*"]
],
"bnf": {
"E": [
"E + E",
"E * E",
"id"
]
}
}