Browse Source

Add debug flag to Early and XEarley to allow dumping the SPPF

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.7.4
night199uk 5 years ago
committed by Erez Sh
parent
commit
d845aa3bf1
3 changed files with 12 additions and 4 deletions
  1. +4
    -1
      lark/parser_frontends.py
  2. +6
    -1
      lark/parsers/earley.py
  3. +2
    -2
      lark/parsers/xearley.py

+ 4
- 1
lark/parser_frontends.py View File

@@ -139,7 +139,8 @@ class Earley(WithLexer):
self.init_traditional_lexer()

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):
return term.name == token.type
@@ -152,10 +153,12 @@ class XEarley(_ParserFrontend):

self._prepare_match(lexer_conf)
resolve_ambiguity = options.ambiguity == 'resolve'
debug = options.debug if options else False
self.parser = xearley.Parser(parser_conf,
self.match,
ignore=lexer_conf.ignore,
resolve_ambiguity=resolve_ambiguity,
debug=debug,
**kw
)



+ 6
- 1
lark/parsers/earley.py View File

@@ -20,10 +20,11 @@ from .earley_common import Item, TransitiveItem
from .earley_forest import ForestToTreeVisitor, ForestSumVisitor, SymbolNode, ForestToAmbiguousTreeVisitor

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)
self.parser_conf = parser_conf
self.resolve_ambiguity = resolve_ambiguity
self.debug = debug

self.FIRST = analysis.FIRST
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
# 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]
if self.debug:
from .earley_forest import ForestToPyDotVisitor
debug_walker = ForestToPyDotVisitor()
debug_walker.visit(solutions[0], "sppf.png")

if not solutions:
expected_tokens = [t.expect for t in to_scan]


+ 2
- 2
lark/parsers/xearley.py View File

@@ -24,8 +24,8 @@ from .earley_forest import SymbolNode


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.complete_lex = complete_lex



Loading…
Cancel
Save