-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #37 Add greedy variants of +, *, and ?
- Loading branch information
1 parent
5f94f1f
commit 517d33e
Showing
3 changed files
with
201 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import pytest | ||
from parglare import GLRParser, Grammar | ||
|
||
|
||
def test_greedy_zero_or_more(): | ||
""" | ||
Test greedy variant of zero or more. | ||
""" | ||
grammar = r""" | ||
S: A* A*; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 7 | ||
|
||
# But greedy variant has only one solution where first A*! collects all tokens. | ||
grammar = r""" | ||
S: A*! A*; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 1 | ||
|
||
|
||
def test_greedy_zero_or_more_complex(): | ||
""" | ||
Test greedy variant of zero or more for complex subexpression. | ||
""" | ||
grammar = r""" | ||
S: ("a" | "b" "c")* "a"*; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a b c a b c a a a") | ||
assert len(forest) == 4 | ||
|
||
grammar = r""" | ||
S: ("a" | "b" "c")*! "a"*; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a b c a b c a a a") | ||
assert len(forest) == 1 | ||
|
||
|
||
def test_greedy_one_or_more(): | ||
""" | ||
Test greedy variant of one or more. | ||
""" | ||
grammar = r""" | ||
S: A+ A*; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 6 | ||
|
||
# But greedy variant has only one solution where first A+! collects all tokens. | ||
grammar = r""" | ||
S: A+! A*; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 1 | ||
|
||
|
||
def test_greedy_optional(): | ||
""" | ||
Test greedy variant of one or more. | ||
""" | ||
grammar = r""" | ||
S: A? A+; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 2 | ||
|
||
# But greedy variant has only one solution where first A?! is non-empty if possible | ||
grammar = r""" | ||
S: A?! A*; | ||
terminals | ||
A: "a"; | ||
""" | ||
|
||
g = Grammar.from_string(grammar) | ||
p = GLRParser(g) | ||
forest = p.parse("a a a a a a") | ||
assert len(forest) == 1 |