Antlr syntax handling Errors or how to give the best message about an unexpected token

We have the following Antlr grammar subparts:

signed_int
        : SIGN? INT
    ;

INT : '0'..'9'+
        ;

When someone enters a numerical value, everything is fine, but if they mistakenly enter something like 1O (one and capital o), we get a cryptic error message like:

error 1 : Missing token  at offset 14
near [Index: 0 (Start: 0-Stop: 0) ='<missing COLON>'     type<24> Line: 26 LinePos:14]
 : syntax error...

What is a good way to handle this type of error? I was thinking about the defining type of SYMBOL tokens, but this leads to too many parser building errors. I will continue to study Antlr error handling, but I thought I posted this here to find some ideas.

+5
source share
1 answer

You must override the reportError methods in lexer and parser. You can do this by adding this code to the lexer file:

  @Override
public void reportError(RecognitionException e) {
    throw new RuntimeException(e);
}

, , :

 public static boolean matches(String input) {
     try {
         regExLexer lexer = new regExLexer(new ANTLRStringStream(input));
         regExParser parser = new regExParser(new CommonTokenStream(lexer));
         parser.goal();
         return true;
     } catch (RuntimeException e) {
         return false;
     }
     catch (Exception e) {
         return false;
     }
     catch (OutOfMemoryError e) {
         return false;
     }

 }

 @Override
 public void reportError(RecognitionException e) {
     throw new RuntimeException(e);
 }

Parser.matches(input); , . , true, false, , false, .

+4

All Articles