ANTLR 如何在输入结束时检测垃圾数据 [英] ANTLR how to detect rubbish data at end of input

查看:29
本文介绍了ANTLR 如何在输入结束时检测垃圾数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当使用 ANTLR 编写的语法时,解析器正确识别来自输入流的数据,但是如果我在输入的末尾有一些垃圾文本(不应该被语法解析),解析器不会抱怨.

我想这种行为没问题(我的意思是解析器完成了它的工作并解析了我说它应该解析的任何内容),但是有什么技巧可以检测解析器完成后输入中何时还有任何数据工作?

谢谢.

解决方案

因为 Scott 已经在 ANTLR 上回答了邮件列表:

<块引用><块引用>

你好

使用 ANTLR 编写的语法时,解析器正确识别来自输入流的数据,但如果我在末尾有一些垃圾文本输入(哪些垃圾文本不应该被语法解析)解析器没有抱怨.

我想这种行为没问题(我的意思是解析器完成了它的工作并解析了无论我说它应该解析什么),但是有什么技巧可以检测到解析器完成工作后,输入中是否还有数据?

谢谢,加比.

在开始规则的末尾添加一个 EOF 令牌

-- 斯科特

When using grammars written in ANTLR, the parser correctly recognizes data from an input stream, but if I have some rubbish text at the end of the input (which is not supposed to be parsed by the grammar) the parser does not complain.

I guess this behavior is all right (I mean the parser did its job and parsed whatever I said it should parse), but is there any trick to detect when there is any data left in the input after the parser has done its job?

Thanks.

解决方案

As Scott already answered on the ANTLR mailing list:

Hello,

When using grammars written in ANTLR, the parser correctly recognizes data from an input stream, but if I have some rubbish text at the end of the input (which rubbish text is not supposed to be parsed by the grammar) the parser does not complain.

I guess this behavior is all right (I mean the parser did its job and parsed whatever I said it should parse), but is there any trick to detect when there is any data left in the input after the parser has done its job?

Thanks, Gabi.

Add an EOF token to the end of your start rule

-- Scott

这篇关于ANTLR 如何在输入结束时检测垃圾数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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