Transcript ppt
Announcements
HW4 is due Monday after the break
We have office hours Mon morning
Lingxun: 9-11am
Ana: 11-1am
If you have questions, email us!
Check your grades in HW Server
Spring 16 CSCI 4430, A Milanova
1
Last Class: Semantic Analysis
Syntax analysis vs. static semantic analysis
Static semantic analysis vs. dynamic
semantic analysis
Languages differ in analysis they perform
C++: static, none (or very few) dynamic checks
Python: dynamic, none (or little) static checks
Java: a mixture of both
Role of semantic analysis: prevent erroneous
run-time behavior!
Spring 16 CSCI 4430, A Milanova
2
Semantic Analysis
Reading: Scott, Chapter 4.1-4.3
3
Today’s Lecture Outline
Static semantic analysis
Attribute grammars
Synthesized and inherited attributes
S-attributed grammars
L-attributed grammars
Spring 16 CSCI 4430, A Milanova
4
Semantic Analyzer
compiler
character stream
scanner
optimizer
token stream
parser
parse trees
semantic analyzer
and intermediate
code generator
modified
intermediate
form
code generator
assembly code
intermediate
form
Semantic analyzer performs static semantic analysis on parse trees and ASTs.
Optimizer performs static semantic analysis on intermediate 3-address code.
Spring 16 CSCI 4430, A Milanova
5
Attribute Grammars:
Foundation for Static Semantic Analysis
Attribute Grammars: generalization of
Context-Free Grammars
Associate meaning with parse trees
Attributes
Each grammar symbol has one or more values called
attributes associated with it. Each parse tree node has
its own attributes; the attribute value carries the
“meaning” of the parse tree rooted at node
Semantic rules
Each grammar production has associated “rule” which
may refer to and compute the values of attributes
Spring 16 CSCI 4430, A Milanova
6
Example: Attribute Grammar to Compute Value
of Expression (denote grammar by AG1)
SE
EE+T |T
TT*F |F
F num
Production
SE
E E1+T
Semantic Rule
print(E.val)
E.val := E1.val + T.val
ET
T T1*F
E.val := T.val
T.val := T1.val * F.val
TF
F num
T.val := F.val
F.val := num.val
val: Attributes
Spring 16 CSCI 4430, A Milanova
7
Example
val: Attributes associated to symbols
Indices used to distinguish between symbols with
same name within same production
E.g., E E1+T
E.val := E1.val+T.val
Attributes of terminals supplied by scanner
Intuitively, A.val holds the value of the expression,
represented by the subtree rooted at symbol A
Fresh attributes are associated with every node in the
parse tree
E.g., num.val is supplied by the scanner
Attributes of symbols + and * are never used
Spring 16 CSCI 4430, A Milanova
8
Example: Decorated parse tree for input
3 * 5 + 2 * 4
SE
E E1+T
ET
T T1*F
TF
F num
S
print(E.val)
E.val := E1.val+T.val
E.val := T.val
T.val := T1.val*F.val
T.val := F.val
F.val := num.val
E 23
E 15 +
T 15
T3
*
F3
num 3
Spring 16 CSCI 4430, A Milanova
T8
T2 *
F5
F2
F4
num 4
num 5 num 2
9
Building an Abstract Syntax Tree (AST)
So far, we talked about parse trees
In fact, compilers use abstract syntax trees
Suitable intermediate representation
Define semantic analyses over ASTs
AST is basis for translation into intermediate code
An AST is an abbreviated parse tree
Operators and keywords do not appear as
leaves, but at the interior node that would have
been their parent
Chains of single productions are collapsed
Spring 16 CSCI 4430, A Milanova
10
Building ASTs for Expressions
Parse tree for 3*5+2*4
Abstract syntax tree (AST)
+
E
+
E
*
T
num:3
T
T
F
num:3
*
T
*
F
F
F
*
num:4
num:2
num:5
num:4
num:5
num:2
Spring 16 CSCI 4430, A Milanova
11
Exercise
Show the parse tree and the AST for 2*3*4
So, how do we construct syntax trees for expressions?
Spring 16 CSCI 4430, A Milanova
12
Attribute Grammar to build AST for
Expression (denote by AG2)
An attribute grammar:
Attribute “nodepointer”
Production
E E1+T
Semantic Rule
E.nptr := mknode(+, E1.nptr, T.nptr)
ET
T T1 *F
E.nptr := T.nptr
T.nptr := mknode(*, T1.nptr, F.nptr)
TF
T.nptr := F.nptr
F num
F.nptr := mkleaf(num, num.val)
Function mknode(op,left,right) creates an operator
node with label op, and two fields containing pointers left,
to left operand and right, to right operand
Function mkleaf(num,num.val) creates a leaf node with
label num, and a field containing the value of the number
13
Constructing ASTs for Expressions
Input:
3 * 5 + 2 * 4
S
+,
E
E
T
T
F
num,3
*
E E1+T
ET
T T1 *F
TF
F num
+
*,
,
*,
T
,
F
E.nptr := mknode(‘+’, E1.nptr, T.nptr)
E.nptr := T.nptr
T.nptr := mknode(‘*’, T1.nptr, F.nptr)
T.nptr := F.nptr
F.nptr := mkleaf(‘num’, num.val)
T
F
num,5 num,2
*
,
F
num,4
Group Exercise
We know that the language L = anbncn is not
context free. It can be captured however with
an attribute grammar. Give an underlying
CFG and a set of attribute rules that
associate an attribute ok with the root R of
each parse tree, such that R.ok is true if and
only if the string corresponding to the fringe
of the tree is in L.
Spring 16 CSCI 4430, A Milanova
15
Question
Consider CFG and attribute grammar:
SE
E E1-T
ET
T num
print(E.val)
E.val := E1.val -T.val
E.val := T.val
T.val := num.val
What is the result for 5-3-2 ?
Answer: 0
Spring 16 CSCI 4430, A Milanova
16
Decorated Parse Tree
SE
E E1-T
ET
T num
Another LR grammar:
Input: 5-3-2.
print(E.val)
E.val := E1.val-T.val
E.val := T.val
T.val := num.val
E0
E2
-
T
2
num 2
E5
-
T5
T3
num 3
num 5
Spring 16 CSCI 4430, A Milanova
17
Another Grammar
T stands for term
TT stands for term_tail
Now, the LL(1) version of same grammar:
E T TT
TT - T TT
TT ε
T num
Goal: construct an attribute grammar that
computes the value of an expression
Values must be computed “normally”, i.e.,
5-3-2 must be evaluated as (5-3)-2, not as
5-(3-2)
Spring 16 CSCI 4430, A Milanova
18
Question
What happens if we write an attribute
grammar in the “bottom-up” style we used so
far?
E T TT
TT - T TT
TT ε
T num
Spring 16 CSCI 4430, A Milanova
19
Attribute Grammar to Compute Value of
Expressions (denote by AG3)
E T TT
Production
E T TT
TT -T TT | ε
T num
Semantic Rules
(1) TT.sub := T.val
(2) E.val := TT.val
TT - T TT1 (1) TT1.sub:= TT.sub - T.val (2) TT.val := TT1.val
TT ε
(1) TT.val := TT.sub
T num
(1) T.val := num.val
Spring 16 CSCI 4430, A Milanova
(provided by scanner)
20
Attribute Flow
Attribute TT1.sub: computed based on parent
TT and sibling T: TT.sub - T.val
15
…
TT
24
T
…
3
TT1
21
15
…
TT
E.g., 25 – 1 - 3 - 6
TT holds subtotal 24 (for 25 – 1, computed so far)
T holds value 3 (i.e., the value of next term)
TT1 gets subtotal 21 (for 25 – 1 – 3)
Passed down the tree of TT1 to next TT on chain
Eventually, we hit TT ε and value gets subtotal 15
Value 15 is passed back up
21
Attribute Flow
Attribute .val carries the total value
Attribute .sub is the subtotal carried from left
Rules for nonterminals E, T do not perform
computation
No need for .sub attribute
.val attribute is carried to the right
In E T TT : val of T is passed right to TT
In TT -T TT1 : val of T is passed right to TT1 . It is
the right operand in the subtraction that computes the
subtotal of TT1.
Spring 16 CSCI 4430, A Milanova
22
Attribute Flow
Rules for nonterminal TT do perform
computation
TT needs to carry subtotal in .sub
E.g., in TT - T TT1 the subtotal of TT1 is computed
by subtracting the value of T from the subtotal of TT
Spring 16 CSCI 4430, A Milanova
23
Synthesized and Inherited Attributes
Synthesized attributes
Attribute value computed from attributes of
descendants in parse tree, and/or attributes of self
E.g., attributes val in AG1, val in AG3
E.g., attributes nptr in AG2
Inherited attributes
Attribute value computed from attributes of parent
in tree and/or attributes of siblings in tree
E.g., attributes sub in AG3
In order to compute value “normally” we needed to
pass sub down the tree (sub is inherited attribute).
Spring 16 CSCI 4430, A Milanova
24
S-attributed Grammars
An attribute grammar for which all attributes
are synthesized is said to be S-attributed
Arguments of rules are attributes of symbols from
the production right-hand-side
Result is placed in the attribute of the symbol on
the left-hand-side of the production
I.e., attributes of children in parse tree
I.e., computes attribute of parent in parse tree
I.e., attribute values depend only on descendants
in tree. They do not depend on parents or
siblings in tree!
25
Questions
Can you give examples of S-attributed
grammars?
Answer: AG1 and AG2
How can we evaluate S-attributed
grammars?
In other words, in what order do we visit the
nodes of the parse tree?
Answer: bottom-up
Spring 16 CSCI 4430, A Milanova
26
L-attributed Grammar
An attribute grammar is L-attributed if each
inherited attribute of Xj on the right-hand-side
of A X1 X2 …Xj-1Xj…Xn depends only on
(1) the attributes of symbols to the left of Xj: X1,
X2 ,…, Xj-1
(2) the inherited attributes of A
Spring 16 CSCI 4430, A Milanova
27
Questions
Can you give examples of L-attributed
grammars?
Answer: AG3
How can we evaluate L-attributed grammars?
I.e., in what order do we visit the nodes of the
parse tree?
Answer: top-down
Spring 16 CSCI 4430, A Milanova
28
Question
An attribute grammar is L-attributed if each
inherited attribute of Xj on the right-hand-side
of A X1 X2 …Xj-1Xj…Xn depends only on
(1) the attributes of symbols to the left of Xj: X1,
X2 ,…, Xj-1
(2) the inherited attributes of A
Why the restriction on siblings? Why not
allow dependence on siblings to the right of
Xj, e.g., Xj+1, etc.?
29
Recursive Descent (partial sketch)
S E $$
E T TT
TT - T TT | ε
T num
num S()
case lookahead() of
num: val = E(); match($$); return val
otherwise PARSE_ERROR
num E()
case lookahead() of
num: sub = T(); val = TT(sub); return val
otherwise PARSE_ERROR
num TT(num sub)
case lookahead() of
- : match(‘-’); Tval = T(); val = TT(sub -Tval); return val
$$:val = sub; return val
otherwise: PARSE_ERROR
30
Evaluating Attributes and Attribute Flow
S-attributed grammars
A very special case of attribute grammars
The most important case in practice
Can be evaluated on-the-fly during a bottom-up
(LR) parse
L-attributed grammars
A proper superset of S-attributed grammars
Each S-attributed grammar is also L-attributed
because restriction applies only to inherited attributes
Can be evaluated on-the-fly during a top-down
(LL) parse
31
Semantic Analysis in Perspective
Based on the theory of attribute grammars
In compilers
E.g., translation into an AST, type checking,
translation into intermediate code
Semantic analysis interleaved with LR parsing
Sometimes analysis is done separately on the
AST
Spring 16 CSCI 4430, A Milanova
32
Semantic Analysis in Perspective
Based on the theory of attribute grammars
In software tools (e.g., Eclipse plug-ins)
Language extensions
Pluggable types: e.g., non-Null types
Catch null-pointer bugs or verify the absence of such bugs
Implement domain specific languages
Other static checkers and debuggers
Reverse engineering tools
Analysis is done on the AST
Spring 16 CSCI 4430, A Milanova
33