Compiler Construction Sohail Aslam Lecture 41 FlowofControl Statements

  • Slides: 21
Download presentation
Compiler Construction Sohail Aslam Lecture 41

Compiler Construction Sohail Aslam Lecture 41

Flow-of-Control Statements S → if E then S | if E then S else

Flow-of-Control Statements S → if E then S | if E then S else S | while E do S | begin L end | A S denotes a statement L→ L; S | S L is a statementlist A is assignment 2

Semantic Actions S → if E then M 1 S 1 N else M

Semantic Actions S → if E then M 1 S 1 N else M 2 S 2 { } backpatch(E. truelist, M 1. quad); backpatch(E. falselist, M 2. quad); S. nextlist = merge(S 1. nextlist, merge( N. nextlist, S 2. nextlist)); If E true, jump to M 1. quad which is N is is marker non-terminal to introduce start of code for S 1 S 2 jump over code 3

Semantic Actions N→e { N. nextlist = makelist(next. Quad()); emit(‘goto_’); } The attribute N.

Semantic Actions N→e { N. nextlist = makelist(next. Quad()); emit(‘goto_’); } The attribute N. nextlist records the quad number of the goto it generates 4

Semantic Actions M→e { M. quad = next. Quad(); } 5

Semantic Actions M→e { M. quad = next. Quad(); } 5

Semantic Actions S → if E then M S 1 { } backpatch(E. truelist,

Semantic Actions S → if E then M S 1 { } backpatch(E. truelist, M. quad); S. nextlist = merge( E. falselist, S 1. nextlist); If E is true, jump to M. quad which is start of code for S 1 6

Semantic Actions S → while M 1 E do M 2 S 1 {

Semantic Actions S → while M 1 E do M 2 S 1 { } backpatch(S 1. nextlist, M 1. quad); backpatch(E. truelist, M 2. quad); S. nextlist = E. falselist; emit( ‘goto’ M 1. quad); 7

Semantic Actions S → begin L end { S. nextlist = L. nextlist; }

Semantic Actions S → begin L end { S. nextlist = L. nextlist; } 8

Semantic Actions S→A { S. nextlist = nil; } initializes S. nextlist to an

Semantic Actions S→A { S. nextlist = nil; } initializes S. nextlist to an empty list 9

Semantic Actions L → L 1 ; M S { backpatch(L 1. nextlist, M.

Semantic Actions L → L 1 ; M S { backpatch(L 1. nextlist, M. quad); L. nextlist = S. nextlist; } Statement following L 1 in order of execution is beginning of S 10

Semantic Actions L→S { L. nextlist = S. nextlist; } 11

Semantic Actions L→S { L. nextlist = S. nextlist; } 11

Example if a<b or c<d and e<f then x=y+z else x=y-z if E 1

Example if a<b or c<d and e<f then x=y+z else x=y-z if E 1 or M E 2 then x=y+z else x=y-z 12

if E 1 or M E 2 then x=y+z else x=y-z if E then

if E 1 or M E 2 then x=y+z else x=y-z if E then x=y+z else x=y-z { E. truelist=[100, 104] E. falselist=[103, 105] } 13

100 101 102 103 104 105 106 107 108 109 if a < b

100 101 102 103 104 105 106 107 108 109 if a < b goto _ goto 102 if c < d goto 104 goto _ if e < f goto _ goto_ 14

if E then M 1 x=y+z else x=y-z M 1 → { M 1.

if E then M 1 x=y+z else x=y-z M 1 → { M 1. quad = 106 } if E then M 1 A else x=y-z { emit(‘x=y+z’) } if E then M 1 S 1 else A { S 1. nextlist = nil} if E then M 1 S 1 N else x=y-z { N. nextlist = [107] emit(‘goto _’ } A → x=y+z S 1 → A N→ 15

100 101 102 103 104 105 106 107 108 109 if a < b

100 101 102 103 104 105 106 107 108 109 if a < b goto _ goto 102 if c < d goto 104 goto _ if e < f goto _ x=y+z goto _ 16

if E then M 1 S 1 N else M 2 x=y-z { M

if E then M 1 S 1 N else M 2 x=y-z { M 2. quad = 108 } M 2 → if E then M 1 S 1 N else M 2 A { emit(‘x=y-z’) } if E then M 1 S 1 N else M 2 S 2 { S 2. nextlist = nil } A → x=y S 2 → A S { backpatch([100, 104], 106) backpatch([103, 105], 108) S. nextlist=[107]} 17

100 101 102 103 104 105 106 107 108 109 if a < b

100 101 102 103 104 105 106 107 108 109 if a < b goto 106 _ goto 102 if c < d goto 104 goto 108 _ if e < f goto 106 _ goto 108 _ x=y+z goto _ x=y-z 18

100 101 102 103 104 105 106 107 108 109 if a < b

100 101 102 103 104 105 106 107 108 109 if a < b goto 106 goto 102 if c < d goto 104 goto 108 if e < f goto 106 goto 108 x=y+z goto _ x=y-z 19

Semantic Actions in YACC The syntax-directed translation statements can be conveniently specified in YACC

Semantic Actions in YACC The syntax-directed translation statements can be conveniently specified in YACC The %union will require more fields because the attributes vary 20

Semantic Actions in YACC The actual mechanics will be covered in the handout for

Semantic Actions in YACC The actual mechanics will be covered in the handout for the syntax-directed translation phase of the course project 21