22A 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
119from .reduced_rule import ReducedRule
10+ from ..objects .cfg_objects .utils import to_variable
1211
1312
1413class 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 ]} "
0 commit comments