1. Martin Vejnár
  2. yapylr


Martin Vejnár  committed fadb8ba

On table conflict, a counterexample is now printed.

  • Participants
  • Parent commits f5a88b2
  • Branches default

Comments (0)

Files changed (3)

File lime_grammar.py

View file
  • Ignore whitespace
     def __repr__(self):
         return 'LexLiteral(%r)' % self.literal
+    def __str__(self):
+        return repr(self.literal)
 class _LimeLexerClassify:
     def __init__(self):
         self.quote = None

File limecc.py

View file
  • Ignore whitespace
         from lime_grammar import parse_lime_grammar, make_lime_parser
         g = parse_lime_grammar(input)
-        from lrparser import InvalidGrammarError
+        from lrparser import InvalidGrammarError, ActionConflictError
         p = make_lime_parser(g, keep_states=options.print_states)
         if options.print_states:
         from lime_cpp import lime_cpp
         open(options.output, 'w').write(lime_cpp(p))
+    except ActionConflictError, e:
+        print e
+        print 'Counter-example:', ', '.join((str(sym) for sym in e.counterexample()))
+        return 1
     except Exception, e:
         import traceback

File lrparser.py

View file
  • Ignore whitespace
 class ActionConflictError(InvalidGrammarError):
     """Raised during a construction of a parser, if the grammar is not LR(k)."""
-    def __init__(self, message, relevant_state, states):
+    def __init__(self, message, relevant_state, states, g):
         InvalidGrammarError.__init__(self, message)
         self.states = states
         self.relevant_state = relevant_state
+        self.g = g
     def pretty_states(self):
         """Returns a string with pretty-printed itemsets."""
         return '\n'.join(res)
+    def counterexample(self):
+        trace = []
+        st = self.relevant_state
+        while st.parent_id:
+            trace.append(st.parent_symbol)
+            st = self.states[st.parent_id]
+        return reversed([self.g.token_comments.get(sym, sym) for sym in trace])
 class ParsingError(Exception):
     """Raised by a parser if the input word is not a sentence of the grammar."""
     def __init__(self, message, position):
                     state.goto[symbol] = len(states)
                     state_map[newstate] = len(states)
                     newstate.parent_id = i
+                    newstate.parent_symbol = symbol
             i += 1
         def add_action(state, lookahead, action, item):
             if lookahead in state.action and state.action[lookahead] != action:
                 raise ActionConflictError('LR(%d) table conflict: actions %s, %s trying to add %s'
-                    % (k, state.action[lookahead], action, item), state, states)
+                    % (k, state.action[lookahead], action, item), state, states, grammar)
             state.action[lookahead] = action
         for state_id, state in enumerate(states):
     def __init__(self, itemlist, grammar, first):
         self._close(itemlist, grammar, first)
         self.parent_id = None
+        self.parent_symbol = None
         self.goto = {}
         self.action = {}