[ACCEPTED]-Where can I find standard BNF or YACC grammar for C++ language?-bnf

Accepted answer
Score: 24

From the C++ FAQ Lite:

38.11 Is there a yacc-able C++ grammar?

The primary yacc grammar you'll 15 want is from Ed Willink. Ed believes his grammar 14 is fully compliant with the ISO/ANSI C++ standard, however he doesn't 13 warrant it: "the grammar has not," he 12 says, "been used in anger." You 11 can get the grammar without action routines or the grammar with dummy action routines. You can also get the corresponding lexer. For those 10 who are interested in how he achieves 9 a context-free parser (by pushing all the 8 ambiguities plus a small number of repairs 7 to be done later after parsing is complete), you 6 might want to read chapter 4 of his thesis.

There 5 is also a very old yacc grammar that doesn't 4 support templates, exceptions, nor namespaces; plus 3 it deviates from the core language in some 2 subtle ways. You can get that grammar 1 here or here.

Score: 4

I've recently found some grammar files for 4 C++ (C++ 1998: ISO/IEC 14882:1998 and C++ 2008: ISO/IEC SC22/WG21 N2723=08-0233) at the grammarware website. The 3 grammars are represented in Enahnced BNF, DMS BNF, BGF, SDF and Rascal notation. It's 2 a pity, though, that the C++ grammars don't 1 seem to get updated (no C++2003 or C++11).

Score: 3

Jared's link is the closest thing to a context-free 16 grammar you can get. Certain things do need 15 to be delayed for later, but that is by 14 some arguments better than the context-sensitive 13 grammar of C++.

To make things worse, C++1x 12 will complexify the grammar significantly. To 11 get as far as a perfect parse of C++, a 10 parser will need to implement enough of 9 the standard to correctly do overload resolution, including 8 template argument deduction, which in turn 7 will require the concepts mechanism, lambdas, and 6 in effect almost all of the language, except 5 for two-stage name lookup and exception 4 specifications which, if I recall correctly, do 3 not need actual implementation to parse 2 a program successfully.

In effect, you are 1 halfway to a compiler if you can parse C++.

Score: 2

I found this one recently. I haven't tried it out, so 6 am not sure if it works. Could you give 5 more info on the tool you're trying to develop? I 4 downloaded this grammar because I'm working 3 on an instrumentation tool so I can add 2 coverage info for my unit test framework.

After re-reading your 1 comment...

I think this tool exactly fit your needs.

Score: 2

For another approach, you could consider 7 piggy-backing on an existing compiler.

GCC-XML will 6 "compile" C++ into XML files with 5 a lot of useful information; it may be enough 4 for your purposes.

Unfortunately, GCC-XML 3 is only 1/4-maintained, and getting it to 2 work can be...interesting. Good luck, if 1 you go this route.

Score: 1

Our DMS Software Reengineering Toolkit can 16 be obtained with a robust, full featured 15 C++ parser. See http://www.semanticdesigns.com/Products/FrontEnds/CppFrontEnd.html This builds ASTs and symbol 14 tables, and can infer the type of any expression. DMS 13 enables one to carry out arbitrary analyses 12 and transformations on the C++ code.

One 11 "simple" transformation is instrumenting 10 the code to collect test coverage data; we 9 offer this as a COTS tool. See this paper 8 to understand how DMS does it: http://www.semanticdesigns.com/Company/Publications/TestCoverage.pdf

EDIT September 7 2013 (This answer was getting a bit stale): DMS's 6 C++ parser/name resolution/control flow 5 analysis handles full C++11, in the ISO-, GNU- and 4 Microsoft variants. It will also parse (and 3 retain) source code containing most preprocessor 2 conditionals. It has an explicit grammar driving 1 the parsing process, unlike GCC or Clang.

More Related questions