Skipping unmatched input in Antlr

[亡魂溺海] 提交于 2019-12-13 02:11:20

问题


Is there a way to specify in a grammar that I want to skip all input that doesnt match any of the rules (that would otherwise throw a recognition error)?


回答1:


Yes. Implementation depends on where you need/want to do the skipping.

In the lexer, a last rule like:

Unknown : . -> skip ; // or -> channel(HIDDEN) ;

will consume any otherwise unmatched input characters yet keep them from being tokenized and considered by the parser. You do want to match a single character at a time so that at every input text index all other lexer rules have a chance to match first.

Similarly, in the parser, a last rule like:

ignored : . ;

will consume unmatched tokens, creating parse tree nodes, each as a context containing a single 'ignored' token. Their presence in the parse-tree can then be, well, ignored.

Again, the ignored rule match should be for just a single token, ensuring that all other longer match rules have priority, and last in the ordering of the rules, ensuring that all other single token match rules are considered first.



来源:https://stackoverflow.com/questions/31689624/skipping-unmatched-input-in-antlr

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