ANTLR exception handling with “$”, Java

后端 未结 1 1211
无人及你
无人及你 2021-01-16 17:39

For my grammar in ANTLR, my java code can catch and print errors for inputs containing \"$\". In my implementation, I need to print out \"success\" for successful input. So,

相关标签:
1条回答
  • 2021-01-16 18:39

    Possibly the lexer (or parser) tries to recover from (minor) errors and continues tokenizing or parsing. If you want to terminate whenever some illegal character occurs, the easiest is to create some sort of "fall-through" rule that is placed at the end of all your lexer rules which will match if none of the lexer rules above have matched, and let that rule throw an exception:

    grammar T;
    
    // parser rules
    
    // lexer rules
    
    /* last lexer rules */
    FALL_THROUGH
      :  .  {throw new RuntimeException("Illegal character: " + getText());}
      ;
    
    0 讨论(0)
提交回复
热议问题