1 / 98

990 likes | 1.19k Vues

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

Télécharger la présentation
## Compiler Construction

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**Compiler Construction**Sohail Aslam Lecture 39**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**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**Boolean Experssions**Consider the grammar E → E or E|E and E|not E|( E ) | id relop id | true | false**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**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**Boolean Experssions**E → id1 relop id2 E.code = gen(‘if’ id1 relop id2 ‘goto’ E.true) || gen(‘goto’ E.false)**Boolean Experssions**E → true E.code = gen(‘goto’ E.true) E → false E.code = gen(‘goto’ E.false)**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**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**Boolean Experssions**E → not E1 E1.true = E.false E1.false = E.true E.code = E1.code**Boolean Experssions**E → ( E1 ) E1.true = E.true E1.false = E.false E.code = E1.code**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**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**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**Consider the while statement**while a < b if c < d then x = y + z else x = y – z**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**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**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**Boolean Experssions**• However, by using a technique called back-patching, we can generate code in one pass.**Boolean Experssions**• we will generate the jumps with targets temporarily left unspecified**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)**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)**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:**Backpatching**1. makelist(i)creates and returns a new list containing only i, the index of quadruple**Backpatching**2. merge(p1, p2) concatenates lists pointed to by p1 and p2 and returns the concatenated list**Backpatching**3. backpatch(p, i) inserts i as the target label for each of the goto statements on list pointed to by p**Boolean Expressions**• We now construct a translation scheme suitable for producing quads (IR) for boolean expressions during bottom-up parsing**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**Boolean Expressions**• We will associate synthesized attributes truelist and falselist with the nonterminal E • Incomplete jumps will be placed on these list**Boolean Expressions**• We associate the semantic action{ M.quad = nextquad() }with the production M → e**Boolean Expressions**• The function nextquad()returns the index of the next quadruple to follow**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**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**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**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**Compiler Construction**Sohail Aslam Lecture 40**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**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.**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.**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**Boolean Experssions**E → not E1 { E.truelist = E1.falselist; E.falselist = E1.truelist;}**Boolean Experssions**E → ( E1 ) { E.truelist = E1.truelist; E.falselist = E1.falselist;}**Boolean Experssions**E → id1 relop id2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id1 relop id2 ‘goto _’) ;emit(‘goto _’ ); }**Boolean Experssions**E → true { E.truelist = makelist(nextquad()); emit(‘goto _’ ); }**Boolean Experssions**E → false { E.falselist = makelist(nextquad()); emit(‘goto _’ ); }**Boolean Expressions**M→ e { M.quad = nextquad(); }**Backpatching**• consider again, the boolean expression a < b or c < d and e < f • We carry out a bottom-up parse**Backpatching**• In response to reduction of a < b to E, the two quadruples 100: if a < b goto _ 101: goto _ are generated**Recall**E → id1 relop id2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id1 relop id2 ‘goto _’) ;emit(‘goto _’ ); } View this in a parse tree

More Related