Using the grammar shown below, draw parse trees for:
1.) x = x + a - 1;
2.) a = b * c / d;
3.) i = i + j * k - 3;
Program:
int main() {
Declarations
Statements
}
Declarations:
{Declaration}
Declaration:
Type Identifier [[Integer]] {, Identifier [[Integer]]};
Type:
int | bool | float | char
Statements:
{Statement}
Statement:
; | Block | Assignment | If Statement | While Statement
Block:
{Statements}
Assignment:
Identifier [[Expression]] = Expression;
If Statement:
if (Expression) Statement [else Statement]
While Statement:
while (Expression) Statement
Expression:
Conjunction {1 | Conjunction}
Conjunction:
Equality {&& Equality}
Equality:
Relation [EquOp Relation]
EquOp:
== | != | < | <= | > | >=
Relation:
Addition [RelOp Addition]
RelOp:
< | <= | > | >=
Addition:
Term {AddOp Term}
AddOp:
+ | -
Term:
Factor {MulOp Factor}
MulOp:
* | / | %
Factor:
[UnaryOp] Primary
UnaryOp:
- | !
Primary:
+ Identifier [[Expression]] | Literal | (Expression) | (Type(Expression))
Identifier:
Letter {Letter | Digit}
Letter:
a | b | ... | i | z | A | B | ... | Z
Digit:
0 | 1 | ... | 9
Literal:
Integer | Boolean | Float | Char
Integer:
Digit {Digit}
Boolean:
true | false
Float:
Integer . Integer
Char:
ASCIIChar