ANTLR3 词法分析器优先级
我想在 ANTLR3 词法分析器中从 '..'
创建一个标记,该标记将用于将表达式串在一起,
a..b // [1]
c .. x // [2]
1..2 // [3]
3 .. 4 // [4]
所以,我添加了,
DOTDOTSEP : '..'
;
问题是我已经有一个规则:
FLOAT : INT (('.' INT (('e'|'E') INT)? 'f'?) | (('e'|'E') INT)? ('f'))
;
并且在示例中[3] 上面的 1..2
被匹配为 FLOAT
(我不知道为什么,因为在第一个 .
之后是另一个 < code>. 不是 INT,但它是)。
我想知道是否有办法更改词法分析器规则的优先级,以便先匹配 DOTDOTSEP,然后再匹配 FLOAT。
看看这里,我似乎输给了,“计数最多的规则是获胜者。”,
但想知道是否有解决方法。
PS INT 定义如下...
fragment DIGIT
: '0'..'9'
;
INT : DIGIT+
;
编辑。 进一步的测试让我觉得它并不像直接匹配 FLOAT
规则那么简单。 (我本来打算改变这个问题,但既然我现在有了答案,我就不会了。)问题(我相信)仍然在于词法分析器规则优先级,所以问题仍然是一样的。
I want to create a token from '..'
in the ANTLR3 lexer which will be used to string together expressions like
a..b // [1]
c .. x // [2]
1..2 // [3]
3 .. 4 // [4]
So, I have added,
DOTDOTSEP : '..'
;
The problem is that I already have a rule:
FLOAT : INT (('.' INT (('e'|'E') INT)? 'f'?) | (('e'|'E') INT)? ('f'))
;
And in example [3] above 1..2
is getting matched as a FLOAT
(I'm not sure why since following the first .
is another .
not an INT, but it is).
I wonder if there is a way to change the precedence of the lexer rules so DOTDOTSEP
gets matched first and then FLOAT
.
Looking here it seems I'm losing out to, "The rule having the greatest count is the winner.",
but wonder if there is a way around it.
P.S. INT is defined as below ...
fragment DIGIT
: '0'..'9'
;
INT : DIGIT+
;
Edit.
A little further testing makes me think it's not quite as simple as it getting directly matched to the FLOAT
rule. (I was going to change the question but since I've got answers now, I won't.) The problem (I believe) still lies in the lexer rules precedence so the question still remains the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你看过http://sds.sourceforge.net/src/antlr/doc吗/lexer.html ?
一个可能的解决方案是定义以下内容:
Did you look at http://sds.sourceforge.net/src/antlr/doc/lexer.html ?
A possible solution is to define the following: