C++ parser generator [closed]

2019-01-27 00:55发布

问题:

I'm writing my own scripting language and I need a software tool which generates C++ code for parsing my language. I need a lexical analyzer and a parser generator which generates C++ code. It would be nice for me to be able also to generate a Visual C++ 2010 project. Suggestions?

回答1:

Try with Flex and Bison. They are good lexical analizers and parser generator usefull to define new languages.

http://en.wikipedia.org/wiki/Flex_lexical_analyser



回答2:

http://en.wikipedia.org/wiki/Comparison_of_parser_generators

for C/C++: http://epaperpress.com/lexandyacc/

Or look at: Boost.Spirit:

"Spirit is a set of C++ libraries for parsing and output generation implemented as Domain Specific Embedded Languages (DSEL) using Expression templates and Template Meta-Programming."

Dou you really need new language? maybe it would be better to use some well known like Lua, Python?



回答3:

It's an old question but still might be relevant: since I was unhappy with the existing options, I recently wrote a template c++ parser generator which doesn't need any external tools (you include a header and define the grammar directly in the c++ source). It uses readable PEG grammars so there is no need for a separate lexing step. You can check it out on Github.



回答4:

You have two choices: whether you create your own parser by creating an AST (abstract syntax tree), then it will be a good exercise for you but it's very long and hard to implement. Or you can use an open source solution such the ANTLR parser generator which has a grammar for C/C++ as well as the preprocessor. I've never used it so I can't say how complete its parsing of C++ is going to be. Then If you are in hurry and you want to create a good parser, you'd better use the second solution