Skip to content

Commit c0f4798

Browse files
committed
Adds example.py
1 parent b609c80 commit c0f4798

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

example.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from syntactes.generator import LR0Generator
2+
from syntactes.grammar import Grammar
3+
from syntactes.rule import Rule
4+
from syntactes.token import Token
5+
6+
EOF = Token.eof()
7+
S = Token("S", False)
8+
E = Token("E", False)
9+
T = Token("T", False)
10+
x = Token("x", True)
11+
PLUS = Token("+", True)
12+
13+
tokens = {EOF, S, E, T, x, PLUS}
14+
15+
16+
# S -> E $
17+
rule_1 = Rule(0, S, E, EOF)
18+
# E -> T + E
19+
rule_2 = Rule(1, E, T, PLUS, E)
20+
# E -> T
21+
rule_3 = Rule(2, E, T)
22+
# T -> x
23+
rule_4 = Rule(3, T, x)
24+
25+
rules = (rule_1, rule_2, rule_3, rule_4)
26+
27+
grammar = Grammar(rule_1, rules, tokens)
28+
29+
generator = LR0Generator(grammar)
30+
31+
table = generator.generate()
32+
33+
print(table.pretty_str())
34+

0 commit comments

Comments
 (0)