Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context-free grammar for C

Tags:

I'm working on a parser for C. I'm trying to find a list of all of the context-free derivations for C. Ideally it would be in BNF or similar. I'm sure such a thing is out there, but googling around hasn't given me much.

Reading the source code for existing parsers/compilers has proven to be far more confusing than helpful, as most that I've found are much more ambitious and complicated than the one I'm building.

like image 761
limp_chimp Avatar asked Mar 19 '13 06:03

limp_chimp


People also ask

What is context-free grammar in C?

A Context-Free Grammar is a set of rules that define the syntax of a language — what is and is not a valid sequence of “tokens”. Programming languages, for example, are context-free grammars — a compiler reads your code to make sure it conforms to specific rules and informs you of any errors.

Is CFG a C language?

C language is CSL These preprocessors are inevitably written by hand (they are not based on a theoretic foundation like regular expressions or context-free grammars). C and C++ lexers require lexical feedback to differentiate between typedef names and identifiers.

What is context-free grammar explain with examples?

A context free grammar (CFG) is a forma grammar which is used to generate all the possible patterns of strings in a given formal language. It is defined as four tuples − G=(V,T,P,S) G is a grammar, which consists of a set of production rules. It is used to generate the strings of a language.

Is C's syntax structure context-free?

The structure of the language (syntax): this is context free as you do not need to know the surroundings to figure out what is an identifier and what is a function.


2 Answers

This is an ANSI-C grammar, updated to C11 standard. Should serve your purpose.

http://www.quut.com/c/ANSI-C-grammar-y.html

like image 51
xji Avatar answered Sep 19 '22 03:09

xji


You could always use Annex A of the C11 standard itself. The freely available draft standard will work for your purposes, at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf .

like image 32
bk. Avatar answered Sep 20 '22 03:09

bk.