How To Terminate a Lexer in ANTLR4

2019-08-15 08:56发布

问题:

Is there a simple way to terminate a lexer?

There are some tokens that I do not want to handle yet. But I also want the Lexer to sound an alarm if the input does contain those tokens. My simple strategy is throwing a RuntimeException in an action:

CHARIZING:  '#@' {throw new RuntimeException("charizing op not supported yet");};

But the action produces compilation error since the generated Lexer has a break command after the action and the Java compiler complains the break is an unreachable statement.

CPPDefineLexer.java:118: error: unreachable statement
                case 1: throw new RuntimeException("charizing op not supported y
et"); break;

Is there a simple strategy to terminate a Lexer?

回答1:

You can fool the compiler:

CHARIZING
 : '#@' 
   {
     if (true) { throw new RuntimeException("charizing op not supported yet"); }
   }
 ;


标签: antlr4