|
1 | 1 | from syntactes import Grammar, Rule, Token |
| 2 | +from syntactes._action import Action |
2 | 3 | from syntactes._item import LR0Item |
3 | 4 | from syntactes._state import LR0State |
| 5 | +from syntactes.table import Entry, LR0ParsingTable, SLRParsingTable |
4 | 6 |
|
5 | 7 | EOF = Token.eof() |
6 | 8 | S = Token("S", False) |
@@ -40,6 +42,7 @@ def state_2(): |
40 | 42 | item_1 = LR0Item(grammar.starting_rule, 1) # S -> E . $ |
41 | 43 | state = LR0State.from_items({item_1}) |
42 | 44 | state.set_number(2) |
| 45 | + state.set_final() |
43 | 46 | return state |
44 | 47 |
|
45 | 48 |
|
@@ -73,3 +76,42 @@ def state_6(): |
73 | 76 | state = LR0State.from_items({item_1}) |
74 | 77 | state.set_number(6) |
75 | 78 | 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