Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find standard BNF or YACC grammar for C++ language?

Tags:

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

like image 809
Kevin Yu Avatar asked Mar 05 '09 03:03

Kevin Yu


People also ask

What is BNF in compiler design?

BNF stands for Backus-Naur Form. It is used to write a formal representation of a context-free grammar. It is also used to describe the syntax of a programming language.

What is BNF explain BNF using suitable example?

In computer science, Backus–Naur form (/ˌbækəs ˈnaʊər/) or Backus normal form (BNF) is a metasyntax notation for context-free grammars, often used to describe the syntax of languages used in computing, such as computer programming languages, document formats, instruction sets and communication protocols.


1 Answers

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.

like image 128
Jared Oberhaus Avatar answered Nov 05 '22 22:11

Jared Oberhaus