Parcourir la source

Add debug attribute to ForestToParseTree

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.11.0
Chanic Panic il y a 3 ans
Parent
révision
e3c7564fa0
2 fichiers modifiés avec 7 ajouts et 5 suppressions
  1. +1
    -1
      lark/parsers/earley.py
  2. +6
    -4
      lark/parsers/earley_forest.py

+ 1
- 1
lark/parsers/earley.py Voir le fichier

@@ -315,7 +315,7 @@ class Parser:

if self.tree_class is not None:
# Perform our SPPF -> AST conversion
transformer = ForestToParseTree(self.tree_class, self.callbacks, self.forest_sum_visitor and self.forest_sum_visitor(), self.resolve_ambiguity)
transformer = ForestToParseTree(self.tree_class, self.callbacks, self.forest_sum_visitor and self.forest_sum_visitor(), self.resolve_ambiguity, self.debug)
return transformer.transform(solutions[0])

# return the root of the SPPF


+ 6
- 4
lark/parsers/earley_forest.py Voir le fichier

@@ -483,20 +483,22 @@ class ForestToParseTree(ForestTransformer):
tree.
"""

def __init__(self, tree_class=Tree, callbacks=dict(), prioritizer=ForestSumVisitor(), resolve_ambiguity=True):
def __init__(self, tree_class=Tree, callbacks=dict(), prioritizer=ForestSumVisitor(), resolve_ambiguity=True, debug=False):
super(ForestToParseTree, self).__init__()
self.tree_class = tree_class
self.callbacks = callbacks
self.prioritizer = prioritizer
self.resolve_ambiguity = resolve_ambiguity
self.debug = debug
self._on_cycle_retreat = False
self._cycle_node = None
self._successful_visits = set()

def on_cycle(self, node, path):
logger.warning("Cycle encountered in the SPPF at node: %s. "
"As infinite ambiguities cannot be represented in a tree, "
"this family of derivations will be discarded.", node)
if self.debug:
logger.warning("Cycle encountered in the SPPF at node: %s. "
"As infinite ambiguities cannot be represented in a tree, "
"this family of derivations will be discarded.", node)
self._cycle_node = node
self._on_cycle_retreat = True



Chargement…
Annuler
Enregistrer