Context-free grammar for C
Asked Answered
T

2

27

I'm working on a parser for C. I'm trying to find a list of all of the context-free derivations for C. Ideally it would be in BNF or similar. I'm sure such a thing is out there, but googling around hasn't given me much.

Reading the source code for existing parsers/compilers has proven to be far more confusing than helpful, as most that I've found are much more ambitious and complicated than the one I'm building.

Treadmill answered 19/3, 2013 at 6:0 Comment(7)
yacc grammar for C: lysator.liu.se/c/ANSI-C-grammar-y.htmlBusboy
I can confirm this is good. I built a flex/bison parser out of the LEX/YACC grammars from that page, just the other week.Ricoricochet
@CongXu, looks good, the only disadvantage that it has that I see that it is only historical C and neither C99 nor C11.Godliman
Am I misremembering, or doesn't the presence of typedef in particular mean that you can't have a context-free grammar in C?Oocyte
@JonathanLeffler no, you can still have a context free grammar by parsing type name in declarations as a terminal identifier and performing the check at semantic level. In fact, Clang does exactly that.Circum
@MortenJensen May I ask you about the preprocessor you used to build the parser? Because the yacc grammar for C assumes that translation phases 1..5 have already been completed.Melanie
@ThuyNguyen just the GNU preprocessor cpp. Mcpp is another alternative with a more permissive licenseRicoricochet
E
13

You could always use Annex A of the C11 standard itself. The freely available draft standard will work for your purposes, at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .

Earshot answered 27/4, 2013 at 2:28 Comment(1)
Here is a link to the page: open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf#page=476.Ronnaronnholm
B
15

This is an ANSI-C grammar, updated to C11 standard. Should serve your purpose.

http://www.quut.com/c/ANSI-C-grammar-y.html

Bleachers answered 21/11, 2014 at 10:22 Comment(0)
E
13

You could always use Annex A of the C11 standard itself. The freely available draft standard will work for your purposes, at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .

Earshot answered 27/4, 2013 at 2:28 Comment(1)
Here is a link to the page: open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf#page=476.Ronnaronnholm

© 2022 - 2024 — McMap. All rights reserved.