@@ -139,7 +139,8 @@ class Earley(WithLexer): | |||||
self.init_traditional_lexer() | self.init_traditional_lexer() | ||||
resolve_ambiguity = options.ambiguity == 'resolve' | resolve_ambiguity = options.ambiguity == 'resolve' | ||||
self.parser = earley.Parser(parser_conf, self.match, resolve_ambiguity=resolve_ambiguity) | |||||
debug = options.debug if options else False | |||||
self.parser = earley.Parser(parser_conf, self.match, resolve_ambiguity=resolve_ambiguity, debug=debug) | |||||
def match(self, term, token): | def match(self, term, token): | ||||
return term.name == token.type | return term.name == token.type | ||||
@@ -152,10 +153,12 @@ class XEarley(_ParserFrontend): | |||||
self._prepare_match(lexer_conf) | self._prepare_match(lexer_conf) | ||||
resolve_ambiguity = options.ambiguity == 'resolve' | resolve_ambiguity = options.ambiguity == 'resolve' | ||||
debug = options.debug if options else False | |||||
self.parser = xearley.Parser(parser_conf, | self.parser = xearley.Parser(parser_conf, | ||||
self.match, | self.match, | ||||
ignore=lexer_conf.ignore, | ignore=lexer_conf.ignore, | ||||
resolve_ambiguity=resolve_ambiguity, | resolve_ambiguity=resolve_ambiguity, | ||||
debug=debug, | |||||
**kw | **kw | ||||
) | ) | ||||
@@ -20,10 +20,11 @@ from .earley_common import Item, TransitiveItem | |||||
from .earley_forest import ForestToTreeVisitor, ForestSumVisitor, SymbolNode, ForestToAmbiguousTreeVisitor | from .earley_forest import ForestToTreeVisitor, ForestSumVisitor, SymbolNode, ForestToAmbiguousTreeVisitor | ||||
class Parser: | class Parser: | ||||
def __init__(self, parser_conf, term_matcher, resolve_ambiguity=True): | |||||
def __init__(self, parser_conf, term_matcher, resolve_ambiguity=True, debug=False): | |||||
analysis = GrammarAnalyzer(parser_conf) | analysis = GrammarAnalyzer(parser_conf) | ||||
self.parser_conf = parser_conf | self.parser_conf = parser_conf | ||||
self.resolve_ambiguity = resolve_ambiguity | self.resolve_ambiguity = resolve_ambiguity | ||||
self.debug = debug | |||||
self.FIRST = analysis.FIRST | self.FIRST = analysis.FIRST | ||||
self.NULLABLE = analysis.NULLABLE | self.NULLABLE = analysis.NULLABLE | ||||
@@ -296,6 +297,10 @@ class Parser: | |||||
# symbol should have been completed in the last step of the Earley cycle, and will be in | # symbol should have been completed in the last step of the Earley cycle, and will be in | ||||
# this column. Find the item for the start_symbol, which is the root of the SPPF tree. | # this column. Find the item for the start_symbol, which is the root of the SPPF tree. | ||||
solutions = [n.node for n in columns[-1] if n.is_complete and n.node is not None and n.s == start_symbol and n.start == 0] | solutions = [n.node for n in columns[-1] if n.is_complete and n.node is not None and n.s == start_symbol and n.start == 0] | ||||
if self.debug: | |||||
from .earley_forest import ForestToPyDotVisitor | |||||
debug_walker = ForestToPyDotVisitor() | |||||
debug_walker.visit(solutions[0], "sppf.png") | |||||
if not solutions: | if not solutions: | ||||
expected_tokens = [t.expect for t in to_scan] | expected_tokens = [t.expect for t in to_scan] | ||||
@@ -24,8 +24,8 @@ from .earley_forest import SymbolNode | |||||
class Parser(BaseParser): | class Parser(BaseParser): | ||||
def __init__(self, parser_conf, term_matcher, resolve_ambiguity=True, ignore = (), complete_lex = False): | |||||
BaseParser.__init__(self, parser_conf, term_matcher, resolve_ambiguity) | |||||
def __init__(self, parser_conf, term_matcher, resolve_ambiguity=True, ignore = (), complete_lex = False, debug=False): | |||||
BaseParser.__init__(self, parser_conf, term_matcher, resolve_ambiguity, debug) | |||||
self.ignore = [Terminal(t) for t in ignore] | self.ignore = [Terminal(t) for t in ignore] | ||||
self.complete_lex = complete_lex | self.complete_lex = complete_lex | ||||