One function

ForestToParseTree in lark-parser/lark

The author described this change as Fix an SPPF cycle edge case (Issue #820). It counts as a record because the check below fails on the code as it stood at 9379161aa and passes on e88646b58, with nothing else changed between the two runs.

Fix saved2021-01-26
Sharing licenceMIT · LICENSE
Change size+3 3

What the code was meant to do, written into the code itself as a docstring

Used by the earley parser when ambiguity equals 'resolve' or 'explicit'. Transforms an SPPF into an (ambiguous) parse tree. tree_class: The tree class to use for construction callbacks: A dictionary of rules to functions that output a tree prioritizer: A `ForestVisitor` that manipulates the priorities of ForestNodes resolve_ambiguity: If True, ambiguities will be resolved based on priorities. Otherwise, `_ambig` nodes will be in the resulting tree.

The change

3030
3131 def _check_cycle(self, node):
3232 if self._on_cycle_retreat:
33- if id(node) == id(self._cycle_node):
33+ if id(node) == id(self._cycle_node) or id(node) in self._successful_visits:
3434 self._cycle_node = None
3535 self._on_cycle_retreat = False
3636 return
6464 def transform_symbol_node(self, node, data):
6565 if id(node) not in self._successful_visits:
6666 raise Discard()
67- self._successful_visits.remove(id(node))
6867 self._check_cycle(node)
68+ self._successful_visits.remove(id(node))
6969 data = self._collapse_ambig(data)
7070 return self._call_ambig_func(node, data)
7171
7272 def transform_intermediate_node(self, node, data):
7373 if id(node) not in self._successful_visits:
7474 raise Discard()
75- self._successful_visits.remove(id(node))
7675 self._check_cycle(node)
76+ self._successful_visits.remove(id(node))
7777 if len(data) > 1:
7878 children = [self.tree_class('_inter', c) for c in data]
7979 return self.tree_class('_iambig', children)

The check that tells the two apart

failpass·tests/test_parser.py::TestFullEarleyDynamic::test_cycle2

Check file tests/test_parser.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.

Origin and history

The code before it9379161aad48994302251803ef7e932cbbd3c4ba
Broken version dated2021-01-25
Modulelark.parsers.earley_forest
Units changedForestToParseTree
Fingerprint9e8bc9f7c5f9889b
Checked2026-08-18 by goldset/0.1

Every field above is generated by our program. None of it is written by hand.

Other bugs found in lark-parser/lark