为具有一个字符的标识符定义antlr规则 [英] Defining antlr rule for identifiers with one character

查看:87
本文介绍了为具有一个字符的标识符定义antlr规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢定义一个非常简单的规则,该规则应代表标识符.这对于长度超过一个字符的标识符非常有效,但是对于仅具有一个字符的标识符,我得到一个MismatchedTokenException(64!= 45).这是规则:

ID:('a'..'z'|'A'..'Z')+ ;

这有什么问题?

谢谢!

解决方案

您是完全正确的,规则本身很好,但是我发现ANTLR中的很多内容都取决于规则的顺序.我之前有另一个规则,该规则已经与ID规则匹配.最终这是有道理的,因为Lexer无法决定采用哪个规则.

谢谢!

i like to define a very simple rule, which should represent identifiers. This works quite well for identifiers which are longer than one characters, but for identifiers with exactly one character I get a MismatchedTokenException(64!=45). This is the rule:

ID : ('a'..'z' | 'A'..'Z')+ ;

What is wrong with that?

Thanks!

解决方案

You're absolutely right, the rule on its own is fine, but I figured out that a lot in ANTLR depends on the order of rules. I had another rule before, which did already match the ID rule. Finally it makes sense, because the Lexer cannot decide which rule to take.

Thanks!

这篇关于为具有一个字符的标识符定义antlr规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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