11""" A transition function in a pushdown automaton """
22
3- from typing import List
3+ from typing import Dict , List , Set , Iterator , Tuple , Optional
44
55from .stack_symbol import StackSymbol
66from .state import State
1010class TransitionFunction :
1111 """ A transition function in a pushdown automaton """
1212
13- def __init__ (self ):
14- self ._transitions = {}
15- self ._iter_key = None
16- self ._current_key = None
17- self ._iter_inside = None
13+ def __init__ (self ) -> None :
14+ self ._transitions : Dict [Tuple [State , Symbol , StackSymbol ],
15+ Set [Tuple [State , List [StackSymbol ]]]] = {}
16+ self ._iter_key : Optional [Iterator [
17+ Tuple [State , Symbol , StackSymbol ]]] = None
18+ self ._current_key : Optional [
19+ Tuple [State , Symbol , StackSymbol ]] = None
20+ self ._iter_inside : Optional [Iterator [
21+ Tuple [State , List [StackSymbol ]]]] = None
1822
19- def get_number_transitions (self ):
23+ def get_number_transitions (self ) -> int :
2024 """ Gets the number of transitions
2125
2226 Returns
@@ -32,7 +36,7 @@ def add_transition(self,
3236 input_symbol : Symbol ,
3337 stack_from : StackSymbol ,
3438 s_to : State ,
35- stack_to : List [StackSymbol ]):
39+ stack_to : List [StackSymbol ]) -> None :
3640 """ Add a transition to the function
3741
3842 Parameters
@@ -49,7 +53,7 @@ def add_transition(self,
4953 The string of stack symbol which replace the stack_from
5054 """
5155 temp_in = (s_from , input_symbol , stack_from )
52- temp_out = (s_to , tuple ( stack_to ))
56+ temp_out = (s_to , stack_to . copy ( ))
5357 if temp_in in self ._transitions :
5458 self ._transitions [temp_in ].add (temp_out )
5559 else :
@@ -70,31 +74,35 @@ def copy(self) -> "TransitionFunction":
7074 temp_out [0 ], temp_out [1 ])
7175 return new_tf
7276
73- def __iter__ (self ):
77+ def __iter__ (self ) -> Iterator [Tuple [Tuple [State , Symbol , StackSymbol ],
78+ Tuple [State , List [StackSymbol ]]]]:
7479 self ._iter_key = iter (self ._transitions .keys ())
7580 self ._current_key = None
7681 self ._iter_inside = None
7782 return self
7883
79- def __next__ (self ):
84+ def __next__ (self ) -> Tuple [Tuple [State , Symbol , StackSymbol ],
85+ Tuple [State , List [StackSymbol ]]]:
8086 if self ._iter_inside is None :
81- next_key = next (self ._iter_key )
87+ next_key = next (self ._iter_key ) # type: ignore
8288 self ._current_key = next_key
8389 self ._iter_inside = iter (self ._transitions [next_key ])
8490 try :
8591 next_value = next (self ._iter_inside )
86- return self ._current_key , next_value
92+ return self ._current_key , next_value # type: ignore
8793 except StopIteration :
88- next_key = next (self ._iter_key )
94+ next_key = next (self ._iter_key ) # type: ignore
8995 self ._current_key = next_key
9096 self ._iter_inside = iter (self ._transitions [next_key ])
9197 return next (self )
9298
9399 def __call__ (self , s_from : State ,
94100 input_symbol : Symbol ,
95- stack_from : StackSymbol ):
96- return self ._transitions .get ((s_from , input_symbol , stack_from ), {})
101+ stack_from : StackSymbol ) \
102+ -> Set [Tuple [State , List [StackSymbol ]]]:
103+ return self ._transitions .get ((s_from , input_symbol , stack_from ), set ())
97104
98- def to_dict (self ):
105+ def to_dict (self ) -> Dict [Tuple [State , Symbol , StackSymbol ],
106+ Set [Tuple [State , List [StackSymbol ]]]]:
99107 """Get the dictionary representation of the transitions"""
100108 return self ._transitions
0 commit comments