Compiler Construction Sohail Aslam Lecture 40 Boolean Expressions
Compiler Construction Sohail Aslam Lecture 40
Boolean Expressions E → E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist = E 2. truelist; E. falselist = merge(E 1. falselist, E 2. falselist); } So the target for E 1. truelist must be the beginning of code generated for E 2 2
Boolean Expressions E → E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist = E 2. truelist; E. falselist = merge(E 1. falselist, E 2. falselist); } This target is obtained using the marker nonterminal M. 3
Boolean Expressions E → E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist = E 2. truelist; E. falselist = merge(E 1. falselist, E 2. falselist); } M. quad records the number of the first statement of E 2. code. 4
Boolean Expressions: OR E → E 1 or M E 2 { } backpatch(E 1. falselist, M. quad); E. truelist = merge(E 1. truelist, E 2. truelist); E. falselist = E 2. falselist; If E 1 is false, need to test E 2 5
Boolean Experssions E → not E 1 { } E. truelist = E 1. falselist; E. falselist = E 1. truelist; 6
Boolean Experssions E → ( E 1 ) { E. truelist = E 1. truelist; E. falselist = E 1. falselist; } 7
Boolean Experssions E → id 1 relop id 2 { E. truelist = makelist(nextquad()); E. falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’) ; emit(‘goto _’ ); } 8
Boolean Experssions E → true { E. truelist = makelist(nextquad()); emit(‘goto _’ ); } 9
Boolean Experssions E → false { E. falselist = makelist(nextquad()); emit(‘goto _’ ); } 10
Boolean Expressions M→e { M. quad = nextquad(); } 11
Backpatching § consider again, the boolean expression a < b or c < d and e < f § We carry out a bottom-up parse 12
Backpatching § In response to reduction of a < b to E, the two quadruples 100: if a < b goto _ 101: goto _ are generated 13
Recall E → id 1 relop id 2 { E. truelist = makelist(nextquad()); E. falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’) ; emit(‘goto _’ ); } View this in a parse tree 14
E. t = {100} E. f = {101} a < b or c < d and e < f 15
Backpatching § The marker non-terminal M in the production E → E 1 or M E 2 records the value of nextquad which at this time is 102. 16
M. q = 102 e E. t = {100} E. f = {101} a < b or c < d and e < f 17
Backpatching § The reduction of c < d to E, the two quadruples 102: if c < d goto _ 103: goto _ are generated 18
M. q = 102 e E. t = {100} E. f = {101} a < b E. t = {102} E. f = {103} or c < d and e < f 19
Backpatching § The marker non-terminal M in the production E → E 1 and M E 2 records the value of nextquad which at this time is 104. 20
M. q = 102 e E. t = {100} E. f = {101} a < b M. q = 104 e E. t = {102} E. f = {103} or c < d and e < f 21
Backpatching § Reducing e < f to E, generates 104: if e < f goto _ 105: goto _ 22
M. q = 102 e E. t = {100} E. f = {101} a < b M. q = 104 e E. t = {102} E. f = {103} or c < d and E. t = {104} E. f = {105} e < f 23
Backpatching § We now reduce by the production E → E 1 and M E 2 24
M. q = 102 e E. t = {100} E. f = {101} a < E. t = {104} E. f = {103, 105} b M. q = 104 e E. t = {102} E. f = {103} or c < d and E. t = {104} E. f = {105} e < f 25
Semantic Actions E → E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist = E 2. truelist; E. falselist = merge(E 1. falselist, E 2. falselist); } 26
Backpatching The semantic action calls backpatch({102}, 104) where {102} denotes E 1. truelist containing only 102 27
Backpatching The six statements generated so far are thus 100: 101: 102: 103: 104: 105: if a goto if c goto if e goto < b goto _ _ < d goto _ _ < f goto _ _ 28
Backpatching The semantic action calls backpatch({102}, 104) which fills in 104 in statement 102. 29
Backpatching The call fills in 104 in statement 102 100: 101: 102: 103: 104: 105: if a goto if c goto if e goto < b goto _ _ < d goto 104 _ < f goto _ _ 30
Semantic Actions E → E 1 and M E 2 { backpatch(E 1. truelist, M. quad); E. truelist = E 2. truelist; E. falselist = merge(E 1. falselist, E 2. falselist); } 31
M. q = 102 e E. t = {100} E. f = {101} a < b and M. q = 104 e E. t = {102} 103 E. f = {103} or E. t = {104} E. f = {103, 105} c < d E. t = {104} 104 105 E. f = {105} e < f 32
Backpatching § We now reduce by the production E → E 1 or M E 2 33
E. t = {100, 104} E. f = {103, 105} M. q = 102 e E. t = {100} E. f = {101} a < b and M. q = 104 e E. t = {102} E. f = {103} or E. t = {104} E. f = {103, 105} c < d E. t = {104} E. f = {105} e < f 34
Semantic Actions E → E 1 or M E 2 { } backpatch(E 1. falselist, M. quad); E. truelist = merge(E 1. truelist, E 2. truelist); E. falselist = E 2. falselist; 35
Backpatching The semantic action calls backpatch({101}, 102) which fills in 102 in statement 101. 36
Backpatching 100: 101: 102: 104 103: 104: 105: if a < b goto _ 102 goto if c < d goto _ if e < f goto _ 37
Backpatching 100: 101: 102: 103: 104: 105: if a goto if c goto if e goto < b goto _ 102 < d goto 104 _ < f goto _ _ These instructions will have their targets filled later in the compilation 38
Semantic Actions E → E 1 or M E 2 { } backpatch(E 1. falselist, M. quad); E. truelist = merge(E 1. truelist, E 2. truelist); E. falselist = E 2. falselist; 39
E. t = {100, 104} E. f = {103, 105} or M. q = 102 e E. t = {100} E. f = {101} a < b E. t = {104} E. f = {103, 105} and M. q = 104 e E. t = {102} E. f = {103} c < d E. t = {104} E. f = {105} e < f 40
Flow-of-Control Statements § We now use backpatching to translate flow-of-control statements in one pass § We will use the same listhandling procedures as before 41
Flow-of-Control Statements § We now use backpatching to translate flow-of-control statements in one pass § We will use the same listhandling procedures as before 42
Flow-of-Control Statements S → if E then S | if E then S else S | while E do S | begin L end | A L→ L; S | S 43
- Slides: 43