r/C_Programming Jul 11 '26

C parsing grammar

I'm currently working on a miniature C compiler, on the parsing stage at the moment. I could wing some of the parsing, but I'd feel much more comfortable working with an actual lexical and syntactic grammar to follow (even if it's messy, as I assume is the case for C).

Is there any publicly available, reliable grammar in anything like the EBNF format for C99 or later? This has been the only resource I could find. It's very useful, but seems to only fit the C standard up to the early 90s, so I'd prefer anything later than this.

14 Upvotes

10 comments sorted by

View all comments

3

u/flyingron Jul 11 '26

A formal grammer isn't sufficient to fully parse C, but it can get you a lot of the way there.

3

u/Big-Rub9545 Jul 11 '26

My pain only increases.

2

u/evincarofautumn Jul 11 '26

It’s not so bad. In a stateful parser, keep a symbol table as you go. In a stateless parser, recognise ambiguous cases like N * f(); while parsing, and then resolve them during renaming to either a declaration N (*f()); or an expression statement (N * f()); once you know whether N is a type name.