如何在ANTLR4中终止Lexer [英] How To Terminate a Lexer in ANTLR4

查看:70
本文介绍了如何在ANTLR4中终止Lexer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种简单的方法可以终止词法分析器吗?

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中终止Lexer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