Skip to content

Commit

Permalink
Fixed issue #3 (infinite recursion in grammar)
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Mar 9, 2017
1 parent d40ddff commit 24f8656
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lark/parsers/earley.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ def add(self, items):
new_tree = old_tree.copy()
new_tree.rule = old_tree.rule
old_tree.set('ambig', [new_tree])
if item.tree.children[0] is old_tree: # XXX a little hacky!
raise ParseError("Infinite recursion in grammar!")
old_tree.children.append(item.tree)
else:
self.completed[item] = item
Expand Down
10 changes: 10 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ def test_same_ast(self):
l2 = g.parse('(a,b,c,*x)')
assert l == l2, '%s != %s' % (l.pretty(), l2.pretty())

def test_infinite_recurse(self):
g = """start: a
a: a | "a"
"""

self.assertRaises(GrammarError, Lark, g, parser='lalr')

l = Lark(g, parser='earley')
self.assertRaises(ParseError, l.parse, 'a')


class TestEarley(unittest.TestCase):
def test_anon_in_scanless(self):
Expand Down

0 comments on commit 24f8656

Please sign in to comment.