Skip to content

Commit 987a5a2

Browse files
authored
Merge pull request #32 from nielstron/fix/cfg_ter_notation
Fix #31
2 parents 04d769a + ffa9eaa commit 987a5a2

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

pyformlang/cfg/cfg.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,16 +1081,18 @@ def _read_line(cls, line, productions, terminals, variables):
10811081
body_component = body_component[5:-1]
10821082
else:
10831083
type_component = ""
1084-
if body_component[0] in string.ascii_uppercase or \
1085-
type_component == "VAR":
1086-
body_var = Variable(body_component)
1087-
variables.add(body_var)
1088-
body.append(body_var)
1089-
elif body_component not in EPSILON_SYMBOLS or type_component \
1090-
== "TER":
1084+
if type_component != "VAR" and (not body_component[0].isupper() or type_component == "TER" or body_component in EPSILON_SYMBOLS):
1085+
if body_component in EPSILON_SYMBOLS:
1086+
continue
10911087
body_ter = Terminal(body_component)
10921088
terminals.add(body_ter)
10931089
body.append(body_ter)
1090+
elif type_component != "TER" and (body_component[0].isupper() or type_component == "VAR"):
1091+
body_var = Variable(body_component)
1092+
variables.add(body_var)
1093+
body.append(body_var)
1094+
else:
1095+
raise ValueError(f"Invalid rule definition: {body_component}")
10941096
productions.add(Production(head, body))
10951097

10961098
def is_normal_form(self):

pyformlang/cfg/tests/test_cfg.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,16 @@ def test_from_text2(self):
769769
assert cfg.contains(["a", "b"])
770770
assert ["a", "b"] in cfg
771771

772+
def test_from_text3(self):
773+
text = """
774+
S -> "TER:A" "TER:B" "VAR:a"
775+
"VAR:a" -> "TER:A" | $
776+
"""
777+
cfg = CFG.from_text(text)
778+
assert cfg.contains(["A", "B"])
779+
assert cfg.contains(["A", "B", "A"])
780+
781+
772782
def test_from_text_union(self):
773783
text = """
774784
"VAR:S" -> TER:a | b

0 commit comments

Comments
 (0)