Exercise 2:

Simplified grammar for C declarations
A declaration specifies the identifier, type, and other aspects of some element, such as a variables or function. Declarations in C have a very rich syntax, but we only consider a subset.

The set of tokens is {\{IDENT, expr, ;, ,, =, (, ), [, ], {, }, *, enum, struct, union, type, TYPE_QUALIFIER, STORAGE_CLASS}\}. The token IDENT represents any identifier (such as a variable or function name), that is, a non-empty sequence of alphanumeric characters and underscore, not starting by a digit. The token expr represents an arbitrary expression, such as 3+4 or i++. Thus, we do not need to parse expressions, as we already dispose of this ‘token’.

A declaration has three parts: the declaration specifiers, the list of declarators, and a terminating semicolon “;”.

Declaration specifiers: they are keywords or fragments of code that specify some property about the declared variables. The declaration specifiers part of a declaration consists of one or more declaration specifiers. Semantically speaking, not all of them can appear together or in any order, but we defer this inspection for the semantical analysis. They can be classified as follows: List of declarators: it consists of one or more declarators separated by commas. A declarator is the part of a declaration that specifies the name that is to be introduced into the program. Declarators can be initialized, which means that they can be followed by the symbol = and an initializer. An initializer is either an expression or an initializer list, which is a list of one or more initializers delimited by curly brackets and separated by commas. A declarator can be any of the following: Examples of correct declarators are: Remarks about the AST construction:
Authors: Nil Mamano / Documentation:
To be able to submit you need to either log in, register, or become a guest.