ANTLR3 词法分析器优先级

发布于 2024-09-27 18:32:47 字数 948 浏览 1 评论 0原文

我想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巾帼英雄 2024-10-04 18:32:47

你看过http://sds.sourceforge.net/src/antlr/doc吗/lexer.html

一个可能的解决方案是定义以下内容:

fragment
INT : DIGIT+
    ;

fragment
RANGE : INT DOTDOTSEP INT
      ;

fragment
FLOAT : INT (('.' INT (('e'|'E') INT)? 'f'?) | (('e'|'E') INT)? ('f'))
      ;

NUMBER
    : (INT '.') => FLOAT       { $type=FLOAT; }
    | (INT DOTDOTSEP) => RANGE { $type=RANGE; }
    | INT                      { $type=INT; }
    ;

Did you look at http://sds.sourceforge.net/src/antlr/doc/lexer.html ?

A possible solution is to define the following:

fragment
INT : DIGIT+
    ;

fragment
RANGE : INT DOTDOTSEP INT
      ;

fragment
FLOAT : INT (('.' INT (('e'|'E') INT)? 'f'?) | (('e'|'E') INT)? ('f'))
      ;

NUMBER
    : (INT '.') => FLOAT       { $type=FLOAT; }
    | (INT DOTDOTSEP) => RANGE { $type=RANGE; }
    | INT                      { $type=INT; }
    ;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文