Skip to content

Commit 01ab0f8

Browse files
committed
Defines lr1 items
1 parent fff4d06 commit 01ab0f8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/syntactes/_item.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,40 @@ def __eq__(self, other) -> bool:
5959
return False
6060

6161
return self.rule == other.rule and self.position == other.position
62+
63+
64+
class LR1Item(LR0Item):
65+
"""
66+
Item of LR1 parser. Contains rule, current position in rule and lookahead token.
67+
Current position is denoted with the dot '.'.
68+
"""
69+
70+
def __init__(self, rule: Rule, position: int, lookahead_token: Token) -> None:
71+
self.rule = rule
72+
self.position = position
73+
self.lookahead_token = lookahead_token
74+
75+
def __repr__(self) -> str:
76+
return f"<LR1Item: {self}>"
77+
78+
def __str__(self) -> str:
79+
rhs = [s for s in self.rule.rhs]
80+
rhs.insert(self.position, ".")
81+
return (
82+
f"{self.rule.lhs} -> "
83+
+ " ".join(map(str, rhs))
84+
+ f", {self.lookahead_token}"
85+
)
86+
87+
def __hash__(self) -> int:
88+
return hash((self.rule, self.position, self.lookahead_token))
89+
90+
def __eq__(self, other) -> bool:
91+
if not isinstance(other, LR1Item):
92+
return False
93+
94+
return (
95+
self.rule == other.rule
96+
and self.position == other.position
97+
and self.lookahead_token == other.lookahead_token
98+
)

0 commit comments

Comments
 (0)