Professional Documents
Culture Documents
Semantic Analysis
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
E → E1 + T
E → T
T → T1 * F
T → F
F → (E)
F → id
F → num
A → LM
• L.inh = f1 (A.inh)
L-Attributed Definitions
• M.inh = f2 (L.syn)
• A.syn = f3 (M.syn)
A → QR
• R.inh = f4 (A.inh)
Not L-Attributed Definitions
• Q.inh = f5 (R.syn)
• A.syn = f6 (Q.syn)
CS F363 Compiler Construction 15
BITS Pilani, Pilani Campus
L-Attributed Definitions
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
• E→ T R
• R→ addop T R | ε
• T→ num
• addop → + | –
E→ T R
R→ addop T {print(addop)}R | ε
T→ num {print(num)}
addop → + | –
E→ T R
R→ addop T {print(addop)}R | ε
T→ num {print(num)}
addop → + | –
Consider the Translation Scheme for the L-Attributed Definition for “type declarations”:
D → T {L.in := T .type } L
T → int {T .type :=int }
T → real {T .type :=real }
L → { L 1.in := L.in } L1, id {addtype(id.entry, L.in) }
L → id {addtype(id.entry, L.in) }
The parse-tree with semantic actions for the input real id1, id2, id3 is:
D D → T {L.in := T .type } L
T → int {T .type :=int }
T → real {T .type :=real }
L → { L 1.in := L.in } L1, id {addtype(id.entry, L.in) }
T L.in:=T.type L
L → id {addtype(id.entry, L.in) }
real T.type := realL1.in:=L.in L1 , id 3 addtype(id 3 . entry , L. in )
E→TR E→TR
R → + T {print (+)} R R→+TMR
R → - T {print (-)} R R→-TNR
R→∈ R→∈
T→num{print(num.val)} T → num {print(num.val)}
M→∈ {print(+)}
N→∈ {print(-)}
18
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Compiler Construction CS F363
Type Checking
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
Static Typing
• Catches most common programming errors at compile time
• Avoids runtime overhead
Most code is written using static types languages
• In fact, developers for large/critical system insist that code
should be strongly type checked at compile time even if
language is not strongly typed
CS F363 Compiler Construction 4
BITS Pilani, Pilani Campus
Type Expression
Type Name
• Type Constructor applied to a list of type expressions.
7
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Constructors
P → D ; E
D → D ; D | id : T
T → char | integer | T[num] | T*
E → literal | num | E%E | E [E] | *E
D → id : T
T → char
T → integer
T → T1*
T → T1 [num]
16
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Rules for Symbol Table entry
D → id : T addtype(id.entry, T.type)
T → char T.type = char
T → integer T.type = int
T → T1* T.type = pointer(T1 .type)
T → T1 [num] T.type = array(0….num-1, T1 .type)
17
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Checking of Functions
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
P → D ; E
D → D ; D | id : T
T → char | integer | T[num] | T*
E → literal | num | E%E | E [E] | *E
D → id : T addtype(id.entry, T.type)
T → char T.type = char
T → integer T.type = int
T → T1* T.type = pointer(T1 .type)
T → T1 [num] T.type = array(0….num-1, T1 .type)
6
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Checking of Functions
The synthesized attribute type for E gives the type expression assigned by
the type system to the expression generated by E
10
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Checking for Expressions…..
Within expressions, the prefix operator * yields the object pointed to by its
operand. The type of *E is the type t of the object pointed to by the pointer E
11
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type checking for Statements
P → D ; S
D → D ; D | id : T
T → char | integer | T[num] | T*
E → literal | num | E%E | E [E] | *E
S → id := E
S → if E then S1
S → while E do S1
S → S1 ; S2
15
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type checking for Statements….
In this rule, a sequence of statements has type void only if each sub-
statement has type void.
18
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Conversion
19
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Conversion…..
E → num
E → num.num
E → id
E → E1 op E2
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
• Overloading of Functions
• Overloaded Function Resolution
• Narrowing the Set of Types
• Implementation of Symbol Table for Local Scope
6
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Set of Possible types for the 3*5……
7
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Finding Set of Possible Types…..
E` → E E`.types = E.types
E.unique = if E`.types=={t} then t else type_error
E → id E.types = lookup(id)
E → E1 (E2 ) E.types = { t | ∃s in E2 .types && s→t is in E1 .types}
t = E.unique
S = {s | s ∈ E2.types and (s→t) ∈ E1.types}
E2 .unique = if S=={s} then s else type_error
E1 .unique = if S=={s} then s→t else type_error
11
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Symbol Table
14
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Symbol Table Entries
P→D
D→D;D
D → id : T
T → integer
T → real
For each name create symbol table entry with information like
type and relative address
P → {offset=0} D
D→D;D
D → id : T enter(id.name, T.type, offset); offset = offset + T.width
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
P→D
D→D;D
D → id : T
T → integer
T → real
For each name create symbol table entry with information like
type and relative address
P → {offset=0} D
D→D;D
D → id : T enter(id.name, T.type, offset); offset = offset + T.width
program sort;
var a : array[1..n] of integer;
x : integer;
procedure readarray;
var i : integer;
……
procedure exchange(i,j:integers);
……
procedure quicksort(m,n : integer);
var k,v : integer;
function partition(x,y:integer):integer;
var i,j: integer;
……
……
begin{main}
……
end. 5
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Design of Symbol Table for Nested Procedures
6
BITS Pilani, Pilani Campus
Keeping track of local information
P → D
D → D ;D
D → id : T
D → proc id ;D ; S
P → {t = mktable (nil);
push(t,tblptr);
push(0,offset)}
D
{addwidth(top(tblptr),top(offset));
pop(tblptr); pop(offset)}
D → D;D
12
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Creating Symbol Table ..…
13
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Creating symbol table …
14
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Compiler Construction CS F363
Intermediate Code Generation
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
Benefits
• Retargeting is possible.
• Machine independent code optimization is possible is a set of values and
operations on those values.
CS F363 Compiler Construction 2
BITS Pilani, Pilani Campus
Position of Intermediate Code Generator
X := Y op Z
• X, Y or Z are names, constants or compiler generated
temporaries
• op stands for any operator such as a fixed - or floating-point
arithmetic operator, or a logical operator
CS F363 Compiler Construction 9
BITS Pilani, Pilani Campus
Three Address Code……
Only one operator on the right hand side is allowed. Source expression
like x + y * z might be translated into
• t1 := y * z
• t2 := x + t1 where t1 and t2 are compiler generated temporary names.
Assignment
• x = y op z
• x = op y
• x=y
Jump
• goto L
• if x relop y goto L
CS F363 Compiler Construction 11
BITS Pilani, Pilani Campus
Three Address Instructions…..
Indexed assignment
• x = y[i]
• x[i] = y
Function
• param x
• call p,n
• return y
CS F363 Compiler Construction 12
BITS Pilani, Pilani Campus
Three Address Instructions…….
Pointer
•x = &y
•x = *y
•*x = y
CS F363 Compiler Construction 13
BITS Pilani, Pilani Campus
Syntax directed translation of expression into 3-
address code
Two attributes
• E.place, a name that will hold the value of E, and
• E.code, the sequence of three-address statements evaluating E.
Shashank Gupta
BITS Pilani Assistant Professor
Pilani Campus
Department of Computer Science and Information Systems
2
CS F363 Compiler Construction BITS Pilani, Pilani Campus
More Examples……
S → id := E {p = lookup(id.place);
if p <> nil then emit(p := E.place)
else error}
E → id {p = lookup(id.name);
if p <> nil then E.place = p
else error}
4
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Flow of Control …
S → if E then S1 else S2 S.else := newlabel
E.code S.after := newlabel
if E.place = 0 goto S.else
S1 .code S.code = E.code ||
goto S.after gen(if E.place = 0 goto S.else) || S1 .code ||
S.else: gen(goto S.after) ||
S2 .code gen(S.else :) ||
S.after: S2 .code ||
gen(S.after :)
5
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Flow of Control ……
6
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Type Conversion within Assignments
8
CS F363 Compiler Construction BITS Pilani, Pilani Campus
Methods of Translation