




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
intermidate code for compiler design
Typology: Lecture notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler to be retargeted. Relatively easy to manipulate (optimize).
Advantages of Using an Intermediate Language Retargeting - Build a compiler for a new machine by attaching a new code generator to an existing front-end. Optimization - reuse intermediate code optimizers in compilers for different languages and different machines.
There are three types of intermediate representation
Intermediate Parser (^) code generator Static type checker
Code generator
Assign
Uminus (^) b Uminus (^) b
a a
X
Syntax tree
Assign
X +
Uminus (^) b
a
DAG
There are three representations used for three address code such as quadruples, triples and indirect triples. Consider the input statement x:= -ab + -ab Three address code for above statement given below:
tj : t1=uminus a t 2 := t1 * b : t3= - a t 4 := t 3 * b t 5 := t 2 + t 4 x : x= t 5
Quadruple representation The quadruple is a structure with at the most four fields such as op,arg1,arg The op field is used to represent the internal code for operator, the arg1 and arg represent the two operands. And result field is used to store the result of an expression.
For obtaining the three address code the SDD translation scheme or semantic rules must be written for each source code statement. There are various programming constructs for which the semantic rules can be defined. Using these rules the corresponding intermediate code in the form of three address code can be generated. Various programming constructs are :
S-> D Offset=
D -> id: T {enter(id.name,T.type,offset); Offset=offset+T.width } T-> integer {T.type:=integer; T.width:=4}
T-> real {T.type:=real; T.width:=8}
T->array[num] of T 1 {T.type:=array(num.val,T 1 .type) T.width:=num.val X T1.width }
T -> *T 1 {T.type:=pointer(T.type) T.width:=4}
formula offset = offset + width.
indicates the data type of corresponding identifier and width is used to indicate the memory units associated with an identifier of corresponding type.
creating the symbol table entry for identifier along with its type and offset.
elements in the array.
Assignment Statements The assignment statement mainly deals with the expressions. The expressions can be of type integer, real, array and record. Consider the following grammar S-> id :=E E-> E1 + E E-> E1 * E E-> -E E-> (E1) E-> id The translation scheme of above grammar is given below: Production Rule Semantic actions S-> id :=E { p=look_up(id.name); If p≠ nil then Emit(p = E.place) Else Error; } E-> E1 + E2 { E.place=newtemp(); Emit (E.place=E1.place ‘+’ E2.place) } E-> E1 * E2 { E.place=newtemp(); Emit (E.place=E1.place ‘*’ E2.place) } E-> -E1 { E.place=newtemp(); Emit (E.place=’uminus’ E1.place) } E-> (E1) {E.place=E1.place}
E-> id { p=look_up(id.name); If p≠ nil then Emit (p = E.place) Else Error; } The p returns the entry for id.name in the symbol table if it exists there.
error will be reported.
E E .place:=newtemp() A Emit (E.place ':="NOT' E1.place) } E -> (E1) { E E.place := E1.place }
E -> id 1 relop id 2 { E. place := newtemp() Emit ('if id.place relop.op id 2 .place 'goto' next_state +3 : Emit (E.place':=' '0' ); Emit ('goto' next state +2); Emit (E.place := '1') } E -> TRUE { E.place := newtemp(); Emit (E.place ':=' '1') } E-> FALSE { E.place := newtemp() Emit (E.place ':=' '0') }
The function Emit generates the three address code and newtemp () is for generation of temporary variables. For the semantic action for the rule E - > id1 relop id2 contains next_state which gives the index of next three address statement in the output sequence. Let us take an example and generate the three address code using above translation scheme : p > q AND r < s OR u > v 100: if p > q goto 103 101: t1:= 102: goto 104 103: t1:= 104: if r < s goto 107 105: t2:= 106: goto 108 107: t2= 108:if u>v goto 111 109:t3= 110:goto 112 111:t3= 112:t4=t1 AND t 113:t5=t4 OR t
Flow of control statement The control statements are if-then-else and while-do. The grammar for such statements is given below S —> if E then S1| if E then S1 else S2 | while E do S Translation scheme is given below S->if E then S1 {E.true=new_label() E.False=new_label() S1.next=S.next S2.next=S.next S.code= E.code ||gen_code(E.true’:’) ||S1.code} S->if E then S1 else S2 {E.true=new_label() E.False=new_label() S1.next=S.next S2.next=S.next S.code=E.code ||gen_code(E.true’:’)||S1.code ||gen_code(‘goto’,s.next) ||gen_code(E.false’:’) || S2.code} S->while E do S1 {S.begin=new_label() E.True=new_label() E.False=S.next S1.next=S.begin S.code=gen_code(S.begin’:’)||E.code ||gen_code(E.true’:’) ||S1.code||gen_code(‘goto’,S.begin)} Consider the statement: if a<b then a=a+5 else a=a+ Three address code for above statement using semantic rule is 100: if a<b goto L 101: goto 103 102: L1: a=a+ 103: a=a+ (*Array & case statement refer from text book)