I'm looking for the C grammar in GCC source code, more specifically for the grammar in the yacc/bison form.
-
6That's more of a statement than a question? (I decided to reciprocate by phrasing my statement as a question. fun!) – Ed James Apr 27 '10 at 12:30
-
Did you try googling? I searched for "c yacc grammar" and came up with http://www.lysator.liu.se/c/ANSI-C-grammar-y.html among many other hits. – Apr 27 '10 at 12:30
-
@Neil, perhaps it's not what Hurkai meant, but GCC has some C extensions, and generic C grammar wouldn't precisely fit it. – P Shved Apr 27 '10 at 12:41
-
I've looked into C-standard grammar already, but its old compared to the grammar used in gcc, that's why I'm looking for it. – Haruki Apr 27 '10 at 14:33
-
1It's just imparsable ... – bmargulies May 01 '10 at 19:38
7 Answers
Found the C grammar in Yacc specification in the GCC version 3.3 in the file "c-parse.y"

- 674
- 1
- 9
- 24
You will not find a C grammar yacc/bison file within the current GCC source code. It was done in the past, before the egcs fork stuff. I cannot give you the exact version and location, but i can tell you that it should be in the 2.x release
The current version of GCC has its own C parser

- 16,555
- 7
- 56
- 102
-
So there is a grammar specification in GCC source code, right? The one to be parsed with GCC own C parser. Do you know in which file i find it? – Haruki Apr 27 '10 at 13:17
-
@Hurkai you can find it in comments to subroutines that perform parsing :-) – P Shved Apr 27 '10 at 13:22
GCC's g++ switched from a yacc (bison) based parser years ago (probably at least 5 years). They started using a recursive decent parser because C++ is difficult in yacc.
After using this parser in for C++ for several years they switched C to parsing using recursive decent as well.
You will have to go back several versions to locate the grammar in bison format, but it is out there. You should try google's code search with
gcc yyparse
Update: Google Code Search Shutdown in 2012
http://en.wikipedia.org/wiki/Google_Code_Search
Old: http://yaxx.googlecode.com/svn/branches/yaxx-proc/gcc-3.4.0/gcc/c-parse.y
to find a version of gcc that has it and then you should be able to find the yacc/bison source file in there. It will be old, though.

- 54,294
- 25
- 151
- 185

- 12,054
- 27
- 42
-
6I think you meant _descent_ , though the parser is _decent_ enough :) – Tim Post Apr 29 '10 at 08:06
GCC of version 4.3 did not contain explicitly written C grammar. Parsing and semantical analysis were performed simultaneously, without presenting syntax tree as a separate data structure.
Information source: I read the GCC source code.

- 96,026
- 17
- 121
- 165
GCC doesn't use a generated parser; its parser is a hand-written recursive-descent parser.

- 476,176
- 80
- 629
- 1,111

- 363,080
- 75
- 446
- 653
-
2
-
1@Jack: Interesting! Can you point me to any documentation? I know they switched *from* a generated parser *to* the new hand-written one for performance and (more importantly) maintainability reasons, probably somewhere around the 3.4–4.2 timeframe (I don't precisely remember). What were the reasons for switching back and which parser generator do they use? – Jörg W Mittag Jan 22 '13 at 01:34
The C grammar can be found in comments in c-parser.c file in GCC sources. It is not a yacc/bison though as it has already been said.

- 2,092
- 16
- 22