r/C_Programming • u/Big-Rub9545 • 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.
13
Upvotes
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.
4
u/SmokeMuch7356 Jul 11 '26
Annex A of latest working draft of the language definition has the grammar, although it's not in a form you can directly plug into a parser generator.
3
u/New_Enthusiasm9053 Jul 11 '26
Microsoft has EBNF for C on their websites somewhere. Unfortunately C is extremely messy.
1
u/mc_pm Jul 11 '26
Like this? https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus-Naur%20form.htm
(it will give you a browser warning, but it's an EDU site, so I wouldn't worry much)
3
u/mikeblas Jul 11 '26
There's an Antlr grammar for C in their official github. I haven't used it, but I've used the T-SQL grammar with good success.
Appendix A of K+R has a BNF grammar, too.
1
u/RealisticDuck1957 Jul 11 '26
This sounds like what the tools lex and yacc (flex and bison on gnu systems) are designed to work with.
1
u/florianist Jul 12 '26
Could this help you? https://github.com/steve-chavez/CParseC