Docsity

Semantic Actions in Compiler Construction: YACC Implementation, Slides of Compiler Construction

Partial preview of the text

Download Semantic Actions in Compiler Construction: YACC Implementation and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 41

2

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

S denotes a statement L is a statementlist A is assignment

4

Semantic Actions

N → ε

N.nextlist = makelist(nextQuad());
emit(‘goto_’);
The attribute N.nextlist records the
quad number of the goto it generates

5

Semantic Actions

M → ε

M.quad = nextQuad();

7

Semantic Actions

S → while M 1 E do M 2 S 1

backpatch(S1.nextlist, M 1 .quad);
backpatch(E.truelist, M 2 .quad);
S.nextlist = E.falselist;
emit( ‘goto’ M 1 .quad);

8

Semantic Actions

S → begin L end

S.nextlist = L.nextlist;

10

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

11

Semantic Actions

L → S

L.nextlist = S.nextlist;

13

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] }

14

100 if a < b goto _

101 goto 102

102 if c < d goto 104

103 goto _

104 if e < f goto _

105 goto_

106

107

108

109

16

100 if a < b goto _

101 goto 102

102 if c < d goto 104

103 goto _

104 if e < f goto _

105 goto _

106 x=y+z

107 goto _

108

109

17

if E then M 1 S 1 N else M 2 x=y-z M 2 → ε { M 2 .quad = 108 }

if E then M 1 S 1 N else M 2 A A → x=y-z { emit(‘x=y-z’) }

if E then M 1 S 1 N else M 2 S 2 S (^2) A { S 2 .nextlist = nil }

S { backpatch([100,104],106) backpatch([103,105],108) Docsity.com

19

100 if a < b goto 106

101 goto 102

102 if c < d goto 104

103 goto 108

104 if e < f goto 106

105 goto 108

106 x=y+z

107 goto _

108 x=y-z

109

20

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