为一个字符的标识符定义

为一个字符的标识符定义

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

问题描述

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

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')+;

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

这有什么问题?

谢谢!

推荐答案

你说得对,规则本身没问题,但我发现 ANTLR 中的很多都取决于规则的顺序.我之前有另一个规则,它已经匹配了 ID 规则.终于说得通了,因为 Lexer 无法决定采用哪条规则.

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.

谢谢!

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

08-29 07:29