Skip to content

Commit c3e2215

Browse files
committed
Defines lr1 parsing table
1 parent ddc4569 commit c3e2215

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
from .entry import Entry
22
from .conflict import Conflict, ConflictType
3-
from .table import LR0ParsingTable, SLRParsingTable
3+
from .table import LR0ParsingTable, SLRParsingTable, LR1ParsingTable, ParsingTable

src/syntactes/parsing_table/table.py

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
from typing import Iterable, Optional, TypeAlias
1+
from typing import Iterable, Optional, Protocol, TypeAlias
22

33
from syntactes import Grammar, Token
44
from syntactes._action import Action
5-
from syntactes._state import LR0State
5+
from syntactes._state import LR0State, LR1State, State
66
from syntactes.parsing_table import Conflict, Entry
77

88
Row: TypeAlias = dict[Token, list[Action]]
99

1010

11+
class ParsingTable(Protocol):
12+
rows: dict[State, Row]
13+
14+
@staticmethod
15+
def from_entries(entries: Iterable[Entry], grammar: Grammar) -> "ParsingTable": ...
16+
17+
1118
class LR0ParsingTable:
1219
"""
1320
Table that contains all the transitions from state to state with a symbol.
@@ -19,13 +26,11 @@ def __init__(self, grammar: Grammar) -> None:
1926
self._initial_state = None
2027

2128
@staticmethod
22-
def from_entries(
23-
entries: Iterable[Entry], tokens: Iterable[Token]
24-
) -> "LR0ParsingTable":
29+
def from_entries(entries: Iterable[Entry], grammar: Grammar) -> "LR0ParsingTable":
2530
"""
2631
Create a parsing table from the given entries.
2732
"""
28-
table = LR0ParsingTable(tokens)
33+
table = LR0ParsingTable(grammar)
2934
{table.add_entry(entry) for entry in entries}
3035
return table
3136

@@ -133,3 +138,46 @@ def from_entries(
133138

134139
def _header_str(self) -> str:
135140
return "SLR PARSING TABLE"
141+
142+
143+
class LR1ParsingTable(LR0ParsingTable):
144+
"""
145+
Table that contains all the transitions from state to state with a symbol.
146+
"""
147+
148+
def __init__(self, grammar: Grammar) -> None:
149+
self.rows: dict[LR1State, Row] = dict()
150+
self._grammar = grammar
151+
self._initial_state = None
152+
153+
@staticmethod
154+
def from_entries(
155+
entries: Iterable[Entry], tokens: Iterable[Token]
156+
) -> "LR1ParsingTable":
157+
"""
158+
Create a parsing table from the given entries.
159+
"""
160+
table = LR1ParsingTable(tokens)
161+
{table.add_entry(entry) for entry in entries}
162+
return table
163+
164+
@property
165+
def initial_state(self) -> LR1State:
166+
return self._initial_state
167+
168+
def get_actions(self, state: LR1State, token: Token) -> Optional[list[Action]]:
169+
"""
170+
Get the actions from state with given number with `token`.
171+
If there are no actions, returns None.
172+
"""
173+
return self.rows.get(state, {}).get(token, None)
174+
175+
def get(self, state: LR1State) -> Optional[Row]:
176+
"""
177+
Get the mapping of tokens to actions for the given state number.
178+
Returns None if the state is not found.
179+
"""
180+
return self.rows.get(state, None)
181+
182+
def _header_str(self) -> str:
183+
return "LR1 PARSING TABLE"

0 commit comments

Comments
 (0)