Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching

Size: px
Start display at page:

Download "Theory of Compilation Erez Petrank. Lecture 7: Intermediate Representation Part 2: Backpatching"

Transcription

1 Theory of Compilation rez Petrank Lecture 7: Intermediate Representation Part 2: ackpatching 1

2 You are here Compiler Source text txt Lexical Analysis Syntax Analysis Semantic Analysis Inter. Rep. (IR) Gen. Code xecutable code exe characters tokens (Abstract Syntax Tree) AST Annotated AST 2

3 Intermediate Representation neutral representation between the front-end and the back-end Abstracts away details of the source language Abstract away details of the target language A compiler may have multiple intermediate representations and move between them Java C Ada Objec2ve C GNRIC arm x86 ia64 3

4 Our Representation: Three Address Code (3AC) very instruction operates on three addresses result = operand1 operator operand2 Close to low-level operations in the machine language Operator is a basic operation Statements in the source language may be mapped to multiple instructions in three address code 4

5 Three address code: example assign a + * b uminus b * uminus t 1 t 2 t 3 t 4 t 5 a := := := := := := c b * t 1 c b * t 3 t 2 + t 4 t 5 c c 5

6 Three address code: example instructions instruc(on meaning x := y op z assignment with binary operator x := op y assignment unary operator x:= y assignment x := &y assign address of y x:=*y assignment from deref y *x := y assignment to deref x instruc(on goto L if x relop y goto L meaning uncondi2onal jump condi2onal jump 6

7 Creating 3AC Assume bottom up parser Covers a wider range of grammars LALR sufficient to cover most programming languages Creating 3AC via syntax directed translation Attributes examples: code code generated for a nonterminal var name of variable that stores result of nonterminal freshvar() helper function that returns the name of a fresh variable 7

8 Creating 3AC: expressions produc(on S id := * 2-1 (1) seman(c rule S.code :=. code gen(id.var :=.var).var := freshvar();.code = 1.code 2.code gen(.var := 1.var + 2.var).var := freshvar();.code = 1.code 2.code gen(.var := 1.var * 2.var).var := freshvar();.code = 1.code gen(.var := uminu 1.var).var := 1.var.code = ( 1.code ) id.var := id.var;.code = (we use to denote concatena2on of intermediate code fragments) 8

9 example a = b * - c + b* - c a assign +.var = t5.code = t1 = - c t2 = b*t1 t3 = - c t4 = b*t3 t5 = t2+t4 *.var = t2.code = t1 = - c t2 = b*t1.var = t4.code = t3 = - c t4 = b*t3 *.var = t3.code = t3 = - c b.var = b.code = uminus c.var = t1.code = t1 = - c b.var = b.code = uminus c.var = c.code =.var = c.code = 9

10 Creating 3AC: control statements S t while do S1 S.begin: S.after:.code if.var = 0 goto S.after S 1.code goto S.begin produc(on S t while do S1 seman(c rule S.begin := freshlabel(); S.a_er = freshlabel(); S.code := gen(s.begin : ).code gen( if.var = 0 goto S.a_er) S1.code gen( goto S.begin) gen(s.a_er : ) 10

11 Generating IR code Option 1 accumulate code in AST attributes Option 2 emit IR code to a file during compilation If for every production the code of the left-hand-side is constructed from a concatenation of the code of the RHS in some fixed order 11

12 xpressions and assignments produc(on S id := seman(c ac(on { p:= lookup(id.name); if p null then emit(p :=.var) else error } 1 op 2 {.var := freshvar(); emit(.var := 1.var op 2.var) } - 1 {.var := freshvar(); emit(.var := uminus 1.var) } ( 1) {.var := 1.var } id { p:= lookup(id.name); if p null then.var :=p else error } 12

13 oolean xpressions produc(on seman(c ac(on 1 op 2 {.var := freshvar(); emit(.var := 1.var op 2.var) } not 1 {.var := freshvar(); emit(.var := not 1.var) } ( 1) {.var := 1.var } true {.var := freshvar(); emit(.var := 1 ) } false {.var := freshvar(); emit(.var := 0 ) } Represent true as 1, false as 0 Wasteful representa2on, crea2ng variables for true/false 13

14 oolean expressions via jumps produc(on seman(c ac(on id1 op id2 {.var := freshvar(); emit( if id1.var relop id2.var goto nextstmt+2); emit(.var := 0 ); emit( goto nextstmt + 1); emit(.var := 1 ) } This is useful for short circuit evaluation. Let us start with an example. 14

15 oolean xpressions: an xample or a < b and c < d e < f 15

16 oolean xpressions: an xample or a < b and 100: 101: 102: 103: if a < b goto 103 T 1 := 0 goto 104 T 1 := 1 c < d e < f 16

17 oolean xpressions: an xample or a < b and 100: 101: 102: 103: if a < b goto 103 T 1 := 0 goto 104 T 1 := 1 c < d 104: if c < d goto : T 2 := 0 106: goto : T 2 := 1 e < f 17

18 oolean xpressions: an xample or a < b and 100: 101: 102: 103: if a < b goto 103 T 1 := 0 goto 104 T 1 := 1 c < d 104: if c < d goto : T 2 := 0 106: goto : T 2 := 1 e < f 108: 109: 110: 111: 112: if e < f goto 111 T 3 := 0 goto 112 T 3 := 1 18

19 oolean xpressions: an xample or a < b and 100: 101: 102: 103: if a < b goto 103 T 1 := 0 goto 104 T 1 := 1 c < d 104: if c < d goto : T 2 := 0 106: goto : T 2 := 1 e < f 108: 109: 110: 111: 112: 113: if e < f goto 111 T 3 := 0 goto 112 T 3 := 1 T 4 := T 2 and T 3 19

20 oolean xpressions: an xample or a < b and 100: 101: 102: 103: if a < b goto 103 T 1 := 0 goto 104 T 1 := 1 c < d 104: if c < d goto : T 2 := 0 106: goto : T 2 := 1 e < f 108: 109: 110: 111: 112: 113: if e < f goto 111 T 3 := 0 goto 112 T 3 := 1 T 4 := T 2 and T 3 T 5 := T 1 or T 4 20

21 Short circuit evaluation Second argument of a boolean operator is only evaluated if the first argument does not already determine the outcome (x and y) is equivalent to: if x then y else false ; (x or y) is equivalent to: if x then true else y ; Is short circuit evaluation equivalent to standard evaluation? We use it if the language definition dictates its use. 21

22 Our previous example a < b or (c<d and e<f) 100: if a < b goto : T 1 := 0 102: goto : T 1 := 1 104: if c < d goto : T 2 := 0 106: goto : T 2 := 1 108: if e < f goto : T 3 := 0 110: goto : T 3 := 1 112: T 4 := T 2 and T 3 113: T 5 := T 1 and T 4 100: if a < b goto : if!(c < d) goto : if e < f goto : T := 0 104: goto : T := 1 106: naive Short circuit evalua2on 22

23 Control Structure: if, else, while. Consider the conditional jumps: S if then S 1 if then S 1 else S 2 while do S 1 One option is to create code for, create code for S, and then create a jump to the beginning of S or the end of S according to s value. ut it would be more efficient to create code that while executing will jump to the correct location immediately when the value of is discovered.

24 Control Structure: if, else, while. The problem is that we do not know where to jump to While parsing s tree for if then S, we don t know S s code and where it starts or ends. Yet, we must generate jumps to these locations. Solution: for each expression keep labels:.truelabel and.falselabel. We jump to these labels when s value is true or false correspondingly. Also, for each statement S, we have a label S.nextLabel which keeps the address of the code that follows the statement S. The semantic equation will be.falselabel = S.next. For.true, we create a new label between s code and S s code. S if then S

25 התכונה next While parsing statement S, generate the code with its next label. produc(on P t S S t S1 S2 seman(c ac(on S.next = freshlabel(); P.code = S.code label(s.next) S1.next = freshlabel(); S2.next = S.next; S.code = S1.code label(s1.next) S2.code The attribute S.next is inherited: the child gets it. The attribute code is generated: the parent gets it. The label S.next is symbolic. The address will only be known after we parse S.

26 Control Structures: conditional produc(on seman(c ac(on S t if then S1.trueLabel = freshlabel();.falselabel = S.next; S1.next = S.next; S.code =.code gen (.truelabel : ) S1.code Are S1.next,.falseLabel inherited or synthesized? Is S.code inherited or synthesized? 26

27 Control Structures: conditional produc(on S t if then S1 else S2 seman(c ac(on.truelabel = freshlabel();.falselabel = freshlabel(); S1.next = S.next; S2.next = S.next; S.code =.code gen(.truelabel : ) S1.code gen( goto S.next) gen(.falselabel : ) S2.code.trueLabel and.falselabel considered inherited 27

28 S t if then S1 else S2.trueLabel:.falseLabel: S.next:.code S1.code goto S.next S2.code.trueLabel = freshlabel();.falselabel = freshlabel(); S1.next = S.next; S2.next = S.next; S.code =.code gen(.truelabel : ) S1.code gen( goto S.next) gen(.falselabel : ) S2.code 28

29 oolean expressions Generate code that jumps to.truelabel if s value is true and to.falselabel if s value is false. produc(on t 1 or 2 t 1 and 2 t not 1 t (1) seman(c ac(on 1.trueLabel =.truelabel; 1.falseLabel = freshlabel(); 2.trueLabel =.truelabel; 2.falseLabel =.falselabel;.code = 1.code gen (1.falseLabel : ) 2.code 1.trueLabel = freshlabel(); 1.falseLabel =.falselabel; 2.trueLabel =.truelabel; 2.falseLabel =.falselabel;.code = 1.code gen (1.trueLabel : ) 2.code 1.trueLabel =.falselabel; 1.falseLabel =.truelabel;.code = 1.code; 1.trueLabel =.truelabel; 1.falseLabel =.falselabel;.code = 1.code; t id1 relop id2.code=gen ( if id1.var relop id2.var goto.truelabel) gen( goto.falselabel); t true t false.code = gen( goto.truelabel).code = gen( goto.falselabel); 29

30 oolean expressions produc(on t 1 or 2 seman(c ac(on 1.trueLabel =.truelabel; 1.falseLabel = freshlabel(); 2.trueLabel =.truelabel;.falselabel =.falselabel;.code = 1.code gen (1.falseLabel : ) 2.code How can we determine the address of 1.falseLabel? Only possible after we know the code of 1 and all the code preceding 1 30

31 xample S if then S1 1 true and 2 false 1.trueLabel = freshlabel(); 1.falseLabel =.falselabel; 2.trueLabel =.truelabel; 2.falseLabel =.falselabel;.code = 1.code gen (1.trueLabel : ) 2.code.code = gen( goto 2.falseLabel).code = gen( goto 1.trueLabel) 31

32 xample.truelabel = freshlabel();.falselabel = S.next; S S1.next = S.next; S.code =.code gen (.truelabel : ) S1.code if then S1 1 true and 2 false 1.trueLabel = freshlabel(); 1.falseLabel =.falselabel; 2.trueLabel =.truelabel; 2.falseLabel =.falselabel;.code = 1.code gen (1.trueLabel : ) 2.code.code = gen( goto 2.falseLabel).code = gen( goto 1.trueLabel) 32

33 Computing the labels We can build the code while parsing the tree bottomup, leaving the jump targets vacant. We can compute the values for the labels in a second traversal of the AST Can we do it in a single pass? 33

34 So far Three address code. Intermediate code generation is executed with parsing (via semantic actions). Creating code for oolean expressions and for control statements is more involved. We typically use short circuit evaluation, value of expression is implicit in control location. We need to compute the branching addresses. Option 1: compute them in a second AST pass. 34

35 (תיקון לאחור) ackpatching Goal: generate code in a single pass Generate code as we did before, but manage labels differently Keep targets of jumps empty until values are known, and then back-patch them New synthesized attributes for.truelist list of jump instructions that eventually get the label where goes when is true..falselist list of jump instructions that eventually get the label where goes when is false. 35

36 ackpatching For every label, maintain a list of instructions that jump to this label When the address of the label is known, go over the list and update the address of the label Previous solutions do not guarantee a single pass The attribute grammar we had before is not S-attributed (e.g., next is inherited), and is not L-attributed (because the inherited attributes are not necessarily from left siblings). 36

37 S Why is it difficult? if then else S 1 S 2 Think of an if-then-else statement. To compute S1.next, we need to know the code of all blocks S1,, S2, in order to compute the address that follows them. On the other hand, to compute the code of S1, we need to know S1.next, or S.next, which is not known before S1 is computed. So we cannot compute S1 s code with all jump targets, but we can compute it up to leaving space for future insertion of S1.next. Using backpatching we maintain a list of all empty spaces that need to jump to S1.next. When we know S1.next, we go over this list and update the jump targets with S1.next s value.

38 Functions for list handling makelist(addr) create a list of instructions containing addr merge(p1,p2) concatenate the lists pointed to by p1 and p2, returns a pointer to the new list backpatch(p,addr) inserts i as the target label for each of the instructions in the list pointed to by p 38

39 Accumulating the code Assume a bottom-up parsing so that the code is created in the correct order from left to right, bottom-up. The semantic analysis is actually executed during parsing and the code is accumulated. Recall that we associated two labels.truelabel and.falselabel for each expression, which are the labels to which we should jump if turns out true or false. We will now replace these with two lists.truelist and.falselist, which record all instructions that should be updated with the address of.truelabel or.falselabel when we discover their values. In addition, we also replace S.next with S.nextlist, maintaining all instructions that should jump to S.next, when we know what it is.

40 Accumulating Code (cont d).truelist and.falselist are generated attributes: the descendants tell the parent where there is code to fix. When ascending to the parent of, we know what are the relevant addresses and we can backpatch them into all the instruction locations that are in the list. Similarly for S.nextlist. We will need a function nextinstr() that retruns the address of the next instruction.

41 Computing oolean expressions Recall the code jumps to.true ot.false according to s value. Previously: id 1 relop id 2 true false {.code := gen ( ' if ' id 1.var relop.op id 2.var ' goto '.true ) gen ( ' goto '.false ) } {.code := gen ( ' goto '.true ) } {.code := gen ( ' goto '.false ) } Now with backpatching: id 1 relop id 2 true false {.truelist := makelist ( nextinstr ) ;.falselist := makelist ( nextinstr + 1 ) ; emit ( ' if ' id 1.var relop.op id 2.var ' goto_ ' ) emit ( ' goto_ ' ) } {.truelist := makelist ( nextinstr ) ; emit ( ' goto_ ' ) } {.falselist := makelist ( nextinstr ) ; emit ( ' goto_ ' ) }

42 Computing oolean expressions Recall the code jumps to.true ot.false according to s value. Previously: not 1 { 1.true :=.false ; 1.false :=.true ;.code := 1.code } ( 1 ) { 1.true :=.true ; 1.false :=.false ;.code := 1.code } Now with backpatching: not 1 {.truelist := 1.falselist ;.falselist := 1.truelist } ( 1 ) {.truelist := 1.truelist ;.falselist := 1.falselist }

43 Computing oolean expressions Recall the code jumps to.true ot.false according to s value. Previously: 1 or 2 1 and 2 { 1.true :=.true ; 1.false := newlable() ; 2.true :=.true ; 2.false :=.false ;.code := 1.code gen ( 1.false ' : ') 2.code } { 1.true := newlabel (); 1.false :=.false ; 2.true :=.true ; 2.false :=.false ;.code := 1.code gen ( 1.true ' : (' 2.code } Now with backpatching: 1 or M 2 1 and M 2 M { backpatch ( 1.falselist, M.instr ) ;.truelist := merge ( 1.truelist, 2.truelist ) ;.falselist := 2.falselist } { backpatch ( 1.truelist, M.instr ) ;.truelist := 2.truelist ;.falselist := merge ( 1.falselist, 2.falselist ) } { M.instr := nextinstr }

44 ackpatching oolean expressions produc(on t 1 or M 2 seman(c ac(on backpatch(1.falselist,m.instr);.truelist = merge(1.truelist,2.truelist);.falselist = 2.falseList; t 1 and M 2 backpatch(1.truelist,m.instr);.truelist = 2.trueList;.falseList = merge(1.falselist,2.falselist); t not 1 t (1).trueList = 1.falseList;.falseList = 1.trueList;.trueList = 1.trueList;.falseList = 1.falseList; t id1 relop id2.truelist = makelist(nextinstr);.falselist = makelist(nextinstr+1); emit ( if id1.var relop id2.var goto _ ) emit( goto _ ); t true t false M t ε.truelist = makelist(nextinstr); emit ( goto _ );.falselist = makelist(nextinstr); emit ( goto _ ); M.instr = nex2nstr; 44

45 Using a marker xample: 1 or M 2 { M.instr = nextinstr;} Use M to obtain the address just before 2 code starts being generated 45

46 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} or M x < : if x< 100 goto _ 101: goto _ ε and M ε x > 200 x!= y t id1 relop id2.truelist = makelist(nextinstr);.falselist = makelist(nextinstr+1); emit ( if id1.var relop id2.var goto _ ) emit( goto _ ); 46

47 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} M.i = 102 or M x < : if x< 100 goto _ 101: goto _ ε and M ε x > 200 x!= y M t ε M.instr = nex(nstr; 47

48 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} M.i = 102 or M x < : if x< 100 goto _ 101: goto _ ε.t = {102}.f = {103} and M ε 102: if x> 200 goto _ 103: goto _ x > 200 x!= y t id1 relop id2.truelist = makelist(nextinstr);.falselist = makelist(nextinstr+1); emit ( if id1.var relop id2.var goto _ ) emit( goto _ ); 48

49 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} M.i = 102 or M x < : if x< 100 goto _ 101: goto _ ε.t = {102}.f = {103} and M ε M.i = : if x> 200 goto _ 103: goto _ x > 200 x!= y M t ε M.instr = nex(nstr; 49

50 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} M.i = 102 or M x < : if x< 100 goto _ 101: goto _ 102: if x> 200 goto _ 103: goto _ ε.t = {102}.f = {103} x > 200 and M ε 104: if x!=y goto _ 105: goto _ M.i = 104 x!= y.t = {104}.f = {105} t id1 relop id2.truelist = makelist(nextinstr);.falselist = makelist(nextinstr+1); emit ( if id1.var relop id2.var goto _ ) emit( goto _ ); 50

51 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} or M M.i = 102.t = {104}.f = {103,105} x < : if x< 100 goto _ 101: goto _ 102: if x> 200 goto : goto _ ε.t = {102}.f = {103} x > 200 and M ε 104: if x!=y goto _ 105: goto _ M.i = 104 x!= y.t = {104}.f = {105} t 1 and M 2 backpatch(1.truelist,m.instr);.truelist = 2.trueList;.falseList = merge(1.falselist,2.falselist); 51

52 xample X < 100 or x > 200 and x!= y.t = {100}.f = {101} or M.t = {100,104}.f = {103,105} M.i = 102.t = {104}.f = {103,105} x < : if x< 100 goto _ 101: goto : if x> 200 goto : goto _ ε.t = {102}.f = {103} x > 200 and M ε 104: if x!=y goto _ 105: goto _ M.i = 104 x!= y.t = {104}.f = {105} t 1 or M 2 backpatch(1.falselist,m.instr);.truelist = merge(1.truelist,2.truelist);.falselist = 2.falseList; 52

53 xample 100: if x<100 goto _ 101: goto _ 102: if x>200 goto _ 103: goto _ 104: if x!=y goto _ 105: goto _ 100: if x<100 goto _ 101: goto _ 102: if x>200 goto : goto _ 104: if x!=y goto _ 105: goto _ 100: if x<100 goto _ 101: goto : if x>200 goto : goto _ 104: if x!=y goto _ 105: goto _ efore backpatching A_er backpatching by the produc2on t 1 and M 2 A_er backpatching by the produc2on t 1 or M 2 53

54 ackpatching for statements produc(on S t if () M S1 S t if () M1 S1 N else M2 S2 S t while M1 () M2 S1 S t { L } S t A M t ε N t ε L t L1 M S L t S seman(c ac(on backpatch(.truelist,m.instr); S.nextList = merge(.falselist,s1.nextlist); backpatch(.truelist,m1.instr); backpatch(.falselist,m2.instr); temp = merge(s1.nextlist,n.nextlist); S.nextList = merge(temp,s2.nextlist); backpatch(s1.nextlist,m1.instr); backpatch(.truelist,m2.instr); S.nextList =.falselist; emit( goto M1.instr); S.nextList = L.nextList; S.nextList = null; M.instr = nex2nstr; N.nextList = makelist(nextinstr); emit( goto _ ); backpatch(l1.nextlist,m.instr); L.nextList = S.nextList; L.nextList = S.nextList 54

55 Generate code for procedures n = f(a[i]); t1 = i * 4 t2 = a[t1] // could have expanded this as well param t2 t3 = call f, 1 n = t3 we will see handling of procedure calls in much more detail later 55

56 xtend grammar for procedures D t define T id (F) { S } F t ε T id, F S t return ; t id (A) A t ε, A statements expressions type checking function type: return type, type of formal parameters within an expression function treated like any other operator symbol table parameter names 56

57 Summary Three address code is our IR. Intermediate code generation is executed while parsing (via semantic actions). Creating code for oolean expressions and for control statements is more involved. We typically use short circuit evaluation, value of an expression is implicit in control location. We need to compute the branching addresses. Option 1: compute them in a second AST pass. Option 2: backpatching (a single pass): maintain lists of incomplete jumps, where all jumps in a list have the same target. When the target becomes known, all instructions on its list are filled in. 57

58 Recap Lexical analysis regular expressions identify tokens ( words ) Syntax analysis context-free grammars identify the structure of the program ( sentences ) Contextual (semantic) analysis type checking defined via typing judgements can be encoded via attribute grammars Syntax directed translation attribute grammars Intermediate representation many possible IRs generation of intermediate representation 3AC 58

59 Journey inside a compiler float posi2on; float ini2al; float rate; posi2on = ini2al + rate * 60 Token Stream <float> <ID,posi2on> <;> <float> <ID,ini2al> <;> <float> <ID,rate> <;> <ID,1> <=> <ID,2> <+> <ID,3> <*> <60> Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. 59

60 Journey inside a compiler <ID,1> <=> <ID,2> <+> <ID,3> <*> <60> symbol table id symbol type data = AST 1 posi2on float 2 ini2al float <id,1> + 3 rate float <id,2> * S t ID = t ID + * NUM <id,3> 60 Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. 60

61 Journey inside a compiler = AST = AST symbol table id symbol type <id,1> + <id,1> + 1 posi2on float 2 ini2al float <id,2> * <id,2> * 3 rate float invofloat <id,3> 60 <id,3> 60 coercion: automa2c conversion from int to float inserted by the compiler Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. 61

62 Journey inside a compiler id1 = t3 <id,1> = <id,2> + * t3 = id2 * t2 3AC t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 t2 = id3 * t1 <id,3> invofloat 60 t1 = invofloat(60) produc(on S t id = t 1 op 2 t invofloat(num) seman(c rule S.code :=. code gen(id.var :=.var).var := freshvar();.code = 1.code 2.code gen(.var := 1.var op 2.var).var := freshvar();.code = gen(.var := invofloat(num)) t id.var := id.var;.code = Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. (for brevity, bubbles show only code generated by the node and not all accumulated code avribute) note the structure: translate 1 translate 2 handle operator 62

63 Journey inside a compiler 3AC Op2mized t1 = inttofloat(60) t2 = id3 * t1 t3 = id2 + t2 id1 = t3 t1 = id3 * 60.0 id1 = id2 + t1 value known at compile 2me can generate code with converted value eliminated temporary t3 Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. 63

64 Journey inside a compiler Op2mized Code Gen t1 = id3 * 60.0 id1 = id2 + t1 LDF R2, id3 MULF R2, R2, #60.0 LDF R1, id2 ADDF R1,R1,R2 STF id1,r1 Lexical Analysis Syntax Analysis Sem. Analysis Inter. Rep. Code Gen. 64

65 Problem 3.8 from [Appel] A simple left-recursive grammar: + id id A simple right-recursive grammar accepting the same language: id + id Which has better behavior for shift-reduce parsing? 65

66 Answer Input id+id+id+id+id id (reduce) + + id (reduce) + + id (reduce) + + id (reduce) + + id (reduce) + id id le_ recursive stack The stack never has more than three items on it. In general, with LR-parsing of left-recursive grammars, an input string of length O(n) requires only O(1) space on the stack. 66

67 Answer Input id+id+id+id+id id + id right recursive id id + id + id id + id + id + id + id id + id + id id + id + id + id id + id + id + id + id + id + id + id + id (reduce) id + id + id + id + (reduce) id + id + id + (reduce) id + id + (reduce) id + (reduce) stack The stack grows as large as the input string. In general, with LR-parsing of right-recursive grammars, an input string of length O(n) requires O(n) space on the stack. 67

68 Next Runtime nvironments 68

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 09 IR (ackpatching) THEORY OF COMPILATION Eran Yahav www.cs.technion.ac.il/~yahave/tocs2011/compilers-lec09.pptx Reference: Dragon 6.2,6.3,6.4,6.6 1 Recap Lexical analysis regular expressions identify

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall Announcements Weekly team meetings starting today Wednesday (4/4) will be a workshop Wednesday - Post questions you'd like addressed

More information

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 7, vrijdag 2 november 2018 Compilerconstructie najaar 2018 http://www.liacs.leidenuniv.nl/~vlietrvan1/coco/ Rudy van Vliet kamer 140 Snellius, tel. 071-527 2876 rvvliet(at)liacs(dot)nl college 7, vrijdag 2 november 2018 + werkcollege

More information

Module 26 Backpatching and Procedures

Module 26 Backpatching and Procedures Module 26 Backpatching and Procedures In this module, we would learn to generate three-address code for control flow statements using backpatching. We shall also discuss to combine Boolean expressions

More information

CMPSC 160 Translation of Programming Languages. Three-Address Code

CMPSC 160 Translation of Programming Languages. Three-Address Code CMPSC 160 Translation of Programming Languages Lectures 16: Code Generation: Three- Address Code Three-Address Code Each instruction can have at most three operands Each operand corresponds to a memory

More information

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation. UNIT-3 SYNTAX-DIRECTED TRANSLATION: A Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are

More information

Formal Languages and Compilers Lecture X Intermediate Code Generation

Formal Languages and Compilers Lecture X Intermediate Code Generation Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2011, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Control-Flow tatements hort-circuit Predicate valuation Back-Patching Copyright 2015, Pedro C. Diniz, all rights reserved. tudents enrolled in the Compilers class at the University

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation 1 Intermediate Code Generation Translating source program into an intermediate language" Simple CPU Independent, yet, close in spirit to machine language Benefits Retargeting

More information

Intermediate Representa.on

Intermediate Representa.on IR Intermediate Representa.on CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class Intermediate Representation Language Specific Language + Machine Independent Machine Dependent

More information

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale

Principle of Compilers Lecture VIII: Intermediate Code Generation. Alessandro Artale Free University of Bolzano Principles of Compilers. Lecture VIII, 2003/2004 A.Artale (1 Principle of Compilers Lecture VIII: Intermediate Code Generation Alessandro Artale Faculty of Computer Science Free

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1

NARESHKUMAR.R, AP\CSE, MAHALAKSHMI ENGINEERING COLLEGE, TRICHY Page 1 SEM / YEAR : VI / III CS2352 PRINCIPLES OF COMPLIERS DESIGN UNIT III INTERMEDIATE CODE GENERATION PART A 1. What are the benefits of intermediate code generation? (A.U May 2008) A Compiler for different

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Intermediate codes are machine independent codes, but they are close to machine instructions The given program in a source language is converted to an equivalent program in

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer

More information

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs(dot)nl Compilerconstructie najaar 2013 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs(dot)nl werkcollege 6, dinsdag 22 oktober 2013 Intermediate Code

More information

Syntax Directed Translation

Syntax Directed Translation Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax

More information

Intermediate Code Generation Part II

Intermediate Code Generation Part II 1 Intermediate Code Generation Part II Chapter 6 (1 st ed Chapter 8) COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 Advanced Intermediate Code Generation

More information

Intermediate Representations

Intermediate Representations Intermediate Representations A variety of intermediate representations are used in compilers Most common intermediate representations are: Abstract Syntax Tree Directed Acyclic Graph (DAG) Three-Address

More information

CSc 453 Intermediate Code Generation

CSc 453 Intermediate Code Generation CSc 453 Intermediate Code Generation Saumya Debray The University of Arizona Tucson Overview Intermediate representations span the gap between the source and target languages: closer to target language;

More information

Compilers. Compiler Construction Tutorial The Front-end

Compilers. Compiler Construction Tutorial The Front-end Compilers Compiler Construction Tutorial The Front-end Salahaddin University College of Engineering Software Engineering Department 2011-2012 Amanj Sherwany http://www.amanj.me/wiki/doku.php?id=teaching:su:compilers

More information

intermediate-code Generation

intermediate-code Generation intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language

More information

Syntax-Directed Translation

Syntax-Directed Translation Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

UNIT IV INTERMEDIATE CODE GENERATION

UNIT IV INTERMEDIATE CODE GENERATION UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of

More information

Module 25 Control Flow statements and Boolean Expressions

Module 25 Control Flow statements and Boolean Expressions Module 25 Control Flow statements and Boolean Expressions In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

CS2210: Compiler Construction. Code Generation

CS2210: Compiler Construction. Code Generation Modern Compiler Project Fortran program Fortran s Lexer, Parser, and Static Checker Intermediate Code Generator IR MIPS Code Generator MIPS code C program C s Lexer, Parser, and Static Checker Intermediate

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation Basic Approach and Application to Assignment and xpressions Array xpressions Boolean xpressions Copyright 2015, Pedro C. Diniz, all rights reserved. Students enrolled in the

More information

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7

CS322 Languages and Compiler Design II. Spring 2012 Lecture 7 CS322 Languages and Compiler Design II Spring 2012 Lecture 7 1 USES OF BOOLEAN EXPRESSIONS Used to drive conditional execution of program sections, e.g. IF (a < 17) OR (b = 12) THEN... ELSE...; WHILE NOT

More information

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore

Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Principle of Complier Design Prof. Y. N. Srikant Department of Computer Science and Automation Indian Institute of Science, Bangalore Lecture - 20 Intermediate code generation Part-4 Run-time environments

More information

Syntax-Directed Translation Part II

Syntax-Directed Translation Part II Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei shift/reduce conflict with

More information

CMPT 379 Compilers. Anoop Sarkar. 11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC

CMPT 379 Compilers. Anoop Sarkar.  11/13/07 1. TAC: Intermediate Representation. Language + Machine Independent TAC CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 11/13/07 1 TAC: Intermediate Representation Language Specific Language + Machine Independent Machine Dependent Front End AST Intermediate Code

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software ngineering Lab. The University of Aizu Japan Intermediate/Code Generation Source language Scanner (lexical analysis) tokens Parser (syntax analysis)

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp-16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Intermediate-Code Genera=on Intermediate representa=ons

More information

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 6: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev Fall 2015-2016 Compiler Principles Lecture 6: Intermediate Representation Roman Manevich Ben-Gurion University of the Negev Tentative syllabus Front End Intermediate Representation Optimizations Code Generation

More information

Chapter 6 Intermediate Code Generation

Chapter 6 Intermediate Code Generation Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate

More information

CS 406: Syntax Directed Translation

CS 406: Syntax Directed Translation CS 406: Syntax Directed Translation Stefan D. Bruda Winter 2015 SYNTAX DIRECTED TRANSLATION Syntax-directed translation the source language translation is completely driven by the parser The parsing process

More information

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV

Control Structures. Boolean Expressions. CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Control Structures Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures

More information

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Compiler Architecture Source language Scanner (lexical analysis) tokens Parser (syntax

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013 COMP455: COMPILER AND LANGUAGE DESIGN Dr. Alaa Aljanaby University of Nizwa Spring 2013 Chapter 1: Introduction Compilers draw together all of the theory and techniques that you ve learned about in most

More information

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False

More information

Time : 1 Hour Max Marks : 30

Time : 1 Hour Max Marks : 30 Total No. of Questions : 6 P4890 B.E/ Insem.- 74 B.E ( Computer Engg) PRINCIPLES OF MODERN COMPILER DESIGN (2012 Pattern) (Semester I) Time : 1 Hour Max Marks : 30 Q.1 a) Explain need of symbol table with

More information

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 16 : Intermediate Code IV. Department of Computer Science University of Arizona CSc 453 Compilers and Systems Software 16 : Intermediate Code IV Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2009 Christian Collberg Control Structures Control Structures

More information

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev

Fall Compiler Principles Lecture 5: Intermediate Representation. Roman Manevich Ben-Gurion University of the Negev Fall 2016-2017 Compiler Principles Lecture 5: Intermediate Representation Roman Manevich Ben-Gurion University of the Negev Tentative syllabus Front End Intermediate Representation Optimizations Code Generation

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Spring 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [25 points] Conser the grammar fragment below. It allows for the declaration of

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 19: Efficient IL Lowering 5 March 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 5 March 08 CS 412/413 Spring 2008 Introduction to Compilers 1 IR Lowering Use temporary variables for the translation

More information

COP5621 Exam 3 - Spring 2005

COP5621 Exam 3 - Spring 2005 COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred

More information

Languages and Compiler Design II IR Code Generation I

Languages and Compiler Design II IR Code Generation I Languages and Compiler Design II IR Code Generation I Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring 2010 rev.: 4/16/2010 PSU CS322 HM 1 Agenda Grammar G1

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the

More information

CS 4201 Compilers 2014/2015 Handout: Lab 1

CS 4201 Compilers 2014/2015 Handout: Lab 1 CS 4201 Compilers 2014/2015 Handout: Lab 1 Lab Content: - What is compiler? - What is compilation? - Features of compiler - Compiler structure - Phases of compiler - Programs related to compilers - Some

More information

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design PSD3A Principles of Compiler Design Unit : I-V 1 UNIT I - SYLLABUS Compiler Assembler Language Processing System Phases of Compiler Lexical Analyser Finite Automata NFA DFA Compiler Tools 2 Compiler -

More information

PRINCIPLES OF COMPILER DESIGN

PRINCIPLES OF COMPILER DESIGN PRINCIPLES OF COMPILER DESIGN 2 MARK QUESTIONS WITH ANSWERS UNIT I 1. What is a Complier? A Complier is a program that reads a program written in one language-the source language-and translates it in to

More information

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Compilers 5. Attributed Grammars Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A

More information

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Objective PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS Explain what is meant by compiler. Explain how the compiler works. Describe various analysis of the source program. Describe the

More information

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Dixita Kagathara Page 1

Dixita Kagathara Page 1 2014 Sem-VII Intermediate Code Generation 1) What is intermediate code? Intermediate code is: The output of the parser and the input to the Code Generator. Relatively machine-independent: allows the compiler

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 11a Intermediate Code Generation Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Declarations For each local name Creation of symbol-table entry Add type

More information

Writing Evaluators MIF08. Laure Gonnord

Writing Evaluators MIF08. Laure Gonnord Writing Evaluators MIF08 Laure Gonnord Laure.Gonnord@univ-lyon1.fr Evaluators, what for? Outline 1 Evaluators, what for? 2 Implementation Laure Gonnord (Lyon1/FST) Writing Evaluators 2 / 21 Evaluators,

More information

Principles of Compiler Design

Principles of Compiler Design Principles of Compiler Design Intermediate Representation Compiler Lexical Analysis Syntax Analysis Semantic Analysis Source Program Token stream Abstract Syntax tree Unambiguous Program representation

More information

Exercises II. Exercise: Lexical Analysis

Exercises II. Exercise: Lexical Analysis xercises II Text adapted from : Alessandro Artale, Free University of Bolzano les adapted from : nrico Cimitan, Università di Padova xercise: Lexical Analysis Describe the notions of token, token name,

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

A programming language requires two major definitions A simple one pass compiler

A programming language requires two major definitions A simple one pass compiler A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:

More information

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]

More information

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Intermediate representations and code generation Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Today Intermediate representations and code generation Scanner Parser Semantic

More information

CSCI565 Compiler Design

CSCI565 Compiler Design CSCI565 Compiler Design Fall 2015 Homework 2 - Solution Problem 1: Attributive Grammar and Syntax-Directed Translation [30 points] In this problem you ned to develop a grammar for array variable expressions

More information

Related Course Objec6ves

Related Course Objec6ves Syntax 9/18/17 1 Related Course Objec6ves Develop grammars and parsers of programming languages 9/18/17 2 Syntax And Seman6cs Programming language syntax: how programs look, their form and structure Syntax

More information

Lecture 14 Sections Mon, Mar 2, 2009

Lecture 14 Sections Mon, Mar 2, 2009 Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

5. Syntax-Directed Definitions & Type Analysis

5. Syntax-Directed Definitions & Type Analysis 5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/spring15/csci-ga.2130-001/lecture-5.pdf

More information

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum

IR Lowering. Notation. Lowering Methodology. Nested Expressions. Nested Statements CS412/CS413. Introduction to Compilers Tim Teitelbaum IR Lowering CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 19: Efficient IL Lowering 7 March 07 Use temporary variables for the translation Temporary variables in the Low IR store intermediate

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised: EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

More information

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code)

Compiler Theory. (Intermediate Code Generation Abstract S yntax + 3 Address Code) Compiler Theory (Intermediate Code Generation Abstract S yntax + 3 Address Code) 006 Why intermediate code? Details of the source language are confined to the frontend (analysis phase) of a compiler, while

More information

Concepts Introduced in Chapter 6

Concepts Introduced in Chapter 6 Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler

More information

Code Genera*on for Control Flow Constructs

Code Genera*on for Control Flow Constructs Code Genera*on for Control Flow Constructs 1 Roadmap Last *me: Got the basics of MIPS CodeGen for some AST node types This *me: Do the rest of the AST nodes Introduce control flow graphs Scanner Parser

More information

LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

More information

2.2 Syntax Definition

2.2 Syntax Definition 42 CHAPTER 2. A SIMPLE SYNTAX-DIRECTED TRANSLATOR sequence of "three-address" instructions; a more complete example appears in Fig. 2.2. This form of intermediate code takes its name from instructions

More information

LECTURE NOTES ON COMPILER DESIGN P a g e 2

LECTURE NOTES ON COMPILER DESIGN P a g e 2 LECTURE NOTES ON COMPILER DESIGN P a g e 1 (PCCS4305) COMPILER DESIGN KISHORE KUMAR SAHU SR. LECTURER, DEPARTMENT OF INFORMATION TECHNOLOGY ROLAND INSTITUTE OF TECHNOLOGY, BERHAMPUR LECTURE NOTES ON COMPILER

More information

CMPT 379 Compilers. Parse trees

CMPT 379 Compilers. Parse trees CMPT 379 Compilers Anoop Sarkar http://www.cs.sfu.ca/~anoop 10/25/07 1 Parse trees Given an input program, we convert the text into a parse tree Moving to the backend of the compiler: we will produce intermediate

More information

Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing. Lecture 11-12 Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11

More information

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation Acknowledgement CS3300 - Compiler Design Semantic Analysis - IR Generation V. Krishna Nandivada IIT Madras Copyright c 2000 by Antony L. Hosking. Permission to make digital or hard copies of part or all

More information

CS606- compiler instruction Solved MCQS From Midterm Papers

CS606- compiler instruction Solved MCQS From Midterm Papers CS606- compiler instruction Solved MCQS From Midterm Papers March 06,2014 MC100401285 Moaaz.pk@gmail.com Mc100401285@gmail.com PSMD01 Final Term MCQ s and Quizzes CS606- compiler instruction If X is a

More information

More On Syntax Directed Translation

More On Syntax Directed Translation More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes

More information

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating

Lecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating 1 ecture Overview Code generation in milestone 2 o Code generation for array indexing o Some rational implementation Over Express Over o Creating records for arrays o Short-circuiting Or o If statement

More information

Question Bank. 10CS63:Compiler Design

Question Bank. 10CS63:Compiler Design Question Bank 10CS63:Compiler Design 1.Determine whether the following regular expressions define the same language? (ab)* and a*b* 2.List the properties of an operator grammar 3. Is macro processing a

More information

Front End. Hwansoo Han

Front End. Hwansoo Han Front nd Hwansoo Han Traditional Two-pass Compiler Source code Front nd IR Back nd Machine code rrors High level functions Recognize legal program, generate correct code (OS & linker can accept) Manage

More information

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking CSE450 Translation of Programming Languages Lecture 11: Semantic Analysis: Types & Type Checking Structure Project 1 - of a Project 2 - Compiler Today! Project 3 - Source Language Lexical Analyzer Syntax

More information

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised:

EDA180: Compiler Construc6on Context- free grammars. Görel Hedin Revised: EDA180: Compiler Construc6on Context- free grammars Görel Hedin Revised: 2013-01- 28 Compiler phases and program representa6ons source code Lexical analysis (scanning) Intermediate code genera6on tokens

More information

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer. The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

More information

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F). CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

More information