Skip to content

Commit e7048a7

Browse files
committed
update tests, correct transition function of pda, correct CFGConvertible interface
1 parent 93c3a35 commit e7048a7

File tree

9 files changed

+22
-30
lines changed

9 files changed

+22
-30
lines changed

pyformlang/cfg/tests/test_llone_parser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ def test_get_follow_set2(self):
8888
cfg = CFG.from_text(text)
8989
llone_parser = LLOneParser(cfg)
9090
follow_set = llone_parser.get_follow_set()
91-
assert follow_set["S"] == \
91+
assert follow_set[Variable("S")] == \
9292
{"$"}
93-
assert follow_set["A"] == \
93+
assert follow_set[Variable("A")] == \
9494
{"$", Terminal("h"), Terminal("g")}
95-
assert follow_set["B"] == \
95+
assert follow_set[Variable("B")] == \
9696
{"$", Terminal("h"), Terminal("g"), Terminal("a")}
97-
assert follow_set["C"] == \
97+
assert follow_set[Variable("C")] == \
9898
{"$", Terminal("h"), Terminal("g"), Terminal("b")}
9999

100100
def test_get_llone_table(self):

pyformlang/fcfg/fcfg.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"""Feature Context-Free Grammar"""
22
import string
3-
from typing import Iterable, AbstractSet, Union
4-
5-
from objects.cfg_objects.utils import to_terminal
3+
from typing import Iterable, AbstractSet, Hashable
64

75
from pyformlang.cfg import CFG, Terminal, Epsilon, Variable, ParseTree
86
from pyformlang.cfg.cfg import is_special_text, EPSILON_SYMBOLS, NotParsableException
97
from pyformlang.fcfg.feature_production import FeatureProduction
108
from pyformlang.fcfg.feature_structure import FeatureStructure, FeatureStructuresNotCompatibleException
119
from pyformlang.fcfg.state import State, StateProcessed
1210

11+
from ..objects.cfg_objects.utils import to_terminal
12+
1313

1414
class FCFG(CFG):
1515
""" A class representing a feature context-free grammar
@@ -73,7 +73,7 @@ def __predictor(self, state, chart, processed):
7373
if processed.add(end_idx, new_state):
7474
chart[end_idx].append(new_state)
7575

76-
def contains(self, word: Iterable[Union[Terminal, str]]) -> bool:
76+
def contains(self, word: Iterable[Hashable]) -> bool:
7777
""" Gives the membership of a word to the grammar
7878
7979
Parameters
@@ -88,7 +88,7 @@ def contains(self, word: Iterable[Union[Terminal, str]]) -> bool:
8888
"""
8989
return self._get_final_state(word) is not None
9090

91-
def get_parse_tree(self, word: Iterable[Union[Terminal, str]]) -> ParseTree:
91+
def get_parse_tree(self, word: Iterable[Hashable]) -> ParseTree:
9292
""" Gives the parse tree for a sentence, if possible
9393
9494
Parameters
@@ -111,7 +111,7 @@ def get_parse_tree(self, word: Iterable[Union[Terminal, str]]) -> ParseTree:
111111
raise NotParsableException()
112112
return final_state.parse_tree
113113

114-
def _get_final_state(self, word: Iterable[Terminal]):
114+
def _get_final_state(self, word: Iterable[Hashable]):
115115
word = [to_terminal(x) for x in word if x != Epsilon()]
116116
chart = [[] for _ in range(len(word) + 1)]
117117
# Processed[i] contains all production rule that are currently working until i.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
""" Interface representing the ability of conversion to cfg object """
22

3-
from typing import Optional
3+
from typing import Optional, Any
44

55

66
class CFGConvertible:
77
""" Interface representing the ability of conversion to cfg object """
88

9-
def __init__(self) -> None:
9+
def __init__(self, *args: Any, **kwargs: Any) -> None:
10+
super().__init__(*args, **kwargs)
1011
self.index_cfg_converter: Optional[int] = None

pyformlang/objects/finite_automaton_objects/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ..cfg_objects import CFGConvertible
99

1010

