Spread Knowledge

CS606 - Compiler Construction - Lecture Handout 41

User Rating:  / 0

Related Content: CS606 - VU Lectures, Handouts, PPT Slides, Assignments, Quizzes, Papers & Books of Compiler Construction

Flow-of-Control Statements

We now use backpatching to translate flow-of-control statements in one pass. We will use the same list-handling procedures as before.

S → if E then S
| if E then S else S
| while E do S
| begin L end
| A
L → L ; S
| S

The semantic actions associated with each production are

Flow-of-Control Statements

semantic actions

Example: Let go through the example with the following input statement

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

The bottom-up parse will reduce the compound boolean expression a < b or c < d and e < f to E1 or M E2 which we have already covered in the previous example. We thus assume that the quads for the boolean expression have been generated. The sentential form at this stage is

sentential form

The semantic actions define the synthesized attributes E.truelist=[100,104] and E.falselist=[103,105].

We now trace the remaining bottom up parse and execute the semantic actions:

semantic actions1

The array of quadruples at this stage will contain

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 _
109 x=y-z

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. The actual mechanics will be covered in the handout for the syntax-directed translation phase of the course project