线程安全 C++ lex/yacc 解析器的包装器
我正在尝试编写一个 JSON 解析器(而不是使用免费可用的解析器之一,因为某些项目限制),并使用简单的包装器 C++ 类编写了基于 lex+yacc 的版本。我已经重新定义了 YY_INPUT 宏,以便 lex 从内存缓冲区读取。现在的问题是确保解析器是线程安全的,但我不确定确保这一点有多容易。有两个问题:
- 最终 YY_INPUT 是从全局对象读取。我想不出另一种方法来做到这一点。
- 我不知道生成的 lex/yacc 代码最终使用了多少个全局变量。
如果人们可以分享他们做类似事情的经验,那就太好了。
干杯。
附言。我们不使用 STL/字符串或任何与此相关的模板。我们使用我们自己的基于变体的容器。我们在四个主要的 Unices 上使用 lex+yacc 而不是 flex+bison。
I am trying to write a JSON parser (instead of using one of the freely available ones, because of certain project constraints) and have written lex+yacc based version with a simple wrapper C++ class. I have redefined the YY_INPUT macro for lex to read from a memory buffer. Now the deal is to ensure that the parser is thread-safe and I am not sure how easy it is to ensure that. There are two concerns:
- Ultimately YY_INPUT is reading from a global object. I could not think of another way of doing this.
- I have no idea how many globals does the generated lex/yacc code end up using.
Would be great if folks can share their experience of doing something similar.
Cheers.
PS. We don' t use STL/string or any templates for that matter. We use our own variant-based containers. We use lex+yacc rather than flex+bison, on four major Unices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有太多直接使用
yacc
的经验,但我知道bison
支持 线程安全的可重入解析器。它看起来也像lex
也支持可重入词法分析器,我猜如果你把两者放在一起应该会很好。I don't have much experience working directly with
yacc
, but I know thatbison
supports reentrant parsers that are thread-safe. It also looks likelex
supports a reentrant lexer as well, and I'd guess that if you put the two together it should work out just fine.