4

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.

machine_1
  • 4,266
  • 2
  • 21
  • 42
  • 2
    Requests for links are off topic, but what you want is the [C language standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). – dbush Jan 26 '19 at 23:26
  • @dbush Is it mentioned anywhere in the standard that you can write something like `int (x) = 0;` ? – machine_1 Jan 26 '19 at 23:32
  • I'm not sure this is complete, but I found https://cs.wmich.edu/~gupta/teaching/cs4850/sumII06/The%20syntax%20of%20C%20in%20Backus-Naur%20form.htm by searching google for "BNF grammar for C". You can always download a C compiler source and there will probably be input files for lex already there. As others have said, the full draft will still be needed... – Bob Shaffer Jan 26 '19 at 23:34
  • @machine_1 Section 6.7.6, specifically paragraph 6. – dbush Jan 26 '19 at 23:34

2 Answers2

5

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

Petr Skocik
  • 58,047
  • 6
  • 95
  • 142
1

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

KamilCuk
  • 120,984
  • 8
  • 59
  • 111