CSE 3341: Context-Free Grammar for simpleC, Project 3

<program> ::= <decl-list> <stmt-list>

<decl-list> ::= ε <decl> <decl-list>

<decl> ::= int id ; | int id = <expr>;

<stmt-list> ::= ε <stmt> <stmt-list>

<stmt> ::= <expr> ; | ε ; | read id ; | print <expr> ; | if ( <expr> ) <stmt> | if ( <expr> ) <stmt> else <stmt> | { <stmt-list> } 

                     |  while ( <expr> ) <stmt>  

<expr> ::= const | id | ( <expr> ) | <binary-expr> | <unary-expr>

<binary-expr> ::= <expr> + <expr> | <expr> - <expr> | <expr> * <expr> | <expr> / <expr> | <expr> % <expr> | id = <expr> 

                                     |  <expr> < <expr> | <expr> <= <expr> | <expr> > <expr> | <expr> >= <expr> | <expr> == <expr> | <expr> != <expr> 

                                    |  <expr> && <expr> | <expr> || <expr>  

<unary-expr> ::= - <expr> | ! <expr>

For simplicity, this grammar is ambiguous. The parser implementation should ensure that the precedence and associativity of these operators match those of the corresponding C operators.