简体   繁体   English

解析器生成器,生成独立的C ++代码

[英]parser generator that generates stand-alone C++ code

Is there a LALR parser generator that produces stand-alone C++ code? 是否有LALR解析器生成器可生成独立的C ++代码? I am hoping that it would generate two files named something like "Parser.cpp" and "Parser.hpp," and the generated parser is implemented in a single class (that I can wrap in whatever namespace) that I can use for my parsing needs. 我希望它将生成两个名为“ Parser.cpp”和“ Parser.hpp”的文件,并且生成的解析器在单个类中实现(可以包装在任何名称空间中),可以用于解析需要。

I want to use it for fun (ie small personal projects), and I'd like the output to be stand-alone (without any headers) so that I know I can compile it wherever I have a C++ compiler. 我想将其用于娱乐(即小型个人项目),并且我希望输出是独立的(没有任何标题),以便我知道我可以在有C ++编译器的任何地方对其进行编译。

The search so far: 到目前为止的搜索:

I've looked at flex/bison, but AFAIK they both require special headers and libraries. 我看过flex / bison,但是AFAIK都需要特殊的头文件和库。 I've also looked at ANTLR a little bit, but it is not obvious to me that it can generate stand-alone C++ code. 我也对ANTLR进行了一些研究,但是对我来说,它可以生成独立的C ++代码并不明显。 If someone can confirm that it can, then I might look more into it. 如果有人可以确认它可以,那么我可能会对其进行更多研究。

GOLD Parser (Bart Kiers mentioned the list on Wikipedia) has support for C and C++ languages. GOLD Parser (Bart Kiers在Wikipedia上提到了列表)具有对C和C ++语言的支持。 It does not generate a completely self-contained C/C++ source code file. 它不会生成完全独立的C / C ++源代码文件。 All it does is the generation of Lexer/Parser tables which can be consumed by the "parsing engine". 它所做的只是生成Lexer / Parser表,这些表可以由“解析引擎”使用。

To accomplish your task (or something similar) I did the following: 为了完成您的任务(或类似任务),我执行了以下操作:

  1. Prepare your LALR grammar in Gold's format 以Gold格式准备您的LALR语法

  2. Generate parsing tables (one binary file) 生成解析表(一个二进制文件)

  3. Use an old trick to convert the binary file into a header file like 使用古老的技巧将二进制文件转换为头文件,例如

    unsigned char ParseTable[] = { ... }; unsigned char ParseTable [] = {...};

  4. Modify the loader from the "parsing engine" sources (or use the C version which supports in-memory loading, as I remember) 从“解析引擎”源修改加载程序(或者,我记得使用支持内存加载的C版本)

  5. Combine the sources for the GPEngine (if it is a C++ version) into the .h/.cpp pair. 将GPEngine的源代码(如果是C ++版本)组合到.h / .cpp对中。

  6. Append the ParseTable to .cpp 将ParseTable附加到.cpp

Sure, it's not that straightforward, but all the steps can in principle be done within a single "combine" script which can be used with a number of grammars. 当然,这不是那么简单,但是所有步骤原则上都可以在单个“组合”脚本中完成,该脚本可以与多种语法一起使用。

I guess the major drawback is the fact that GOLD is closed-source and windows-only (it means that to produce the parsing tables you have to use Windows machine). 我猜主要的缺点是GOLD是封闭源代码和仅Windows的事实(这意味着要生成解析表,您必须使用Windows计算机)。

ANTLR can generate C++ code although IMHO I find the support for C++ is a bit weak, it is more like C code. ANTLR 可以生成C ++代码,尽管恕我直言,我发现对C ++的支持有点薄弱,它更像C代码。 Still it is a good environment to work with ANTLRWorks giving you a graphical representation of your syntax tree. 仍然可以与ANTLRWorks一起使用,这是一个很好的环境,可为您提供语法树的图形表示。

The output from flex+bison consists of two .c files and one .h file. flex + bison的输出包含两个.c文件和一个.h文件。 These are completely stand-alone, in that they are all you need to compile into your application to make use of the parser. 这些都是完全独立的,您只需要将它们编译到应用程序中即可使用解析器。 There are no additional libraries or headers needed (beside the standard C ones). 不需要其他库或标头(在标准C之外)。

Unless I've misunderstood your requirements, you definitely can do what you want with flex+bison. 除非我误解了您的要求,否则您绝对可以使用flex + bison来完成您想要的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM