My aim is not to write a C compiler, however I do require the full syntax of the C programming language. This will allow me to write program(s) to format, manage, and analyze C programs and libraries more easily. To achieve that, I have no option other than to get my hands on the entire syntax of the language.
The syntax shall clearly state what is valid and what is not valid. Consider the following line of code:
int (x) = 0;
A C programmer glancing at this statement might hesitate about its validity and until he tries to compile it, he may not know that it is actually valid C. Of course, it is easy to tell that it is equivalent to int x = 0;
and that the enclosing parenthesis around the x
are redundant, but its not clear to a programmer who sees it for the first time whether it is allowed or not.
This is the level of detail that I require regarding the full syntax of the language. It must be sufficient to an implementer to use it to write a compiler that can compile any C code, even though my intention is not to write a compiler yet full syntax details are required for my project.
The C standard lists the complete grammar at the end.
At http://www.lysator.liu.se/c/ANSI-C-grammar-y.html it's in a form compilable by yacc/bison.
int (x) = 0;
is valid because when you combine
(6.7) declaration:
declaration-specifiers init-declarator-listopt ;
static_assert-declaration
(6.7) declaration-specifiers:
storage-class-specifier declaration-specifiersopt
type-specifier declaration-specifiersopt
type-qualifier declaration-specifiersopt
function-specifier declaration-specifiersopt
alignment-specifier declaration-specifiersopt
(6.7) init-declarator-list:
init-declarator
init-declarator-list , init-declarator
(6.7) init-declarator:
declarator
declarator = initializer
with
(6.7.6) declarator:
pointeropt direct-declarator
(6.7.6) direct-declarator:
identifier
( declarator )
direct-declarator [ type-qualifier-listopt assignment-expressionopt ]
direct-declarator [ static type-qualifier-listopt assignment-expression ]
direct-declarator [ type-qualifier-list static assignment-expression ]
direct-declarator [ type-qualifier-listopt * ]
direct-declarator ( parameter-type-list )
direct-declarator ( identifier-listopt )
then x
in int x = 0;
is direct-declarator
and the grammar allows parentheses around it (production direct-declarator ::= ( declarator )
).
In the standard. You have to buy it or work with the draft. The Annex A describes lexical grammar of the language.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With