耶尔瓦尔和联盟
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
union
的目的是允许将不同类型的对象存储到由 flex 发出的节点中。为了更好地解释,
如果您想为
int
、float
和string< 提供基本支持,您可以在
.y
中 使用以下示例: /代码> 类型。你能用这个做什么?有两件事:
首先,您可以在生成令牌时自动设置正确的值。考虑一下上一个示例的
.l
文件,您可以:此外,您可以直接在您的 flex 语法中使用值:
最后,如果您计划使用 OOP 语法树您可以将 union 定义为
其中 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:
in
.y
if you want to provide basic support forint
,float
andstring
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:In addition you can use value directly in your flex grammar:
Finally if you plan to use an OOP syntax tree you can define union as
in which
ASTNode
is the ancestor class of any kind of syntax node.%union
声明修改yylval
的类型。bison
手册解释:The
%union
declaration modifies the type ofyylval
.The
bison
manual explains: