I'm trying to work on a kind of code generator to help unit-testing an legacy C/C++ blended project. I don't find any kind of independent tool can generate stub code from declaration. So I decide to build one, it shouldn't be that hard.
Please, anybody can point me a standard grammar link, better described by yacc language.
Hope I'm not reinventing wheel, please help me out in that case.
Best Regards, Kevin
From the C++ FAQ Lite:
38.11 Is there a yacc-able C++ grammar?
The primary yacc grammar you'll want is from Ed Willink. Ed believes his grammar is fully compliant with the ISO/ANSI C++ standard, however he doesn't warrant it: "the grammar has not," he says, "been used in anger." You can get the grammar without action routines or the grammar with dummy action routines. You can also get the corresponding lexer. For those who are interested in how he achieves a context-free parser (by pushing all the ambiguities plus a small number of repairs to be done later after parsing is complete), you might want to read chapter 4 of his thesis.
There is also a very old yacc grammar that doesn't support templates, exceptions, nor namespaces; plus it deviates from the core language in some subtle ways. You can get that grammar here or here.
I've recently found some grammar files for C++ (C++ 1998: ISO/IEC 14882:1998 and C++ 2008: ISO/IEC SC22/WG21 N2723=08-0233) at the grammarware website. The grammars are represented in Enahnced BNF, DMS BNF, BGF, SDF and Rascal notation. It's a pity, though, that the C++ grammars don't seem to get updated (no C++2003 or C++11).
Jared's link is the closest thing to a context-free grammar you can get. Certain things do need to be delayed for later, but that is by some arguments better than the context-sensitive grammar of C++.
To make things worse, C++1x will complexify the grammar significantly. To get as far as a perfect parse of C++, a parser will need to implement enough of the standard to correctly do overload resolution, including template argument deduction, which in turn will require the concepts mechanism, lambdas, and in effect almost all of the language, except for two-stage name lookup and exception specifications which, if I recall correctly, do not need actual implementation to parse a program successfully.
In effect, you are halfway to a compiler if you can parse C++.
For another approach, you could consider piggy-backing on an existing compiler.
GCC-XML will "compile" C++ into XML files with a lot of useful information; it may be enough for your purposes.
Unfortunately, GCC-XML is only 1/4-maintained, and getting it to work can be...interesting. Good luck, if you go this route.
I found this one recently. I haven't tried it out, so am not sure if it works. Could you give more info on the tool you're trying to develop? I downloaded this grammar because I'm working on an instrumentation tool so I can add coverage info for my unit test framework.
After re-reading your comment...
I think this tool exactly fit your needs.
Our DMS Software Reengineering Toolkit can be obtained with a robust, full featured C++ parser. See http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html This builds ASTs and symbol tables, and can infer the type of any expression. DMS enables one to carry out arbitrary analyses and transformations on the C++ code.
One "simple" transformation is instrumenting the code to collect test coverage data; we offer this as a COTS tool. See this paper to understand how DMS does it: http://www.semanticdesigns.com/Company/Publications/TestCoverage.pdf
EDIT September 2013 (This answer was getting a bit stale): DMS's C++ parser/name resolution/control flow analysis handles full C++11, in the ISO-, GNU- and Microsoft variants. It will also parse (and retain) source code containing most preprocessor conditionals. It has an explicit grammar driving the parsing process, unlike GCC or Clang.
来源:https://stackoverflow.com/questions/613479/where-can-i-find-standard-bnf-or-yacc-grammar-for-c-language