Bison/Yacc 中通过节点的简单字符串
我必须在 yacc 文件的语义规则中连接字符串:
%union {
stringstream sstream;
}
%type<sstream> node1 node2
---
node1
: node2 { $$ << $1 << " goodbye" }
node2
: final { $$ << "hello" }
但是,由于联合中不允许使用 stringstream
甚至 string
,我找不到任何简单的方法来混合char *
、int
,并使节点传输我可以在任何地方操作的字符串。 我该怎么办呢?
I have to concatenate strings in semantic rules of my yacc file:
%union {
stringstream sstream;
}
%type<sstream> node1 node2
---
node1
: node2 { $ << $1 << " goodbye" }
node2
: final { $ << "hello" }
However, as stringstream
or even string
are not allowed in unions, I don't find any easy way to mix char *
, int
, and make nodes transport a string that I can manipulate everywhere.
How should I do it ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不记得
bison
/yacc
详细信息,但您肯定可以使用指针并new
它。如果bison
/yacc
给您机会,请记住删除
。I don't remember
bison
/yacc
details, but you sure can use pointer andnew
it. Just remember todelete
ifbison
/yacc
gives you the opportunity.事实上,跟踪指针并不那么困难。例如:
一个主要问题是,如果您的输入中存在语法错误,导致值在不运行操作的情况下被丢弃,则会泄漏。您可以使用 bison 的
%destructor
扩展来解决这个问题。在这种情况下,您只需要添加:Its actually not all that hard to keep track of the pointers. For example:
The one major issue is that this will leak if there are syntax errors in your input that cause values to be thrown away without running your actions. You can get around that problem by using bison's
%destructor
extension. In this case you need only add: