本文介绍了如何在 ANTLR4 中终止词法分析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种简单的方法可以终止词法分析器?
Is there a simple way to terminate a lexer?
有些令牌我还不想处理.但我也希望 Lexer 在输入确实包含这些标记时发出警报.我的简单策略是在一个动作中抛出一个 RuntimeException:
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");};
但是该操作会产生编译错误,因为生成的 Lexer 在该操作之后有一个 break 命令,并且 Java 编译器抱怨 break 是一个无法访问的语句.
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;
是否有终止 Lexer 的简单策略?
Is there a simple strategy to terminate a Lexer?
推荐答案
你可以欺骗编译器:
CHARIZING
: '#@'
{
if (true) { throw new RuntimeException("charizing op not supported yet"); }
}
;
这篇关于如何在 ANTLR4 中终止词法分析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!