Browse Source

Fixed issue #3 (infinite recursion in grammar)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.5.1
Erez Shinan 8 years ago
parent
commit
24f86569da
2 changed files with 12 additions and 0 deletions
  1. +2
    -0
      lark/parsers/earley.py
  2. +10
    -0
      tests/test_parser.py

+ 2
- 0
lark/parsers/earley.py View File

@@ -105,6 +105,8 @@ class Column:
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


+ 10
- 0
tests/test_parser.py View File

@@ -39,6 +39,16 @@ class TestParsers(unittest.TestCase):
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):


Loading…
Cancel
Save