Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lark/load_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,9 @@ def compile(self, start, terminals_to_keep) -> Tuple[List[TerminalDef], List[Rul
else:
exp_options = options

if len(expansion) == 1 and expansion[0] == NonTerminal(name):
continue

for sym in expansion:
assert isinstance(sym, Symbol)
if sym.is_term and exp_options and exp_options.keep_all_tokens:
Expand Down
5 changes: 5 additions & 0 deletions tests/test_grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ def test_symbol_eq(self):

self.assertNotEqual(a, b)

def test_issue_1585_cyk(self):
l = Lark('start.1: "a" | start start*', parser='cyk')
for s in ('aa', 'aaaa'):
self.assertEqual(l.parse(s).data, 'start')


if __name__ == '__main__':
main()
17 changes: 13 additions & 4 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ def test_infinite_recurse(self):
a: a | "a"
"""

self.assertRaises(GrammarError, Lark, g, parser='lalr')
for parser in ('lalr', 'earley'):
l = Lark(g, parser=parser)
tree = l.parse('a')
self.assertEqual(tree, Tree('start', [Tree('a', [])]))

# TODO: should it? shouldn't it?
# l = Lark(g, parser='earley', lexer='dynamic')
# self.assertRaises(ParseError, l.parse, 'a')
g = """start: a
a: a
"""
self.assertRaises(GrammarError, Lark, g, parser='lalr')

def test_propagate_positions(self):
g = Lark("""start: a
Expand Down Expand Up @@ -2256,6 +2260,11 @@ def test_prioritization_sum(self):
self.assertEqual(''.join(child.data for child in res.children), 'indirection')


def test_issue_1585_priority_recursive_star(self):
l = _Lark('start.1: "a" | start start*')
for s in ('a', 'aa', 'aaaa'):
self.assertEqual(l.parse(s).data, 'start')

def test_utf8(self):
g = u"""start: a
a: "±a"
Expand Down
Loading