11-
class State(FiniteAutomatonObject, CFGConvertible):
11+
class State(CFGConvertible, FiniteAutomatonObject):
1212
""" A state in a finite automaton
1313
1414
Parameters

pyformlang/objects/pda_objects/stack_symbol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..cfg_objects import CFGConvertible
77

88

9-
class StackSymbol(Symbol, CFGConvertible):
9+
class StackSymbol(CFGConvertible, Symbol):
1010
""" A StackSymbol in a pushdown automaton
1111
1212
Parameters

pyformlang/objects/pda_objects/state.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ..cfg_objects import CFGConvertible
77

88

9-
class State(PDAObject, CFGConvertible):
9+
class State(CFGConvertible, PDAObject):
1010
""" A State in a pushdown automaton
1111
1212
Parameters

pyformlang/pda/tests/test_pda.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from pyformlang.cfg import Terminal
66
from pyformlang.finite_automaton import DeterministicFiniteAutomaton
77
from pyformlang.finite_automaton import State as FAState, Symbol as FASymbol
8-
from pyformlang.pda.utils import PDAObjectCreator
98
from pyformlang.regular_expression import Regex
109

1110

@@ -326,11 +325,6 @@ def test_intersection_regex(self):
326325
cfg = pda_es.to_cfg()
327326
assert not cfg
328327

329-
def test_pda_object_creator_epsilon(self):
330-
""" Test creation objects """
331-
poc = PDAObjectCreator()
332-
assert poc.to_stack_symbol(Epsilon()) == Epsilon()
333-
334328
def test_pda_paper(self):
335329
""" Code in the paper """
336330
pda = PDA()

pyformlang/pda/transition_function.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
""" A transition function in a pushdown automaton """
22

33
from copy import deepcopy
4-
from typing import Dict, List, Set, Iterator, Iterable, Tuple, Optional
4+
from typing import Dict, Set, Sequence, Iterator, Iterable, Tuple
55

66
from ..objects.pda_objects import State, Symbol, StackSymbol
77

88
TransitionKey = Tuple[State, Symbol, StackSymbol]
9-
TransitionValue = Tuple[State, List[StackSymbol]]
9+
TransitionValue = Tuple[State, Tuple[StackSymbol, ...]]
1010
TransitionValues = Set[TransitionValue]
1111
Transition = Tuple[TransitionKey, TransitionValue]
1212

@@ -16,9 +16,6 @@ class TransitionFunction(Iterable[Transition]):
1616

1717
def __init__(self) -> None:
1818
self._transitions: Dict[TransitionKey, TransitionValues] = {}
19-
self._current_key: Optional[TransitionKey] = None
20-
self._iter_key: Optional[Iterator[TransitionKey]] = None
21-
self._iter_inside: Optional[Iterator[TransitionValue]] = None
2219

2320
def get_number_transitions(self) -> int:
2421
""" Gets the number of transitions
@@ -36,7 +33,7 @@ def add_transition(self,
3633
input_symbol: Symbol,
3734
stack_from: StackSymbol,
3835
s_to: State,
39-
stack_to: List[StackSymbol]) -> None:
36+
stack_to: Sequence[StackSymbol]) -> None:
4037
""" Add a transition to the function
4138
4239
Parameters
@@ -53,7 +50,7 @@ def add_transition(self,
5350
The string of stack symbol which replace the stack_from
5451
"""
5552
temp_in = (s_from, input_symbol, stack_from)
56-
temp_out = (s_to, stack_to.copy())
53+
temp_out = (s_to, tuple(stack_to))
5754
if temp_in in self._transitions:
5855
self._transitions[temp_in].add(temp_out)
5956
else:
@@ -71,7 +68,7 @@ def copy(self) -> "TransitionFunction":
7168
for temp_in, transition in self._transitions.items():
7269
for temp_out in transition:
7370
new_tf.add_transition(temp_in[0], temp_in[1], temp_in[2],
74-
temp_out[0], temp_out[1])
71+
*temp_out)
7572
return new_tf
7673

7774
def __iter__(self) -> Iterator[Transition]:

pyformlang/rsa/tests/test_rsa.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
""" Tests for RSA """
2-
from pyformlang.finite_automaton.symbol import Symbol
2+
from pyformlang.finite_automaton import Symbol
33
from pyformlang.regular_expression import Regex
44

55
from pyformlang.rsa.recursive_automaton import RecursiveAutomaton

0 commit comments

Comments
 (0)