简体   繁体   English

JavaCC:定制词法分析器错误消息

[英]JavaCC: customizing lexer error messages

According to the JavaCC documentation , if I want to customize the messages given by errors in the lexer: 根据JavaCC文档 ,如果我要自定义词法分析器中的错误给出的消息:

To customize error reporting by the lexer, the lexer class must be subclassed and this method [ LexicalError() in TokenMgrError ] redefined in the subclass. 要自定义词法分析器的错误报告,必须对词法分析器类进行子类化,并在子类中重新定义此方法[ TokenMgrError LexicalError() ]。 To help with creating your error reporting scheme, the following variables are available: 为了帮助您创建错误报告方案,可以使用以下变量:

However, I see nothing about how to get the generated code to use that subclass, unlike with the TOKEN_FACTORY option. 但是,与TOKEN_FACTORY选项不同,我对如何获取生成的代码以使用该子类TOKEN_FACTORY

On the other hand, looking in the TokenMgrError source, some comments indicate that the source file should be directly edited, rather than subclassed. 另一方面,在TokenMgrError源中查看时,一些注释指示应直接编辑源文件,而不是对其进行子类化。 However, if I do that, the next time I change the .jj file and have Ant regenerate the parser and lexer code, I get this warning: 但是,如果执行此操作,则下次更改.jj文件并让Ant重新生成解析器和词法分析器代码时,会收到以下警告:

Warning: TokenMgrError.java: File is obsolete. 警告:TokenMgrError.java:文件已过时。 Please rename or delete this file so that a new one can be generated for you. 请重命名或删除此文件,以便可以为您生成一个新文件。

So, how am I supposed to be doing this? 那么,我应该怎么做呢?

You can modify TokenMgrError if you want. 您可以根据需要修改TokenMgrError There isn't a lot of downside to it. 它没有太多缺点。

If you subclass the token manager, then you can subclass TokenMgrError too. 如果将令牌管理器作为子类,则也可以将TokenMgrError子类TokenMgrError You just need to be sure to override all the routines that create TokenMgrError . 您只需要确保覆盖所有创建TokenMgrError的例程TokenMgrError To get the parser to use your subclass, you just create the parser like this 要使解析器使用您的子类,您只需像这样创建解析器

MyParser parser = new MyParser( new MySubclassTokenManagr(...) ) ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM