Skip to content

Instantly share code, notes, and snippets.

@DmitrySoshnikov
Created November 29, 2016 00:33
Show Gist options
  • Save DmitrySoshnikov/817913fe8b53120b7acde8f397451f35 to your computer and use it in GitHub Desktop.
Save DmitrySoshnikov/817913fe8b53120b7acde8f397451f35 to your computer and use it in GitHub Desktop.
LL(1) boolean
%%
boolean
: term bool'
;
bool'
: 'OR' term bool'
| /* epsilon */
;
term
: factor term'
;
term'
: 'AND' factor term'
| /* epsilon */
;
factor
: primary
| 'NOT' primary
;
primary
: predicate
| '(' boolean ')'
;
predicate
: expr comp_op expr
;
expr
: 'x'
| 'y'
| 'z'
;
comp_op
: '='
| '>'
;
.dmitrys:syntax$ ./bin/syntax -g ~/Documents/bool.ll1 -t -w -p '(x > y OR y = z) AND z = x'
Parsing mode: LL(1).
Grammar:
1. boolean -> term bool'
2. bool' -> 'OR' term bool'
3. | ε
4. term -> factor term'
5. term' -> 'AND' factor term'
6. | ε
7. factor -> primary
8. | 'NOT' primary
9. primary -> predicate
10. | '(' boolean ')'
11. predicate -> expr comp_op expr
12. expr -> 'x'
13. | 'y'
14. | 'z'
15. comp_op -> '='
16. | '>'
LL(1) parsing table:
┌───────────┬──────┬───────┬───────┬─────┬─────┬─────┬─────┬─────┬─────┬─────┬───┐
│ │ 'OR' │ 'AND' │ 'NOT' │ '(' │ ')' │ 'x' │ 'y' │ 'z' │ '=' │ '>' │ $ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ boolean │ │ │ 1 │ 1 │ │ 1 │ 1 │ 1 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ bool' │ 2 │ │ │ │ 3 │ │ │ │ │ │ 3 │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ term │ │ │ 4 │ 4 │ │ 4 │ 4 │ 4 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ term' │ 6 │ 5 │ │ │ 6 │ │ │ │ │ │ 6 │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ factor │ │ │ 8 │ 7 │ │ 7 │ 7 │ 7 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ primary │ │ │ │ 10 │ │ 9 │ 9 │ 9 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ predicate │ │ │ │ │ │ 11 │ 11 │ 11 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ expr │ │ │ │ │ │ 12 │ 13 │ 14 │ │ │ │
├───────────┼──────┼───────┼───────┼─────┼─────┼─────┼─────┼─────┼─────┼─────┼───┤
│ comp_op │ │ │ │ │ │ │ │ │ 15 │ 16 │ │
└───────────┴──────┴───────┴───────┴─────┴─────┴─────┴─────┴─────┴─────┴─────┴───┘
Parsing:
(x > y OR y = z) AND z = x
✓ Accepted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment