

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Sohail Aslam Compiler Construction Notes Set:6-
Three-Address Statement Types
Prior to proceeding with flow-of-control construct, here are the types of three-Address statements that we will use
We associate with a boolean expression E two labels (attributes); E.true and E.false. The control flows to E.true if the expression evaluates to true, to E.false otherwise. Following is syntax-directed translation for
S → if E then S 1
E.true = newlabel() E.false = S.next S 1 .next = S.next S.code = E.code || gen(E.true ‘:’) || S 1 .code
The attribute “ next ” records the label of the next statement to execute. “ code ” is string-valued attribute that holds the actual code generated in the form of a character string. The code can be eventually written out to a file. The || is the string concatenation operator, that is “hello”|| “ world” will yield the combined string “hello world”.
Sohail Aslam Compiler Construction Notes Set:6-
Suppose E is “a < b”. E.code would be
if a < b goto E.true goto E.false
We will discuss semantic rules for boolean expressions shortly
The syntax-directed translation for
S → if E then S 1 else S 2 is
E.true = newlabel() E.false = newlabel() S 1 .next = S.next S 2 .next = S.next S.code = E.code || gen(E.true ‘:’) || S 1 .code || gen(‘goto’ S.next) || gen(E.false ‘:’) || S 2 .code
Similarly, the syntax-directed translation for the while loop is
S → while E do S 1
S.begin = newlabel() E.true = newlabel() E.false = S.next S 1 .next = S.begin S.code = gen(S.begin ‘:’) || E.code || gen(E.true ‘:’) || S 1 .code || gen(‘goto’ S.begin)