4

I want to know how can I keep the lexer or parser from running when it finds a mismatch. For Example if the lexer expected a '.' I want it not to continue with recovery mode.

2
  • 3
    Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable Example. Commented Nov 21, 2015 at 19:21
  • 1
    Actually, this is a very reasonable question! The above comment from @MattDMo criticizes that the problem and expected behaviour is not described well, but that is not true.Humberto clearly wrote he wants the lexer to abort if it encounters an unknown symbol. This is a very wise strategy in unit tests! Commented Jun 10, 2020 at 9:32

1 Answer 1

2

This is what worked for me:

import sys
from antlr4 import *
from bin.LEDSGrammarLexer import LEDSGrammarLexer
from bin.LEDSGrammarParser import LEDSGrammarParser
from bin.LEDSGrammarListener import LEDSGrammarListener
from src.Gramatica import Gramatica
from src.Traductor import Translator
#Add This Library
from antlr4.error.ErrorListener import ErrorListener

import src.CuboSemantico

class MyErrorListener( ErrorListener ):
    def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
        print str(line) + ":" + str(column) + ": sintax ERROR, " + str(msg)
        print "Terminating Translation"
        sys.exit()

    def reportAmbiguity(self, recognizer, dfa, startIndex, stopIndex, exact, ambigAlts, configs):
        print "Ambiguity ERROR, " + str(configs)
        sys.exit()

    def reportAttemptingFullContext(self, recognizer, dfa, startIndex, stopIndex, conflictingAlts, configs):
        print "Attempting full context ERROR, " + str(configs)
        sys.exit()

    def reportContextSensitivity(self, recognizer, dfa, startIndex, stopIndex, prediction, configs):
        print "Context ERROR, " + str(configs)
        sys.exit()

def main(argv):
    print "Parsing: " + argv[1] + "\n"
    input = FileStream(argv[1])
    lexer = LEDSGrammarLexer(input)

    #This was the key!
    stream = CommonTokenStream(lexer)
    parser = LEDSGrammarParser(stream)
    parser._listeners = [ MyErrorListener() ]

    tree = parser.programa()
    printer = Gramatica()
    walker = ParseTreeWalker()
    result = walker.walk(printer,tree)
    print "Resultado del parseo" + str(result)
    for idx,x in enumerate(printer.Cuadruplos):
        print str(idx) +" - "+str(x)
    translator = Translator()
    translator.translate(printer)
    #print(tree.toStringTree(recog=parser))

if __name__ == '__main__':
    main(sys.argv)

Thanks to: HAR

Reference Answer

Sign up to request clarification or add additional context in comments.

1 Comment

I would suggest using the method addErrorListener provided by the Recognizer class. parser.addErrorListener(MyErrorListener())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.