Browse Source

Added a nicer error for duplicate rules

tags/gm/2021-09-23T00Z/github.com--lark-parser-lark/0.66
Erez Shinan 5 years ago
parent
commit
00510edee6
1 changed files with 5 additions and 1 deletions
  1. +5
    -1
      lark/parsers/grammar_analysis.py

+ 5
- 1
lark/parsers/grammar_analysis.py View File

@@ -1,3 +1,4 @@
from collections import Counter

from ..utils import bfs, fzset, classify
from ..exceptions import GrammarError
@@ -111,7 +112,10 @@ class GrammarAnalyzer(object):
rules = parser_conf.rules + [Rule(NonTerminal('$root'), [NonTerminal(parser_conf.start), Terminal('$END')])]
self.rules_by_origin = classify(rules, lambda r: r.origin)

assert len(rules) == len(set(rules))
if len(rules) != len(set(rules)):
duplicates = [item for item, count in Counter(rules).items() if count > 1]
raise GrammarError("Rules defined twice: %s" % ', '.join(str(i) for i in duplicates))

for r in rules:
for sym in r.expansion:
if not (sym.is_term or sym in self.rules_by_origin):


Loading…
Cancel
Save