source code
int i = 0; /* some comment */
int j = 0;
while (i < 10) {
System.out.println(j);
i = i + 1;
j = j + 2*i+1;
}
list of tokens
INT, Identifier("i"), EQUAL, IntNumeral(0), SEMICOLON, INT, Identifier("j"), EQUAL, ...
WHILE, OPENPAREN, Identifier("i"), LESSTHAN, IntNumeral(10), CLOSEDPAREN, OPENBRACE,
Identifier("System"), DOT, Identifier("out"), ...
concrete syntax tree
Declaration(INT, Identifier("i"), EQUAL, InitialExpression(IntConst(IntNumeral(0)),
Declaration(INT, Identifier("j"), EQUAL, InitialExpression(IntConst(IntNumeral(0)),
WhileStatememt(WHILE, OPENPAREN, LessThanExpr(Identifier("i"),LESSTHAN,IntNumeral(10)),
CLOSEDPAREN,
StatementBlock(OPENBRACE,
Statement(MethodCall(DotExpr(Identifier("System"),DOT,Identifier("out")),DOT,
Identifier("println"),
OPENPAREN,ArgumentList(Identifier("j")),CLOSEDPAREN),
SEMICOLON,
...
)
)
)
)
abstract syntax tree
Declaration("i",IntType,Some(IntConst(0)),
Declaration("j",IntType,Some(IntConst(0)),
WhileStatememt(LessThan(Ident("i"),IntConst(10)),
StatementBlock(
MethodCall(DotExpr(Ident("System"),"out"),
"println",
List(Ident("j"))),
Assignment("i", Plus(Ident("i"),IntConst(1))),
Assignment("j", Plus(Ident("j"),
Times(IntConst(2),Ident("i"),
IntConst(1))
)
)
)
)
resolved tree and symbol table
Declaration("i",IntType,Some(IntConst(0)),
Declaration("j",IntType,Some(IntConst(0)),
WhileStatememt(LessThan(Ident("i"),IntConst(10)),
StatementBlock(
MethodCall(DotExpr(Ident("System"),"out"),
"println",
List(Ident("j"))),
Assignment("i", TYPE(Plus(TYPE(Ident("i"),INT),IntConst(1))),INT),
Assignment("j", TYPE(Plus(TYPE(Ident("j"),INT),
TYPE(Times(IntConst(2),TYPE(Ident("i"),INT),INT),
TYPE(IntConst(1),INT)),INT)
)
)
)
)
i -> (LocalVar,INT)
j -> (LocalVar,INT)
source- and taget-independent format
control-flow graph containing three-address code instructions
translated program