Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where can I find the full syntax of C that is necessary to implement a compiler?

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.

like image 570
machine_1 Avatar asked Mar 04 '23 15:03

machine_1


2 Answers

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 )).

like image 198
PSkocik Avatar answered Mar 09 '23 14:03

PSkocik


In the standard. You have to buy it or work with the draft. The Annex A describes lexical grammar of the language.

like image 28
KamilCuk Avatar answered Mar 09 '23 14:03

KamilCuk