Skip to content

Commit f24329e

Browse files
committed
add start symbol setter for cfg
1 parent e7c5398 commit f24329e

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

pyformlang/cfg/cfg.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
from .parse_tree import ParseTree
1414
from .cyk_table import CYKTable
1515
from .cfg_variable_converter import CFGVariableConverter
16-
from .utils import remove_nullable_production, get_productions_d
16+
from .utils import remove_nullable_production, get_productions_d, \
17+
is_special_text
1718
from ..objects.cfg_objects import CFGObject, \
1819
Variable, Terminal, Epsilon, Production
1920
from ..objects.cfg_objects.utils import to_variable, to_terminal
@@ -23,13 +24,6 @@
2324
SUBS_SUFFIX = "#SUBS#"
2425

2526

26-
def is_special_text(text: str) -> bool:
27-
""" Check if the input is given an explicit type """
28-
return len(text) > 5 and \
29-
(text[0:5] == '"VAR:' or text[0:5] == '"TER:') and \
30-
text[-1] == '"'
31-
32-
3327
class CFG(FormalGrammar):
3428
""" A class representing a context free grammar
3529

pyformlang/cfg/formal_grammar.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from abc import abstractmethod
55

66
from ..objects.cfg_objects import Variable, Terminal, Production
7+
from ..objects.cfg_objects.utils import to_variable
78

89
GrammarT = TypeVar("GrammarT", bound="FormalGrammar")
910

@@ -66,6 +67,12 @@ def start_symbol(self) -> Optional[Variable]:
6667
"""
6768
return self._start_symbol
6869

70+
@start_symbol.setter
71+
def start_symbol(self, symbol: Hashable) -> None:
72+
""" Sets the start symbol of the grammar """
73+
self._start_symbol = to_variable(symbol) \
74+
if symbol is not None else None
75+
6976
@abstractmethod
7077
def copy(self: GrammarT) -> GrammarT:
7178
""" Copies the grammar """

pyformlang/cfg/utils.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
from ..objects.cfg_objects import CFGObject, Variable, Epsilon, Production
66

77

8+
def is_special_text(text: str) -> bool:
9+
""" Check if the input is given an explicit type """
10+
return len(text) > 5 and \
11+
(text[0:5] == '"VAR:' or text[0:5] == '"TER:') and \
12+
text[-1] == '"'
13+
14+
815
def remove_nullable_production_sub(body: List[CFGObject],
916
nullables: AbstractSet[CFGObject]) \
1017
-> List[List[CFGObject]]:

0 commit comments

Comments
 (0)