ANTLR how to detect rubbish data at end of input

拟墨画扇 提交于 2019-12-23 12:34:50

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/2147964/antlr-how-to-detect-rubbish-data-at-end-of-input

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