Skip to content

Commit 63ca621

Browse files
committed
finish pda annotation
1 parent dbb3a22 commit 63ca621

File tree

4 files changed

+153
-84
lines changed

4 files changed

+153
-84
lines changed

pyformlang/cfg/cfg.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def _intersection_starting_rules(cfg: "CFG",
859859
new_body: List[CFGObject] = [
860860
cv_converter.to_cfg_combined_variable(
861861
start_other,
862-
cfg.start_symbol, # type: ignore
862+
cfg.start_symbol,
863863
final_state)]
864864
productions_temp.append(
865865
Production(start, new_body, filtering=False))
@@ -900,7 +900,7 @@ def _intersection_when_two_non_terminals(
900900
cv_converter.to_cfg_combined_variable(
901901
state_p, production.head, state_r)
902902
productions_temp += [Production(new_head,
903-
body, # type: ignore
903+
body,
904904
filtering=False)
905905
for body in bodies]
906906
return productions_temp
@@ -911,15 +911,15 @@ def _get_all_bodies(production: Production,
911911
state_r: FAState,
912912
states: Iterable[FAState],
913913
cv_converter: CFGVariableConverter) \
914-
-> List[List[Variable]]:
914+
-> List[List[CFGObject]]:
915915
return [
916916
[cv_converter.to_cfg_combined_variable(
917917
state_p,
918-
production.body[0], # type: ignore
918+
production.body[0],
919919
state_q),
920920
cv_converter.to_cfg_combined_variable(
921921
state_q,
922-
production.body[1], # type: ignore
922+
production.body[1],
923923
state_r)]
924924
for state_q in states]
925925

pyformlang/finite_automaton/finite_automaton.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def to_networkx(self) -> MultiDiGraph:
507507
peripheries=2 if state in self.final_states else 1,
508508
label=state.value)
509509
if state in self.start_states:
510-
add_start_state_to_graph(graph, state)
510+
__add_start_state_to_graph(graph, state)
511511
for s_from, symbol, s_to in self._transition_function.get_edges():
512512
label_ = symbol.value
513513
if label_ == 'epsilon':
@@ -743,7 +743,7 @@ def to_symbol(given: Any) -> Symbol:
743743
return Symbol(given)
744744

745745

746-
def add_start_state_to_graph(graph: MultiDiGraph, state: State) -> None:
746+
def __add_start_state_to_graph(graph: MultiDiGraph, state: State) -> None:
747747
""" Adds a starting node to a given graph """
748748
graph.add_node("starting_" + str(state.value),
749749
label="",

pyformlang/pda/cfg_variable_converter.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@
22

33
from typing import Dict, List, AbstractSet, Tuple, Optional, Any
44

5-
from pyformlang.finite_automaton import State
65
from pyformlang.cfg import Variable
76

87
class 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

Comments
 (0)