gpt4 book ai didi

c++ - Flex++ Bisonc++ 解析器

转载 作者:可可西里 更新时间:2023-11-01 18:37:09 29 4
gpt4 key购买 nike

我正在尝试在我的项目中使用 flexbison 来为文件结构生成解析器代码。主要编程语言是 C++,项目采用 OO 设计,主要并行运行。

我听说 flexbison 生成的解析器是 C 代码,它们不可重入。谷歌搜索,我找到了 flex++bisonc++。不幸的是,没有简单的入门教程。大多数示例都基于 bison/flex。有些人以某种方式在他们的 C++ 代码中集成了 bison/flex 解析器。他们应该是“棘手的”......

flex++bisonc++ 的文档对我没有帮助。教程和示例,它们都从 stdin 获取输入并在 stdout 上打印一些消息。

我的解析器需要这些特性:

  1. 解析器应该是一个 C++ 类,以正常方式定义(一个头文件和一个 cpp 文件)
  2. 解析器从 std::stringstd::stringstream 或以 null 结尾的 char* 接收数据。

我感到很困惑。我应该使用 flex++/bisonc++ 还是 flex/bison?以及如何做到这一点,满足上述条件?

最佳答案

有flex/bison、flex++/bison++和flexc++/bisonc++。我认为最好从这三对中选择一个,而不是混合/匹配 flex++ 和 bisonc++。这是 Flexc++ 的用户指南和 Bisonc++ .

来自 Flexc++ 网站:

Flexc++, contrary to flex and flex++, generates code that is explicitly intended for use by C++ programs. The well-known flex(1) program generates C source-code and flex++(1) merely offers a C++-like shell around the yylex function generated by flex(1) and hardly supports present-day ideas about C++ software development. Contrary to this, flexc++ creates a C++ class offering a predefined member function lex matching input against regular expressions and possibly executing C++ code once regular expressions were matched. The code generated by flexc++ is pure C++, allowing its users to apply all of the features offered by that language.

来自 Bisonc++ 网站:

Bisonc++ is a general-purpose parser generator that converts a grammar description for an LALR(1) context-free grammar into a C++ class to parse that grammar. Once you are proficient with bisonc++, you may use it to develop a wide range of language parsers, from those used in simple desk calculators to complex programming languages. Bisonc++ is highly comparable to the program bison++, written by Alain Coetmeur: all properly-written bison++ grammars ought to be convertible to bisonc++ grammars after very little or no change. Anyone familiar with bison++ or its precursor, bison, should be able to use bisonc++ with little trouble. You need to be fluent in using the C++ programming in order to use bisonc++ or to understand this manual.

所以 flexc++/bisonc++ 不仅仅是旧的 flex/bison 实用程序的包装器。它们生成完整的 C++ 类以用于重入扫描/解析。

关于c++ - Flex++ Bisonc++ 解析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674104/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com