耶尔瓦尔和联盟

发布于 2024-08-13 08:51:06 字数 78 浏览 7 评论 0原文

yacc 文件中 union 的用途是什么?它与flex文件中的yylval直接相关吗?如果不使用yylval,那么就不需要使用union了?

What is the purpose of union in the yacc file? Is it directly related to yylval in the flex file? If you don't use yylval, then you don't need to use union?

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

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

发布评论

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

评论(2

留一抹残留的笑 2024-08-20 08:51:06

union 的目的是允许将不同类型的对象存储到由 flex 发出的节点中。

为了更好地解释,

%union
{
    int intValue;
    float floatValue;
    char *stringValue;
}

如果您想为 intfloatstring< 提供基本支持,您可以在 .y 中 使用以下示例: /代码> 类型。你能用这个做什么?

有两件事:

首先,您可以在生成令牌时自动设置正确的值。考虑一下上一个示例的 .l 文件,您可以:

[a-zA-Z][a-zA-Z0-9]* {
 yylval.stringValue = strdup(yytext);
 return IDENTIFIER;
}

[0-9]+ { 
 yylval.intValue = atoi(yytext);
 return INTEGER;
}

[0-9]*\.[0-9]+"f"? {
    yylval.floatValue = new atof(yytext);
 return FLOAT;
}

此外,您可以直接在您的 flex 语法中使用值

nexp: nexp '+' nexp { 
lt;floatValue>$ = 
lt;floatValue>1 + 
lt;floatValue>3 }

最后,如果您计划使用 OOP 语法树您可以将 union 定义为

%union
{
    class ASTNode *node;
}

其中 ASTNode 是任何类型语法节点的祖先类。

The purpose of the union is to allow storing different kind of objects into nodes emitted by flex.

To explain better you can have for example:

%union
{
    int intValue;
    float floatValue;
    char *stringValue;
}

in .y if you want to provide basic support for int, float and string types. What can you do with this?

Two things:

First, you can automatically set right values when generating tokens. Think about .l file of the previous example, you can have:

[a-zA-Z][a-zA-Z0-9]* {
 yylval.stringValue = strdup(yytext);
 return IDENTIFIER;
}

[0-9]+ { 
 yylval.intValue = atoi(yytext);
 return INTEGER;
}

[0-9]*\.[0-9]+"f"? {
    yylval.floatValue = new atof(yytext);
 return FLOAT;
}

In addition you can use value directly in your flex grammar:

nexp: nexp '+' nexp { 
lt;floatValue>$ = 
lt;floatValue>1 + 
lt;floatValue>3 }

Finally if you plan to use an OOP syntax tree you can define union as

%union
{
    class ASTNode *node;
}

in which ASTNode is the ancestor class of any kind of syntax node.

御守 2024-08-20 08:51:06

%union 声明修改 yylval 的类型。

bison 手册解释

在普通(不可重入)解析器中,标记的语义值必须存储到全局变量yylval中。当您仅使用一种数据类型作为语义值时,yylval 具有该类型。因此,如果类型是 int (默认),您可以在 yylex 中编写:

<前><代码>...
yylval = 值; /* 将值放入 Bison 堆栈。 */
返回整数; /* 返回令牌的类型。 */
...

当您使用多种数据类型时,yylval 的类型是由 %union 声明组成的联合(请参阅值类型集合部分)。因此,当您存储令牌的值时,必须使用联合体的正确成员。如果 %union 声明如下所示:

<前><代码>%联合{
int 间隔;
双值;
symrec *tptr;
}

那么yylex中的代码可能如下所示:

<前><代码>...
yylval.intval = 值; /* 将值放入 Bison 堆栈。 */
返回整数; /* 返回令牌的类型。 */
...

The %union declaration modifies the type of yylval.

The bison manual explains:

In an ordinary (nonreentrant) parser, the semantic value of the token must be stored into the global variable yylval. When you are using just one data type for semantic values, yylval has that type. Thus, if the type is int (the default), you might write this in yylex:

...
yylval = value;  /* Put value onto Bison stack. */
return INT;      /* Return the type of the token. */
...

When you are using multiple data types, yylval's type is a union made from the %union declaration (see section The Collection of Value Types). So when you store a token's value, you must use the proper member of the union. If the %union declaration looks like this:

%union {
  int intval;
  double val;
  symrec *tptr;
}

then the code in yylex might look like this:

...
yylval.intval = value; /* Put value onto Bison stack. */
return INT;          /* Return the type of the token. */
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文