Skip to content

Commit 568f311

Browse files
committed
correct indexed grammar annotations and imports
1 parent 8333168 commit 568f311

File tree

11 files changed

+227
-221
lines changed

11 files changed

+227
-221
lines changed

pyformlang/fst/fst.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,12 @@ def _add_transitions_to(self,
422422
idx: int) -> None:
423423
for head, transition in self._delta.items():
424424
s_from, input_symbol = head
425-
for s_to, output_symbol in transition:
425+
for s_to, output_symbols in transition:
426426
union_fst.add_transition(
427427
state_renaming.get_renamed_state(s_from, idx),
428428
input_symbol,
429429
state_renaming.get_renamed_state(s_to, idx),
430-
output_symbol)
430+
output_symbols)
431431

432432
def _add_extremity_states_to(self,
433433
union_fst: "FST",

pyformlang/indexed_grammar/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,23 @@
2323
"""
2424

2525
from .rules import Rules
26+
from .reduced_rule import ReducedRule
2627
from .consumption_rule import ConsumptionRule
2728
from .end_rule import EndRule
2829
from .production_rule import ProductionRule
2930
from .duplication_rule import DuplicationRule
3031
from .indexed_grammar import IndexedGrammar
32+
from ..objects.cfg_objects import CFGObject, Variable, Terminal, Epsilon
3133

3234

3335
__all__ = ["Rules",
36+
"ReducedRule",
3437
"ConsumptionRule",
3538
"EndRule",
3639
"ProductionRule",
3740
"DuplicationRule",
38-
"IndexedGrammar"]
41+
"IndexedGrammar",
42+
"CFGObject",
43+
"Variable",
44+
"Terminal",
45+
"Epsilon"]

pyformlang/indexed_grammar/consumption_rule.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@
33
the stack
44
"""
55

6-
from typing import List, Set, Any
6+
from typing import List, Set, Hashable, Any
77

8-
from pyformlang.cfg import Variable, Terminal
9-
from pyformlang.cfg.utils import to_variable, to_terminal
10-
from pyformlang.cfg.cfg_object import CFGObject
8+
from pyformlang.cfg import CFGObject, Variable, Terminal
119

1210
from .reduced_rule import ReducedRule
11+
from ..objects.cfg_objects.utils import to_variable, to_terminal
1312

1413

1514
class ConsumptionRule(ReducedRule):
@@ -27,14 +26,10 @@ class ConsumptionRule(ReducedRule):
2726
The non terminal on the right (here B)
2827
"""
2928

30-
@property
31-
def production(self) -> Terminal:
32-
raise NotImplementedError
33-
3429
def __init__(self,
35-
f_param: Any,
36-
left_term: Any,
37-
right_term: Any) -> None:
30+
f_param: Hashable,
31+
left_term: Hashable,
32+
right_term: Hashable) -> None:
3833
self._f = to_terminal(f_param)
3934
self._left_term = to_variable(left_term)
4035
self._right_term = to_variable(right_term)
@@ -50,6 +45,10 @@ def f_parameter(self) -> Terminal:
5045
"""
5146
return self._f
5247

48+
@property
49+
def production(self) -> Terminal:
50+
raise NotImplementedError
51+
5352
@property
5453
def left_term(self) -> Variable:
5554
"""Gets the symbol on the left of the rule
@@ -97,12 +96,12 @@ def terminals(self) -> Set[Terminal]:
9796
"""
9897
return {self._f}
9998

100-
def __repr__(self) -> str:
101-
return f"{self._left_term} [ {self._f} ] -> {self._right_term}"
102-
10399
def __eq__(self, other: Any) -> bool:
104100
if not isinstance(other, ConsumptionRule):
105101
return False
106102
return other.left_term == self.left_term \
107103
and other.right_term == self.right_term \
108104
and other.f_parameter == self.f_parameter
105+
106+
def __repr__(self) -> str:
107+
return f"{self._left_term} [ {self._f} ] -> {self._right_term}"

pyformlang/indexed_grammar/duplication_rule.py

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
A representation of a duplication rule, i.e. a rule that duplicates the stack
33
"""
44

5-
from typing import List, Set, Any
5+
from typing import List, Set, Hashable, Any
66

7-
from pyformlang.cfg import Variable, Terminal
8-
from pyformlang.cfg.utils import to_variable
9-
from pyformlang.cfg.cfg_object import CFGObject
7+
from pyformlang.cfg import CFGObject, Variable, Terminal
108

119
from .reduced_rule import ReducedRule
10+
from ..objects.cfg_objects.utils import to_variable
1211

1312

1413
class DuplicationRule(ReducedRule):
@@ -25,6 +24,14 @@ class DuplicationRule(ReducedRule):
2524
The second non-terminal on the right of the rule (C here)
2625
"""
2726

27+
def __init__(self,
28+
left_term: Hashable,
29+
right_term0: Hashable,
30+
right_term1: Hashable) -> None:
31+
self._left_term = to_variable(left_term)
32+
self._right_terms = (to_variable(right_term0),
33+
to_variable(right_term1))
34+
2835
@property
2936
def f_parameter(self) -> Terminal:
3037
raise NotImplementedError
@@ -33,18 +40,6 @@ def f_parameter(self) -> Terminal:
3340
def production(self) -> Terminal:
3441
raise NotImplementedError
3542

36-
@property
37-
def right_term(self) -> CFGObject:
38-
raise NotImplementedError
39-
40-
def __init__(self,
41-
left_term: Any,
42-
right_term0: Any,
43-
right_term1: Any) -> None:
44-
self._left_term = to_variable(left_term)
45-
self._right_terms = (to_variable(right_term0),
46-
to_variable(right_term1))
47-
4843
@property
4944
def left_term(self) -> Variable:
5045
"""Gives the non-terminal on the left of the rule
@@ -56,6 +51,10 @@ def left_term(self) -> Variable:
5651
"""
5752
return self._left_term
5853

54+
@property
55+
def right_term(self) -> CFGObject:
56+
raise NotImplementedError
57+
5958
@property
6059
def right_terms(self) -> List[CFGObject]:
6160
"""Gives the non-terminals on the right of the rule
@@ -89,13 +88,13 @@ def terminals(self) -> Set[Terminal]:
8988
"""
9089
return set()
9190

92-
def __repr__(self) -> str:
93-
"""Gives a string representation of the rule, ignoring the sigmas"""
94-
return f"{self._left_term} -> \
95-
{self._right_terms[0]} {self._right_terms[1]}"
96-
9791
def __eq__(self, other: Any) -> bool:
9892
if not isinstance(other, DuplicationRule):
9993
return False
10094
return other.left_term == self._left_term \
10195
and other.right_terms == self.right_terms
96+
97+
def __repr__(self) -> str:
98+
"""Gives a string representation of the rule, ignoring the sigmas"""
99+
return f"{self._left_term} -> \
100+
{self._right_terms[0]} {self._right_terms[1]}"

pyformlang/indexed_grammar/end_rule.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
Represents a end rule, i.e. a rule which give only a terminal
33
"""
44

5-
from typing import List, Set, Any
5+
from typing import List, Set, Hashable, Any
66

7-
from pyformlang.cfg import Variable, Terminal
8-
from pyformlang.cfg.utils import to_variable, to_terminal
9-
from pyformlang.cfg.cfg_object import CFGObject
7+
from pyformlang.cfg import CFGObject, Variable, Terminal
108

119
from .reduced_rule import ReducedRule
10+
from ..objects.cfg_objects.utils import to_variable, to_terminal
1211

1312

1413
class EndRule(ReducedRule):
@@ -23,6 +22,10 @@ class EndRule(ReducedRule):
2322
The terminal on the right, "a" here
2423
"""
2524

25+
def __init__(self, left_term: Hashable, right_term: Hashable) -> None:
26+
self._left_term = to_variable(left_term)
27+
self._right_term = to_terminal(right_term)
28+
2629
@property
2730
def f_parameter(self) -> Terminal:
2831
raise NotImplementedError
@@ -31,10 +34,6 @@ def f_parameter(self) -> Terminal:
3134
def production(self) -> Terminal:
3235
raise NotImplementedError
3336

34-
def __init__(self, left_term: Any, right_term: Any) -> None:
35-
self._left_term = to_variable(left_term)
36-
self._right_term = to_terminal(right_term)
37-
3837
@property
3938
def left_term(self) -> Variable:
4039
"""Gets the non-terminal on the left of the rule
@@ -90,12 +89,12 @@ def terminals(self) -> Set[Terminal]:
9089
"""
9190
return {self._right_term}
9291

93-
def __repr__(self) -> str:
94-
"""Gets the string representation of the rule"""
95-
return f"{self._left_term} -> {self._right_term}"
96-
9792
def __eq__(self, other: Any) -> bool:
9893
if not isinstance(other, EndRule):
9994
return False
10095
return other.left_term == self.left_term \
10196
and other.right_term == self.right_term
97+
98+
def __repr__(self) -> str:
99+
"""Gets the string representation of the rule"""
100+
return f"{self._left_term} -> {self._right_term}"

0 commit comments

Comments
 (0)