Skip to content

Commit

Permalink
Merge pull request #1435 from lark-parser/issue1434
Browse files Browse the repository at this point in the history
Bugfix for issue #1434
  • Loading branch information
erezsh committed Jun 30, 2024
2 parents 5016894 + 7547758 commit be542c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lark/parsers/grammar_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections import Counter, defaultdict
from typing import List, Dict, Iterator, FrozenSet, Set

from ..utils import bfs, fzset, classify
from ..utils import bfs, fzset, classify, OrderedSet
from ..exceptions import GrammarError
from ..grammar import Rule, Terminal, NonTerminal, Symbol
from ..common import ParserConf
Expand Down Expand Up @@ -177,13 +177,13 @@ def __init__(self, parser_conf: ParserConf, debug: bool=False, strict: bool=Fals

self.FIRST, self.FOLLOW, self.NULLABLE = calculate_sets(rules)

def expand_rule(self, source_rule: NonTerminal, rules_by_origin=None) -> State:
def expand_rule(self, source_rule: NonTerminal, rules_by_origin=None) -> OrderedSet[RulePtr]:
"Returns all init_ptrs accessible by rule (recursive)"

if rules_by_origin is None:
rules_by_origin = self.rules_by_origin

init_ptrs = set()
init_ptrs = OrderedSet[RulePtr]()
def _expand_rule(rule: NonTerminal) -> Iterator[NonTerminal]:
assert not rule.is_term, rule

Expand All @@ -200,4 +200,4 @@ def _expand_rule(rule: NonTerminal) -> Iterator[NonTerminal]:
for _ in bfs([source_rule], _expand_rule):
pass

return fzset(init_ptrs)
return init_ptrs
3 changes: 3 additions & 0 deletions lark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,6 @@ def __bool__(self):

def __len__(self) -> int:
return len(self.d)

def __repr__(self):
return f"{type(self).__name__}({', '.join(map(repr,self))})"
13 changes: 13 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,19 @@ def test_resolve_ambiguity_with_shared_node2(self):
tree = l.parse("")
self.assertEqual(tree, Tree('start', [Tree('x', [])]))


def test_consistent_derivation_order1(self):
# Should return the same result for any hash-seed
parser = Lark('''
start: a a
a: "." | b
b: "."
''', lexer=LEXER)

tree = parser.parse('..')
n = Tree('a', [Tree('b', [])])
assert tree == Tree('start', [n, n])

_NAME = "TestFullEarley" + LEXER.capitalize()
_TestFullEarley.__name__ = _NAME
globals()[_NAME] = _TestFullEarley
Expand Down

0 comments on commit be542c2

Please sign in to comment.