Skip to content

Commit 784b0fb

Browse files
committed
Defines test parsing table
1 parent d4f8c00 commit 784b0fb

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

src/syntactes/tests/data.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from syntactes import Grammar, Rule, Token
2+
from syntactes._action import Action
23
from syntactes._item import LR0Item
34
from syntactes._state import LR0State
5+
from syntactes.table import Entry, LR0ParsingTable, SLRParsingTable
46

57
EOF = Token.eof()
68
S = Token("S", False)
@@ -40,6 +42,7 @@ def state_2():
4042
item_1 = LR0Item(grammar.starting_rule, 1) # S -> E . $
4143
state = LR0State.from_items({item_1})
4244
state.set_number(2)
45+
state.set_final()
4346
return state
4447

4548

@@ -73,3 +76,42 @@ def state_6():
7376
state = LR0State.from_items({item_1})
7477
state.set_number(6)
7578
return state
79+
80+
81+
def lr0_parsing_table():
82+
table = LR0ParsingTable(grammar)
83+
table.add_entry(Entry(state_1(), E, Action.shift(state_2())))
84+
table.add_entry(Entry(state_1(), T, Action.shift(state_3())))
85+
table.add_entry(Entry(state_1(), x, Action.shift(state_5())))
86+
table.add_entry(Entry(state_2(), EOF, Action.accept()))
87+
table.add_entry(Entry(state_3(), x, Action.reduce(rule_3)))
88+
table.add_entry(Entry(state_3(), PLUS, Action.shift(state_4())))
89+
table.add_entry(Entry(state_3(), PLUS, Action.reduce(rule_3)))
90+
table.add_entry(Entry(state_3(), EOF, Action.reduce(rule_3)))
91+
table.add_entry(Entry(state_4(), x, Action.shift(state_5())))
92+
table.add_entry(Entry(state_4(), E, Action.shift(state_6())))
93+
table.add_entry(Entry(state_4(), T, Action.shift(state_3())))
94+
table.add_entry(Entry(state_5(), x, Action.reduce(rule_4)))
95+
table.add_entry(Entry(state_5(), PLUS, Action.reduce(rule_4)))
96+
table.add_entry(Entry(state_5(), EOF, Action.reduce(rule_4)))
97+
table.add_entry(Entry(state_6(), x, Action.reduce(rule_2)))
98+
table.add_entry(Entry(state_6(), PLUS, Action.reduce(rule_2)))
99+
table.add_entry(Entry(state_6(), EOF, Action.reduce(rule_2)))
100+
return table
101+
102+
103+
def slr_parsing_table():
104+
table = SLRParsingTable(grammar)
105+
table.add_entry(Entry(state_1(), x, Action.shift(state_5())))
106+
table.add_entry(Entry(state_1(), E, Action.shift(state_2())))
107+
table.add_entry(Entry(state_1(), T, Action.shift(state_3())))
108+
table.add_entry(Entry(state_2(), EOF, Action.accept()))
109+
table.add_entry(Entry(state_3(), PLUS, Action.shift(state_4())))
110+
table.add_entry(Entry(state_3(), EOF, Action.reduce(rule_3)))
111+
table.add_entry(Entry(state_4(), x, Action.shift(state_5())))
112+
table.add_entry(Entry(state_4(), E, Action.shift(state_6())))
113+
table.add_entry(Entry(state_4(), T, Action.shift(state_3())))
114+
table.add_entry(Entry(state_5(), PLUS, Action.reduce(rule_4)))
115+
table.add_entry(Entry(state_5(), EOF, Action.reduce(rule_4)))
116+
table.add_entry(Entry(state_6(), EOF, Action.reduce(rule_2)))
117+
return table

0 commit comments

Comments
 (0)