配置文件+ yacc;如何实际获取数据
我在 yacc 中编写了一个简单的配置文件解析器,它处理如下文件:
asdf=50
foobar=42
即 word=number。
我的问题是,如何将其集成到我的程序中?我最终希望将处理后的值存储在某些内部数据结构中,以便稍后可以随意访问。我见过的每个 yacc 示例都只是 printf() 将值输出到标准输出,但这在实际程序中似乎毫无用处。
I've written a simple config file parser in yacc, which processes files like this:
asdf=50
foobar=42
I.e. word=number.
My question is, how do I integrate this into my program? I would ultimately like to have the processed values stored in some internal data structure that I can access later as I please. Every yacc example I have seen simply printf()s the value out to stdout, but that seems kind of useless in an actual program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以用将数据插入到您自己的数据结构中的代码来替换这些 printf。类似这样的:
其中
add_data
是一个函数,它将$1
、$2
和$3
中包含的数据添加到变量your_data
中包含的自定义数据结构。恐怕
your_data
必须是全局的。我挖掘了一些旧的 yacc 项目以及其他 stackoverflow 问题,例如 this 而且我还没有找到另一种方法。如果有人知道更好的方法,请评论。如果您发布代码,我可以尝试帮助您解决具体问题。
You can replace those printf's by code that inserts the data into a data structure of your own. Something like this:
where
add_data
is a function that adds the data contained in$1
,$2
, and$3
to a custom data structure contained in the variableyour_data
.I'm afraid
your_data
has to be global. I dug in some old yacc projects I have and in other stackoverflow questions like this and I haven't found another way of doing it. If someone knows of a better way, please comment.If you post your code I can try to help you with specifics.