Professional Documents
Culture Documents
8
8
http://www.labouseur.com/courses/compilers/compilers/alan/
3/7/15
Semantic Analysis
Beyond context free grammar
Is
Is
Is
Is
Choices
Attribute Grammar
Augment context free grammar with rules
Each grammar symbol has an associated set of attributes
The attributes are evaluated by the semantic rules
attached to the productions
For example
Semantic rules: The rules you added to yacc input file for
3/7/15
Attribute Grammar
Example
Expression evaluation
LE
print(E.val)
E E1 + T
E.val := E1.val + T.val
ET
E.val := T.val
T T1 * F
T.val := T1.val * F.val
TF
T.val := F.val
F(E)
F.val := E.val
F digit
F.val := digit.lexval
By the way, now you see why we do not like to do left recursion elimination
and left factoring. It will be more difficult to write attribute rules for an
incomprehensible grammar.
5
Input: 2 * 3 + 4
E E.val = 10
E.val = 6 E
T.val = 6 T
T.val = 2 T
F.val = 2 F
digit = 2 digit
T
F
F.val = 3
digit = 3
digit
T.val = 4
F.val = 4
digit
LE
E E1 + T
E T
T T1 * F
TF
F(E)
F digit
digit = 4
print(E.val)
E.val := E1.val + T.val
E.val := T.val
T.val := T1.val * F.val
T.val := F.val
T.val := E.val
F.val := digit.lexval
3/7/15
S
(T)
a! F
S (T),
TFTN|N
F a! | b!, N num
Input: (a! b! a! 5 3 2 6)
b!
N
N
a!
S (T)
TN
T F T1 N
F a!
F b!
N num
5
print (T.list)
T.list := [N.val]
if (F.op = a) T.list := append (T1.list, N.val)
if (F.op = b) T.list := removeLastN (T1.list, N.val)
F.op := a
F.op := b
Remove last N.val
N.val := num
elements from T .list
1
(T)
a! F
b!
S (T)
T F T1 N
TN
F a!
F b!
N num
N
N
a!
print (T.val)
5
T.val := T1.val F.op N.val
T.val := N.val
F.op := +
Attributed grammar defines the
F.op := *
meaning for the production rules
N.val := num
Which gives the meaning for the
input program
8
3/7/15
S AB
A CdE
AF
B EF
S.addr = new-node(S)
S.child = A.addr; A.sib = B.addr;
A.addr = new-node(A); d.addr = new-node(d);
A.child = C.addr; C.sib = d.addr; d.sib = E.addr;
A.addr = new-node(A)
A.child = F.addr;
B.addr = new-node(B)
B.child = E.addr; E.sib = F.addr
Attribute Grammar
Two types of attributes
Synthesized attributes
from bottom up
The value of the parent is defined
on the values of the children
Inherited attributes
Attribute rules
Only reference local information
only refer to symbols in the corresponding production
10
3/7/15
S-Attribute Grammar
Only has synthesized attributes
Suitable for shift-reduce parsing
Example
11
digit
Inherited Attributes
Ls attribute value type depends
on left sibling Ts type
Example
Adding type to symbol table
D TL
L.type := T.type
T int
T.type := integer
T real
T.type := real
L L1, id
L id
Attribute type is
still synthesized
3/7/15
Dependency Graph
Dependency specifies
the evaluation order
T.type = int
D TL
T int
T real
L L1, id
L id
L.type := T.type
T.type := integer
T.type := real
L1.type := L.type, addtype (id.entry, L,type)
addtype (id.entry, L.type)
L1 L1.type = T.type
int
L2 L2.type = L1.type ,
L3.type = L2.type L
3
id addtype(id.entry, L1.type)
id addtype(id.entry, L2.type)
id addtype(id.entry, L3.type)
13
Dependency Graph
Evaluation based on the dependencies
Circularity check
Dependency graph should be acyclic
Can we do better?
L-attributed grammar
Parser stack based technique
Marker nonterminals (not covered)
14
3/7/15
Left-Attributed Grammar
L-attributed grammar
May have synthesized attributes
Has inherited attributes
For any production: A X1 X2 Xn
D T L
T int
T real
L L1, id
L id
L.type := T.type
T.type := integer
T.type := real
L1.type := L.type, addtype (id.entry, L.type)
addtype (id.entry, L.type)
15
L-Attributed Grammar
D
T.type = int
L1 L1.type = T.type
int
L2 L2.type = L1.type ,
L3.type = L2.type L
3
id
addtype(id.entry, L2.type)
16
id addtype(id.entry, L3.type)
id addtype(id.entry, L1.type)
3/7/15
T.type = real
T is kept in the stack
till the D reduction
evaluated
already evaluated?
From stack
Input
Stack
Action
p, q, r $
real
T real
p, q, r $
shift
, q, r $
Tp
L id
, q, r $
TL
shift
q, r $
TL,
shift
,r$
TL,q
L L, id
,r$
TL
shift
r$
TL,
shift
TL,r
L L, id
TL
D TL
accept
real p, q, r $
T.type = real
L.type := T.type
T.type := integer
T.type := real
L1.type := L.type, addtype(, L.type)
addtype (, L.type)
shift
L.type = T.type
T.type = integer
T.type = real
L1.type = L.type, addtype (id.entry, L.type)
addtype (id.entry, L.type)
18
3/7/15
Inherited Attribute
Example: not an L-attributed grammar Depend on right sibling;
No longer L-attributed
DL:T
L L1, id
L id
T int
T real
L.type = T.type
L1.type = L.type, addtype (id.entry, L.type)
addtype (id.entry, L.type)
T.type = integer
T.type = real
D
Parse p, q, r : int
Inherited attribute
L1
L, 2
L3 ,
T
int
r
q
p
19
Rewrite Grammar
Example:
DL:T
T int
20
Input
Stack
p, q: real $
Action
shift (D id L)
, q: real $
shift (L , id L)
q: real $
p,
shift (L , id L)
: real $
p, q
shift (L : T)
real $
p, q:
shift (T real )
p, q: real
reduce (T real)
p, q: T
reduce (L : T)
p, q L
reduce (L , id L)
pL
reduce (D id L)
accept
D id L
id.type := L.type
L , id L1
L:T
L.type := T.type
T int
T.type := integer
T real
T.type := real
10
3/7/15
A aB
A.u := B.u + h3(a); print (A.u, B.v);
B bC
B.u := C.u; B.v := C.v + g3(b)
C cD
C.u := D.u + h2(c); C.v := D.v
D dEF
D.u := E.u; D.v := F.v + g2(d)
Ee
E.u := h1(e)
Can something be done to save
Ff
F.v := g1(f)
the copying effort?
Both u and v are synthesized attributes Use global table
A.u is computed from C.u and E.u
Is global attribute worse than
B.v is computed from D.v and F.v
stack technique?
D.u := E.u, C.v := D.v, B.u := C.u are only for value passing
21
11
3/7/15
+
T
F
F
b
F
*
23
Attribute Grammars
Other works in semantic analysis
Attribute grammar
yacc
Operational semantics
Actions that are executed directly rather than by
translation
Denotational semantics
a mathematical formalism for semantic specification
Axiomatic semantics
Mainly for program proof
24
12
3/7/15
25
Type Checking
Intermediate Code Generation
13
3/7/15
Symbol Table
Used by all phases of compiler
A data structure to keep track of the binding of the
identifiers
At type checking time
Keep track of identifier types
Symbol Table
Identifier categories
Variable names
Defined constants
Compiler generated temporary variables
Function names and parameter names
Labels
Type names
14
3/7/15
Starting address
Element type = (e.g., integer for integer array)
Linked list of the fields
In each field, specify: name of the field, type of the field, size
Offset for each field
Fields may itself be record/structure
29
Type Checking
First handle type definitions
Statement for basic type definitions (discussed earlier)
D id L
L , id L1
L:T
T int
T real
30
15
3/7/15
Type Definition
Basic Types
Integer, real, Boolean, char, etc.
Compound types
Pointers
Type = pointer (basic-type)
Array
Type = array (dimension)
Structure
Type = structure with the types of all fields
Function
Type = function with the types of all parameters
31
Type Checking
Type checking for the statements from bottom up
L S; L1
L.type if (S.type = void and L1.type = void) then void
else type error
S id := E
S.type := if (id.type = E.type) then void else type error
S if E then S1
S.type := if E.type = Boolean then S1.type else type error
S while E do S1
S.type := if E.type = Boolean then S1.type else type error
S if E then S1 else S2
S.type := if E.type = Boolean then ? else type error
Best can be done is to return the supertype of S1 and S2
32
16
3/7/15
Type Checking
Type equivalence
array(a,b)
array(c,d)
In some situations, like parameter passing, they are structurally
equivalent
Subtypes
Define subtype relation
e.g., A B: A is a subtype of B
A may be accepted anywhere B is expected
33
Disadvantages
Extra phase in the compilation
If the target machine language is relatively high level,
34
17
3/7/15
the language)
35
Copy
x := y
Indexed assignment
x := y[i] or x[i] := y or x[i] = y[j]
18
3/7/15
Conditional jump
If x rel-op y goto L
Procedure call
Call: p (x1, x2, xn)
Represented as
37
param x1
param x2
param xn
call p, n
n is the number of parameters
Handling Declaration
When entering a procedure
Create a child symbol table
Declaration statements
A new entry for each identifier declared
Compute the relative address of the identifier
38
19
3/7/15
Handling Declaration
Processing declarations in a basic block (e.g., procedure)
PL
L DL | D
D id : T ;
{ offset := 0 }
Initialized offset.
But it will not be executed
till the very end.
Handling Statements
In declaration
Create entries in symbol table
In statements
Need to look up the symbol table
lookup(id.name)
Return the pointer to the id entry in the table
If not found, then return nil
20
3/7/15
41
Array Addressing
Array elements are stored consecutively
Allow quick computation of the physical memory location
Assume low = 0
42
21
3/7/15
Array Addressing
Bottom up translation
3 other cases
S L := E
EE+E
E (E)
E L | num
L id
LA
{ L.array := true;
L.place := A.place; L.offset := A.offset }
{}
{}
{}
A Elist]
Elist Elist1, E
Elist id[E
43
Array Addressing
Bottom up translation
LA
A Elist]
Elist Elist1, E
Elist id[E
arrayPlace, offset,
arraySize, elementWidth
are global variables
array element
22
3/7/15
Conditional Branch
if E then S1 else S2
Evaluation of Boolean expression
Similar to evaluation of arithmetic expressions
Conditional Branch
if E then S1 else S2
if a < b then S1 else S2
Use label
test := a < b
if test goto L
S2
L: S1
Use address
Do not know how much space S2 requires
Cannot do it in one path
Back-patching
46
23
3/7/15
Procedure Call
Just like a program
Handle declaration, but in a child symbol table
Search may go back to parent symbol table
Handle statements
table
Record current instruction location
Need to handle return
Copy the output parameters from local table to parent table
47
48
24