22
33from typing import Dict , List , AbstractSet , Tuple , Optional , Any
44
5- from pyformlang .finite_automaton import State
65from pyformlang .cfg import Variable
76
87class CFGVariableConverter :
98 """A CFG Variable Converter"""
109
1110 def __init__ (self ,
12- states : AbstractSet [State ],
13- stack_symbols : AbstractSet [Variable ]) -> None :
11+ states : AbstractSet [Any ],
12+ stack_symbols : AbstractSet [Any ]) -> None :
1413 self ._counter = 0
15- self ._inverse_states_d : Dict [State , int ] = {}
14+ self ._inverse_states_d : Dict [Any , int ] = {}
1615 self ._counter_state = 0
1716 for self ._counter_state , state in enumerate (states ):
1817 self ._inverse_states_d [state ] = self ._counter_state
1918 state .index_cfg_converter = self ._counter_state
2019 self ._counter_state += 1
21- self ._inverse_stack_symbol_d = {}
20+ self ._inverse_stack_symbol_d : Dict [ Any , int ] = {}
2221 self ._counter_symbol = 0
2322 for self ._counter_symbol , symbol in enumerate (stack_symbols ):
2423 self ._inverse_stack_symbol_d [symbol ] = self ._counter_symbol
@@ -29,7 +28,7 @@ def __init__(self,
2928 for _ in range (len (stack_symbols ))] for _ in
3029 range (len (states ))]
3130
32- def _get_state_index (self , state : State ) -> int :
31+ def _get_state_index (self , state : Any ) -> int :
3332 """Get the state index"""
3433 if state .index_cfg_converter is None :
3534 if state not in self ._inverse_states_d :
@@ -38,7 +37,7 @@ def _get_state_index(self, state: State) -> int:
3837 state .index_cfg_converter = self ._inverse_states_d [state ]
3938 return state .index_cfg_converter
4039
41- def _get_symbol_index (self , symbol : Variable ) -> int :
40+ def _get_symbol_index (self , symbol : Any ) -> int :
4241 """Get the symbol index"""
4342 if symbol .index_cfg_converter is None :
4443 if symbol not in self ._inverse_stack_symbol_d :
@@ -48,9 +47,9 @@ def _get_symbol_index(self, symbol: Variable) -> int:
4847 return symbol .index_cfg_converter
4948
5049 def to_cfg_combined_variable (self ,
51- state0 : State ,
52- stack_symbol : Variable ,
53- state1 : State ) -> Variable :
50+ state0 : Any ,
51+ stack_symbol : Any ,
52+ state1 : Any ) -> Variable :
5453 """ Conversion used in the to_pda method """
5554 i_stack_symbol , i_state0 , i_state1 = self ._get_indexes (
5655 stack_symbol , state0 , state1 )
@@ -75,19 +74,19 @@ def _create_new_variable(self,
7574 return temp
7675
7776 def set_valid (self ,
78- state0 : State ,
79- stack_symbol : Variable ,
80- state1 : State ) -> None :
77+ state0 : Any ,
78+ stack_symbol : Any ,
79+ state1 : Any ) -> None :
8180 """Set valid"""
8281 i_stack_symbol , i_state0 , i_state1 = self ._get_indexes (
8382 stack_symbol , state0 , state1 )
8483 prev = self ._conversions [i_state0 ][i_stack_symbol ][i_state1 ]
8584 self ._conversions [i_state0 ][i_stack_symbol ][i_state1 ] = (True , prev [1 ])
8685
8786 def is_valid_and_get (self ,
88- state0 : State ,
89- stack_symbol : Variable ,
90- state1 : State ) -> Optional [Variable ]:
87+ state0 : Any ,
88+ stack_symbol : Any ,
89+ state1 : Any ) -> Optional [Variable ]:
9190 """Check if valid and get"""
9291 i_state0 = self ._get_state_index (state0 )
9392 i_stack_symbol = self ._get_symbol_index (stack_symbol )
@@ -103,9 +102,9 @@ def is_valid_and_get(self,
103102 return current [1 ]
104103
105104 def _get_indexes (self ,
106- stack_symbol : Variable ,
107- state0 : State ,
108- state1 : State ) \
105+ stack_symbol : Any ,
106+ state0 : Any ,
107+ state1 : Any ) \
109108 -> Tuple [int , int , int ]:
110109 i_state0 = self ._get_state_index (state0 )
111110 i_stack_symbol = self ._get_symbol_index (stack_symbol )
0 commit comments