ANTLR not throwing errors on invalid input

后端 未结 2 1850
暖寄归人
暖寄归人 2020-12-11 15:28

I\'m using ANTLR to parse logical expressions in a Java tool I\'m writing, and I\'m having issues because passing invalid input strings to the generated ANTLR lexer and pars

2条回答
  •  悲&欢浪女
    2020-12-11 15:39

    An easy fix would be to override your lexer's and parser's reportError(...) and throw an exception of your own instead of letting ANTLR trying to recover from the incorrect syntax/input:

    grammar YourGrammar;
    
    // options/header/tokens
    
    @parser::members {
      @Override
      public void reportError(RecognitionException e) {
        throw new RuntimeException("I quit!\n" + e.getMessage()); 
      }
    }
    
    @lexer::members {
      @Override
      public void reportError(RecognitionException e) {
        throw new RuntimeException("I quit!\n" + e.getMessage()); 
      }
    }
    
    // lexer & parser rules
    

    More on error reporting (and recovery): https://theantlrguy.atlassian.net/wiki/display/ANTLR3/Error+reporting+and+recovery

提交回复
热议问题