From 39fb4c0f3e2c1c24ceeb4de29d6904a957eaaaf1 Mon Sep 17 00:00:00 2001 From: Erez Sh Date: Fri, 14 Aug 2020 16:34:51 +0300 Subject: [PATCH] Bugfix and warn on ambiguous intermediate nodes, based on PR #651 --- lark/parsers/earley_forest.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lark/parsers/earley_forest.py b/lark/parsers/earley_forest.py index c8b4f25..4ed75d9 100644 --- a/lark/parsers/earley_forest.py +++ b/lark/parsers/earley_forest.py @@ -13,6 +13,7 @@ from collections import deque from operator import attrgetter from importlib import import_module +from ..utils import logger from ..tree import Tree from ..exceptions import ParseError @@ -328,10 +329,17 @@ class ForestToAmbiguousTreeVisitor(ForestToTreeVisitor): self.output_stack[-1].children.append(node) def visit_symbol_node_in(self, node): - if self.forest_sum_visitor and node.is_ambiguous and isinf(node.priority): - self.forest_sum_visitor.visit(node) - if not node.is_intermediate and node.is_ambiguous: - self.output_stack.append(Tree('_ambig', [])) + if node.is_ambiguous: + if self.forest_sum_visitor and isinf(node.priority): + self.forest_sum_visitor.visit(node) + if node.is_intermediate: + # TODO Support ambiguous intermediate nodes! + logger.warning("Ambiguous intermediate node in the SPPF: %s. " + "Lark does not currently process these ambiguities; resolving with the first derivation.", node) + return next(iter(node.children)) + else: + self.output_stack.append(Tree('_ambig', [])) + return iter(node.children) def visit_symbol_node_out(self, node):