Unit 5: Semantic Analysis Parse Trees Parse this:

advertisement
Unit 5: Semantic Analysis
SI 413: Programming Languages and Implementation
Dist. Visit. Prof. Carl Albing
USNA
Fall 2015
SI 413 (USNA)
Fall 2015
Parse Trees
●
Parse this:
x := (5 + 3) * 2; x ­ 7;
●
Show the parse tree
SI 413 (USNA)
Fall 2015
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
●
Same program
Differences from
parse tree?
–
–
–
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
●
Same program
Differences from
parse tree?
–
simpler
–
–
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
●
Same program
Differences from
parse tree?
–
simpler
–
–
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
●
Same program
Differences from
parse tree?
–
simpler
–
operator
–
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
●
Same program
Differences from
parse tree?
–
simpler
operator
–
sequence
–
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
Friday's Lab AST
Stmt: assign
Expr: Id: x
Expr: OPM:*
Expr: OPA:+
Stmt: print
Expr: Num: 2
...
...
just more decoration
SI 413 (USNA)
Fall 2015
Abstract Syntax Trees
●
ASTs – not about the syntax
–
abstracted away from the syntax
●
●
●
remove the syntactic details
leaves only the semantics
Ordering – Implicit Semantics
–
made explicit in the AST
–
how would you show if/then/else? while?
SI 413 (USNA)
Fall 2015
Abstract Syntax Trees
●
Are ASTs Language Independent?
(discuss/vote)
SI 413 (USNA)
Fall 2015
Abstract Syntax Trees
●
Are ASTs Language Independent?
●
Yes and No
SI 413 (USNA)
Fall 2015
Abstract Syntax Trees
●
Are ASTs Language Independent?
●
Yes and No
●
Yes
–
different languages, same AST
SI 413 (USNA)
Fall 2015
Abstract Syntax Trees
●
Are ASTs Language Independent?
●
Yes and No
●
Yes
–
●
different languages, same AST
No
–
specific semantics in certain languages
SI 413 (USNA)
Fall 2015
Abstract Syntax Tree
●
Why ASTs?
●
What are they good for?
–
semantic error detection
–
assist in code generation
●
the source of code gen.
–
–
●
what code would you generate for “ares → bres” ?
grammar vs. meaning
pre-code gen. optimization
SI 413 (USNA)
Fall 2015
Static Type Checking
●
Consider this string:
(7 > 2) + 3;
●
In some languages (not C), an error!
●
what is the error?
●
Where/when can this be identified?
●
(How) Can the AST help?
SI 413 (USNA)
Fall 2015
Static Type Checking
●
Consider this string:
x = 6 > 3; x * 12;
●
Is this an error? where?
●
(How) Can the AST help?
Can you draw the AST?
SI 413 (USNA)
Fall 2015
Static Type Checking
●
Consider this string:
x = 6 > 3; x * 12;
●
Is this an error? where?
●
(How) Can the AST help?
=
x
>
6
*
3
x
SI 413 (USNA)
Fall 2015
12
Static Type Checking
●
Consider this string:
x = 6 > 3; x * 12;
●
Is this an error? where?
●
Need to know the type of x
●
Statically-typed languages
●
●
c.f. dynamic typing
=
x
>
*
Errors are a good thing
●
prevent runtime problems
6
3
x
SI 413 (USNA)
Fall 2015
Static Type Checking
●
Use the AST
●
Each node in AST
–
–
●
a type
type may be “void”
Analyze the type...
SI 413 (USNA)
Fall 2015
Static Type Checking
●
Analyze the type
–
–
tree traversal
over a complex sequence of operations
float*double + int/(double+atoi(string))
–
–
promotion is semantics
needed for error checking, code generation
SI 413 (USNA)
Fall 2015
12
Abstract Syntax Trees
●
Do you know what one is?
●
Can you draw one?
●
Do you know why we need them?
●
What is the relationship between:
–
grammar – parse tree – AST ?
●
Can you describe static type checking?
●
AST and code generation
–
coming soon to a lab near you!
SI 413 (USNA)
Fall 2015
Download