Browse Source

Fix LALR shift/reduce warnings when debug=True (Issue #258)

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.66
Erez Shinan 6 years ago
parent
commit
a892f184e3
5 changed files with 13 additions and 9 deletions
  1. +1
    -1
      lark/grammar.py
  2. +4
    -2
      lark/parser_frontends.py
  3. +3
    -3
      lark/parsers/grammar_analysis.py
  4. +3
    -1
      lark/parsers/lalr_analysis.py
  5. +2
    -2
      lark/parsers/lalr_parser.py

+ 1
- 1
lark/grammar.py View File

@@ -46,7 +46,7 @@ class Rule(object):
self.options = options

def __str__(self):
return '<%s : %s>' % (self.origin, ' '.join(map(str,self.expansion)))
return '<%s : %s>' % (self.origin.name, ' '.join(x.name for x in self.expansion))

def __repr__(self):
return 'Rule(%r, %r, %r, %r)' % (self.origin, self.expansion, self.alias, self.options)


+ 4
- 2
lark/parser_frontends.py View File

@@ -39,12 +39,14 @@ class WithLexer:

class LALR_TraditionalLexer(WithLexer):
def __init__(self, lexer_conf, parser_conf, options=None):
self.parser = lalr_parser.Parser(parser_conf)
debug = options.debug if options else False
self.parser = lalr_parser.Parser(parser_conf, debug=debug)
self.init_traditional_lexer(lexer_conf)

class LALR_ContextualLexer(WithLexer):
def __init__(self, lexer_conf, parser_conf, options=None):
self.parser = lalr_parser.Parser(parser_conf)
debug = options.debug if options else False
self.parser = lalr_parser.Parser(parser_conf, debug=debug)
self.init_contextual_lexer(lexer_conf)

class LALR_CustomLexer(WithLexer):


+ 3
- 3
lark/parsers/grammar_analysis.py View File

@@ -14,9 +14,9 @@ class RulePtr(object):
self.index = index

def __repr__(self):
before = self.rule.expansion[:self.index]
after = self.rule.expansion[self.index:]
return '<%s : %s * %s>' % (self.rule.origin, ' '.join(before), ' '.join(after))
before = [x.name for x in self.rule.expansion[:self.index]]
after = [x.name for x in self.rule.expansion[self.index:]]
return '<%s : %s * %s>' % (self.rule.origin.name, ' '.join(before), ' '.join(after))

@property
def next(self):


+ 3
- 1
lark/parsers/lalr_analysis.py View File

@@ -82,7 +82,9 @@ class LALR_Analyzer(GrammarAnalyzer):
for k, v in lookahead.items():
if len(v) > 1:
if self.debug:
logging.warn("Shift/reduce conflict for %s: %s. Resolving as shift.", k, v)
logging.warn("Shift/reduce conflict for terminal %s: (resolving as shift)", k.name)
for act, arg in v:
logging.warn(' * %s: %s', act, arg)
for x in v:
# XXX resolving shift/reduce into shift, like PLY
# Give a proper warning


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

@@ -8,10 +8,10 @@ from ..lexer import Token
from .lalr_analysis import LALR_Analyzer, Shift

class Parser:
def __init__(self, parser_conf):
def __init__(self, parser_conf, debug=False):
assert all(r.options is None or r.options.priority is None
for r in parser_conf.rules), "LALR doesn't yet support prioritization"
analysis = LALR_Analyzer(parser_conf)
analysis = LALR_Analyzer(parser_conf, debug=debug)
analysis.compute_lookahead()
callbacks = {rule: getattr(parser_conf.callback, rule.alias or rule.origin, None)
for rule in parser_conf.rules}


Loading…
Cancel
Save