Skip to content

Commit 5caa44e

Browse files
committed
Adds SLRParsingTable class
1 parent 5d0a758 commit 5caa44e

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/syntactes/generator.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from syntactes._item import LR0Item
33
from syntactes._state import LR0State
44
from syntactes.grammar import Grammar
5-
from syntactes.table import Entry, ParsingTable
5+
from syntactes.table import Entry, LR0ParsingTable, SLRParsingTable
66
from syntactes.token import Token
77

88

@@ -14,7 +14,7 @@ class LR0Generator:
1414
def __init__(self, grammar: Grammar) -> None:
1515
self.grammar = grammar
1616

17-
def generate(self) -> ParsingTable:
17+
def generate(self) -> LR0ParsingTable:
1818
"""
1919
Generates an LR0 parsing table for the configured grammar.
2020
"""
@@ -23,7 +23,7 @@ def generate(self) -> ParsingTable:
2323

2424
entries = shift_entries | reduce_entries
2525

26-
table = ParsingTable.from_entries(entries, self.grammar)
26+
table = LR0ParsingTable.from_entries(entries, self.grammar)
2727

2828
return table
2929

@@ -233,6 +233,19 @@ def _create_reduce_entries(self, states: set[LR0State]) -> set[Entry]:
233233

234234

235235
class SLRGenerator(LR0Generator):
236+
def generate(self) -> SLRParsingTable:
237+
"""
238+
Generates an SLR parsing table for the configured grammar.
239+
"""
240+
states, shift_entries = self._create_states_and_shift_entries()
241+
reduce_entries = self._create_reduce_entries(states)
242+
243+
entries = shift_entries | reduce_entries
244+
245+
table = SLRParsingTable.from_entries(entries, self.grammar)
246+
247+
return table
248+
236249
def _create_reduce_entries(self, states: set[LR0State]) -> set[Entry]:
237250
"""
238251
Computes and returns the entries for reduce actions and the accept action.

src/syntactes/table.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from typing import Iterable, Optional, TypeAlias
22

33
from syntactes._action import Action
4-
from syntactes._state import State
4+
from syntactes._state import LR0State
55
from syntactes.grammar import Grammar
66
from syntactes.token import Token
77

@@ -14,7 +14,7 @@ class Entry:
1414
a state to another state via a symbol.
1515
"""
1616

17-
def __init__(self, from_state: State, token: Token, action: Action) -> None:
17+
def __init__(self, from_state: LR0State, token: Token, action: Action) -> None:
1818
self.from_state = from_state
1919
self.token = token
2020
self.action = action
@@ -39,23 +39,23 @@ def __eq__(self, other) -> bool:
3939
)
4040

4141

42-
class ParsingTable:
42+
class LR0ParsingTable:
4343
"""
4444
Table that contains all the transitions from state to state with a symbol.
4545
"""
4646

4747
def __init__(self, grammar: Grammar) -> None:
48-
self.rows: dict[State, Row] = dict()
48+
self.rows: dict[LR0State, Row] = dict()
4949
self._grammar = grammar
5050

51-
def get_actions(self, state: State, token: Token) -> Optional[list[Action]]:
51+
def get_actions(self, state: LR0State, token: Token) -> Optional[list[Action]]:
5252
"""
5353
Get the actions from state with given number with `token`.
5454
If there are no actions, returns None.
5555
"""
5656
return self.rows.get(state, {}).get(token, None)
5757

58-
def get(self, state: State) -> Optional[Row]:
58+
def get(self, state: LR0State) -> Optional[Row]:
5959
"""
6060
Get the mapping of tokens to actions for the given state number.
6161
Returns None if the state is not found.
@@ -73,11 +73,11 @@ def add_entry(self, entry: Entry) -> None:
7373
@staticmethod
7474
def from_entries(
7575
entries: Iterable[Entry], tokens: Iterable[Token]
76-
) -> "ParsingTable":
76+
) -> "LR0ParsingTable":
7777
"""
7878
Create a parsing table from the given entries.
7979
"""
80-
table = ParsingTable(tokens)
80+
table = LR0ParsingTable(tokens)
8181
{table.add_entry(entry) for entry in entries}
8282
return table
8383

@@ -128,3 +128,19 @@ def _table_pretty_str(self) -> str:
128128

129129
def _header_str(self) -> str:
130130
return "LR0 PARSING TABLE"
131+
132+
133+
class SLRParsingTable(LR0ParsingTable):
134+
@staticmethod
135+
def from_entries(
136+
entries: Iterable[Entry], tokens: Iterable[Token]
137+
) -> "SLRParsingTable":
138+
"""
139+
Create a parsing table from the given entries.
140+
"""
141+
table = SLRParsingTable(tokens)
142+
{table.add_entry(entry) for entry in entries}
143+
return table
144+
145+
def _header_str(self) -> str:
146+
return "SLR PARSING TABLE"

0 commit comments

Comments
 (0)