1 / 98

# Compiler Construction

Compiler Construction. Sohail Aslam Lecture 39. Boolean Experssions. In programming languages, boolean expressions have two primary purposes: compute logical values x = a &lt; b &amp;&amp; d &gt; e conditional expressions in flow-of-control statements. Boolean Experssions.

Télécharger la présentation

## Compiler Construction

E N D

### Presentation Transcript

1. Compiler Construction Sohail Aslam Lecture 39

2. Boolean Experssions In programming languages, boolean expressions have two primary purposes: • compute logical valuesx = a < b && d > e • conditional expressions in flow-of-control statements

3. Boolean Experssions In programming languages, boolean expressions have two primary purposes: • compute logical valuesx = a < b && d > e • conditional expressions in flow-of-control statements

4. Boolean Experssions Consider the grammar E → E or E|E and E|not E|( E ) | id relop id | true | false

5. Boolean Experssions • We will implement the boolean expression by flow of control method • I.e., representing the value of a boolean expression by a position reached in the program

6. Boolean Experssions • We will implement the boolean expression by flow of control method • I.e., representing the value of a boolean expression by a position reached in the program

7. Boolean Experssions E → id1 relop id2 E.code = gen(‘if’ id1 relop id2 ‘goto’ E.true) || gen(‘goto’ E.false)

8. Boolean Experssions E → true E.code = gen(‘goto’ E.true) E → false E.code = gen(‘goto’ E.false)

9. Boolean Experssions E → E1 or E2 E1.true = E.true E1.false = newlabel()E2.true = E.true E2.false = E.falseE.code = E1.code ||gen(E1.false ‘:’) || E2.code

10. Boolean Experssions E → E1 and E2 E1.true = newlabel()E1.false = E.false E2.true = E.true E2.false = E.falseE.code = E1.code ||gen(E1.true ‘:’) || E2.code

11. Boolean Experssions E → not E1 E1.true = E.false E1.false = E.true E.code = E1.code

12. Boolean Experssions E → ( E1 ) E1.true = E.true E1.false = E.false E.code = E1.code

13. Boolean Experssions • Consider the expression a < b or c < d and e < f • Suppose the true and false exits for the entire expression are Ltrue and Lfalse

14. Boolean Experssions • Consider the expression a < b or c < d and e < f • Suppose the true and false exits for the entire expression are Ltrue and Lfalse

15. if a < b or c < d and e < f if a < b goto Ltrue goto L1L1: if c < d goto L2 goto LfalseL2: if e < f goto Ltrue goto Lfalse

16. Consider the while statement while a < b if c < d then x = y + z else x = y – z

17. while a < b if c < d then x = y + z else x = y – z L1: if a < b goto L2 goto Lnext L2: if c < d goto L3 goto L4 L3: t1 = y + z x = t1 goto L1 L4: t2 = y – z x = t2 goto L1 Lnext: nop

18. Implementation • The easiest way to implement syntax-directed definitions is to use two passes • construct a syntax tree for the input • walk the tree in depth-first order

19. Boolean Experssions • The problem in generating three-address code in one pass is that we may not know the labelsthat the control must go to when we generate jump statements

20. Boolean Experssions • However, by using a technique called back-patching, we can generate code in one pass.

21. Boolean Experssions • we will generate the jumps with targets temporarily left unspecified

22. Boolean Experssions • Each such statement will be put on a list of goto statements • We will fill the labels when the proper label can be determined (backpatch)

23. Boolean Experssions • Each such statement will be put on a list of goto statements • We will fill the labels when the proper label can be determined (backpatch)

24. Backpatching • Assume that the quadruples are put into an array • Labels will be indicies into this array • To manipulate list of labels, we will use three functions:

25. Backpatching 1. makelist(i)creates and returns a new list containing only i, the index of quadruple

26. Backpatching 2. merge(p1, p2) concatenates lists pointed to by p1 and p2 and returns the concatenated list

27. Backpatching 3. backpatch(p, i) inserts i as the target label for each of the goto statements on list pointed to by p

28. Boolean Expressions • We now construct a translation scheme suitable for producing quads (IR) for boolean expressions during bottom-up parsing

29. The grammar we use is E → E1or M E2|E1and M E2| not E1| ( E1)| id1 relop id2| true| false M →e{M.quad = nextquad()} M is the marker non-terminal M.quad records the number of first statement of E2

30. Boolean Expressions • We will associate synthesized attributes truelist and falselist with the nonterminal E • Incomplete jumps will be placed on these list

31. Boolean Expressions • We associate the semantic action{ M.quad = nextquad() }with the production M → e

32. Boolean Expressions • The function nextquad()returns the index of the next quadruple to follow

33. Boolean Expressions: AND E → E1 and M E2{backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} Let us look at the mechanics

34. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} If E1 is false, E is also false

35. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} so the statements on E1.falselist become part of E.falselist

36. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} If E1 is true, we must test E2

37. Compiler Construction Sohail Aslam Lecture 40

38. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} So the target for E1.truelist must be the beginning of code generated for E2

39. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} This target is obtained using the markernonterminal M.

40. Boolean Expressions E → E1 and M E2{ backpatch(E1.truelist, M.quad); E.truelist = E2.truelist; E.falselist = merge(E1.falselist, E2.falselist);} M.quad records the number of the firststatement of E2.code.

41. Boolean Expressions: OR E → E1 or M E2{ backpatch(E1.falselist, M.quad); E.truelist = merge(E1.truelist, E2.truelist); E.falselist = E2.falselist;} If E1 is false, need to test E2

42. Boolean Experssions E → not E1 { E.truelist = E1.falselist; E.falselist = E1.truelist;}

43. Boolean Experssions E → ( E1 ) { E.truelist = E1.truelist; E.falselist = E1.falselist;}

44. Boolean Experssions E → id1 relop id2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id1 relop id2 ‘goto _’) ;emit(‘goto _’ ); }

45. Boolean Experssions E → true { E.truelist = makelist(nextquad()); emit(‘goto _’ ); }

46. Boolean Experssions E → false { E.falselist = makelist(nextquad()); emit(‘goto _’ ); }