ANTLR 语法中的等于(不是标记)。这意味着什么?

发布于 2024-08-20 10:29:08 字数 158 浏览 1 评论 0原文

下面的规则中 basename = 的构造是什么?

tabname:
   (ID'.')? basename = ID
;

语法中仅出现一次basename

谢谢

What does the construct basename = in the following rule?

tabname:
   (ID'.')? basename = ID
;

There is this single occurrence of basename in the grammar.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

叹沉浮 2024-08-27 10:29:08

在该语法中使用 equals 创建一个名为 basename 的变量,然后可以在操作中引用该变量:

tabname:
    (ID '.')? basename=ID {
        if ($basename.equals("CAT"))
            System.out.println("CAT found");
    };

Using equals in that syntax creates a variable called basename that can then be referenced in actions:

tabname:
    (ID '.')? basename=ID {
        if ($basename.equals("CAT"))
            System.out.println("CAT found");
    };
帅冕 2024-08-27 10:29:08

它用于命名变量。

如果您想在解析器期间运行一些代码,这可能非常有用。

考虑 java 计算器示例:

expr returns [float r]
{
float a,b;
r=0;
}
:   #(PLUS a=expr b=expr)   {r = a+b;}
|   #(STAR a=expr b=expr)   {r = a*b;}
|   i:INT           {r = (float)Integer.parseInt(i.getText());}
;

这里我们说,当我们匹配具有 PLUS 或 STAR 标记后跟 2 个表达式的树时,我们将匹配表达式并将它们命名为 ab 分别。

在我们将使用我们在 java 语句中匹配的这些变量之后。该语句包含在 {} 括号内。这里我们使用java语句来实际进行计算。

It is used to name variables.

This can be very useful if you want to run some code during the parser.

Consider the java calculator example:

expr returns [float r]
{
float a,b;
r=0;
}
:   #(PLUS a=expr b=expr)   {r = a+b;}
|   #(STAR a=expr b=expr)   {r = a*b;}
|   i:INT           {r = (float)Integer.parseInt(i.getText());}
;

Here we say when we match a tree that has a PLUS or STAR token followed by 2 expressions, we'll match the expressions and name them a and b respectively.

After we'll use those variables we matched in a java statement. This statement is contained inside the { and } brackets. Here we use the java statements to actually do the calculation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文