Exercise 3:

Simplified grammar for C statements
The set of tokens is {\{IDENT, expr, decl, if, else, switch, while, do, for, goto, continue, break, return, case, default, :, ;, {, }, (, ) }\}. The token IDENT refers to an identifier (such as a label name), that is, a non-empty sequence of alphanumeric characters and underscore, not starting by a digit. We also dispose of tokens expr and decl, which denote expressions and declarations respectively. Thus, instead of parsing those, we use these ‘tokens’. Note that the expressions and declarations represented by them are not empty.

The most elemental statements are: nothing, an expression, or a list of declarations and statements enclosed in curly brackets ({ and }).

All statements must end in a semicolon (;), except for the aforementioned list in curly brackets, which does not need a semicolon after the closing bracket. A statement does not need to have its own ending semicolon if it ends in another statement, because that statement will have its own semicolon or be a block {...}, which does not require one. For instance, the statement “expr;” has an ending semicolon, so in the if statement “if(expr) expr;”, it is not required to add another semicolon.

A statement can be labeled, in which case it is preceded by a label identifier (IDENT) and the token “:”. Alternatively, instead of a label identifier we can have the keyword case followed by an expression, or the keyword default (these options are only semantically valid in the context of a switch statement, but this doesn’t have to be considered by the parser).

There are also the following types of statements: Moreover, the statements continue and break consist of just the keywords continue and break, respectively.

Remarks about AST construction:
Authors: Nil Mamano / Documentation:
To be able to submit you need to either log in, register, or become a guest.