Skip to content

Commit e824720

Browse files
committed
Defines lr1 states
1 parent 01ab0f8 commit e824720

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/syntactes/_state.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Iterable
22

3-
from syntactes._item import LR0Item
3+
from syntactes._item import LR0Item, LR1Item
44

55

66
class LR0State:
@@ -45,7 +45,37 @@ def __hash__(self) -> int:
4545
return hash(frozenset(self.items))
4646

4747
def __eq__(self, other) -> bool:
48-
if not isinstance(other, LR0State):
48+
if not isinstance(other, self.__class__):
4949
return False
5050

5151
return self.items == other.items
52+
53+
54+
class LR1State(LR0State):
55+
"""
56+
State of LR1 parser. An LR1 state is a set of LR1 items.
57+
"""
58+
59+
def __init__(self) -> None:
60+
self.number = None
61+
self.items = set()
62+
self.is_final = False
63+
64+
@staticmethod
65+
def from_items(items: Iterable[LR1Item]) -> "LR1State":
66+
"""
67+
Create an LR1 state from a set of LR1 items.
68+
"""
69+
state = LR1State()
70+
{state.add_item(item) for item in items}
71+
72+
return state
73+
74+
def add_item(self, item: LR1Item) -> None:
75+
"""
76+
Adds an item to the state.
77+
"""
78+
self.items.add(item)
79+
80+
def __repr__(self) -> str:
81+
return f"<LR1State: {self.number}>"

0 commit comments

Comments
 (0)