How To Terminate a Lexer in ANTLR4

帅比萌擦擦* 提交于 2019-12-24 06:34:18

问题


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"); }
   }
 ;


来源:https://stackoverflow.com/questions/22507775/how-to-terminate-a-lexer-in-antlr4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!