在 Antlr 中跳过不匹配的输入 [英] Skipping unmatched input in Antlr

查看:24
本文介绍了在 Antlr 中跳过不匹配的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在语法中指定我想跳过所有与任何规则不匹配的输入(否则会引发识别错误)?

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)?

推荐答案

是的.实现取决于您需要/想要跳过的位置.

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.

这篇关于在 Antlr 中跳过不匹配的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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