getting to know decaf
DESCRIPTION
Getting to Know Decaf. Goals for Today: Get acquainted with the Decaf language Get experience with ASTs. The Decaf Language. Activity #1: With a partner: Using the Decaf language specification handout, and comparing with either Java or C++: - PowerPoint PPT PresentationTRANSCRIPT
Getting to Know Decaf
Goals for Today:• Get acquainted with the Decaf language• Get experience with ASTs
The Decaf Language
Activity #1:
With a partner:
Using the Decaf language specification handout, and comparing with either Java or C++:
1. Create a list of similarities between Decaf and Java/C++
2. Create a list of differences between Decaf and Java/C++
Be ready to discuss your results, and also turn in your written listings with your names on it at the end of the activity.
Timing:
20 minutes – reading and list creation
15 minutes – reporting out – charts on the board, discussion
Abstract Syntax Trees
• What are they? • Give example for some strings of the
grammar:
E -> E + T | E – T | T
T -> T * a | T / a | a
Building an AST during ParsingS -> E { $$ = $1; root = $$; }E -> E + T { $$ = makenode(‘+’, $1, $3);} // E is $1, - is $2, T is $3E -> E - T { $$ = makenode(‘-’, $1, $3);}E -> T { $$ = $1;} // $$ is top of stackT -> ( E ) { $$ = $2;}T -> id { $$ = makeleaf(‘idnode’, $1);}T -> num { $$ = makeleaf(‘numnode’, $1);}
Consider parsing 4 + ( x - y )
state semantic value
Parsing Stack
S
4num S
Getting to know the Decaf Compiler AST Representation
Consider the Decaf program:
void main() {
Print("hello world");
}
The Decaf Parser Output
Program:
1 FnDecl:
(return type) Type: void
1 Identifier: main
(body) StmtBlock:
PrintStmt:
2 (args) StringConstant: "hello world"
Practice with Decaf ASTsActivity 2: With a partner, draw an AST for the Decaf program using the grammar from the Decaf specification language handout:
class Cow {
int height;
bool isSpotted;
void Moo() {
Print ( this.height, " ", isSpotted, "\n" );
}
}
void main() {
Cow betsy;
betsy = New(Cow);
betsy.Moo();
}
The Decaf Parser Output• Program: • 1 ClassDecl: • 1 Identifier: Cow• 2 VarDecl: • Type: int• 2 Identifier: height• 3 VarDecl: • Type: bool• 3 Identifier: isSpotted• 4 FnDecl: • (return type) Type: void• 4 Identifier: Moo• (body) StmtBlock: • PrintStmt: • 5 (args) FieldAccess: • 5 This: • 5 Identifier: height• 5 (args) StringConstant: " "• 5 (args) FieldAccess: • 5 Identifier: isSpotted• 5 (args) StringConstant: "\n"• 10 FnDecl: • (return type) Type: void• 10 Identifier: main• (body) StmtBlock: • 11 VarDecl: • 11 NamedType: • 11 Identifier: Cow• 11 Identifier: betsy• 12 AssignExpr: • 12 FieldAccess: • 12 Identifier: betsy• 12 Operator: =• 12 NewExpr: • 12 NamedType: • 12 Identifier: Cow• 13 Call: • 13 FieldAccess: • 13 Identifier: betsy• 13 Identifier: Moo