Compiler Construction Notes: Bottom-Up Parse and Three-Address Code, Study notes of Compiler Construction

These notes provide an explanation of the bottom-up parse of an assignment statement and its translation into three-address code. The document also discusses the implementation of three-address codes as a set of quadruples and the use of a linked list to overcome size limitations. Lastly, it covers the syntax-directed translation scheme for flow-of-control statements.

Typology: Study notes

2011/2012

Uploaded on 11/06/2012

asim.amjid
asim.amjid ๐Ÿ‡ต๐Ÿ‡ฐ

4.4

(47)

41 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes
105
L
Le
ec
ct
tu
ur
re
e
3
37
7
Here is the bottom-up parse of the assignment statement a = b*-c + b*-c and the
syntax-directed translation into three-address code.
Parser action attribute Three- address code
id=id โˆ— โ€“id + id โˆ— โ€“id
id=E1 โˆ— โ€“id + id โˆ— โ€“id E1.place = b
id=E1 โˆ— โ€“E2 + id โˆ— โ€“id E2.place = c
id=E1 โˆ— E2 + id โˆ— โ€“id E2.place = t1 t1 = โ€“ c
id=E1 + id โˆ— โ€“id E1.place = t2 t2 = bโˆ—t1
id=E1 + E2 โˆ— โ€“id E2.place = b
id=E1 + E2 โˆ— โ€“E3 E3.place = c
id=E1 + E2 โˆ— E3 E3.place = t3 t3 = โ€“ c
id=E1 + E2 E2.place = t4 t4 = bโˆ—t3
id=E1 E1.place = t5 t5 = t2+t4
S a = t5
Representing Linear Codes
Three-address codes are often implemented as a set of quadruples. Each quadruple has
four fields: an operator, two operands (or sources) and a destination. In C++, for
example, one can design a quadruple class and then declare a simple array of quadruples.
This leads to the following arrangement; the index of the array element acts as the
number of quadruple generated.
Target Op Arg1 Arg2
t1 โ† 2
t2 โ† y
t3 ร— t1 t2
t4 โ† x
t5 โ€“ t4 t3
pf3

Partial preview of the text

Download Compiler Construction Notes: Bottom-Up Parse and Three-Address Code and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 3 377

Here is the bottom-up parse of the assignment statement a = b-c + b-c** and the syntax-directed translation into three-address code.

Parser action attribute Three- address code id=id โˆ— โ€“id + id โˆ— โ€“id id=E1 โˆ— โ€“id + id โˆ— โ€“id E1.place = b id=E1 โˆ— โ€“E2 + id โˆ— โ€“id E2.place = c id=E1 โˆ— E2 + id โˆ— โ€“id E2.place = t1^ t1 = โ€“ c id=E1 + id โˆ— โ€“id E1.place = t2^ t2 = bโˆ—t id=E1 + E2 โˆ— โ€“id E2.place = b id=E1 + E2 โˆ— โ€“E3 E3.place = c id=E1 + E2 โˆ— E3 E3.place = t3^ t3 = โ€“ c id=E1 + E2 E2.place = t4 (^) t4 = bโˆ—t id=E1 E1.place = t5 t5 = t2+t S a = t

Representing Linear Codes

Three-address codes are often implemented as a set of quadruples. Each quadruple has four fields: an operator, two operands (or sources) and a destination. In C++, for example, one can design a quadruple class and then declare a simple array of quadruples. This leads to the following arrangement; the index of the array element acts as the number of quadruple generated.

Target Op Arg1 Arg t1 โ† 2 t2 โ† y t3 (^) ร— t1 t t4 โ† x t5 โ€“ t4 t

An array of pointers to quads can be employed which leads to the following structure:

Both simple array and array of pointers have maximum size limitation. This limitation can be overcome by using a linked list of quads:

Flow-of-Control Statements

We now use the syntax-directed translation scheme for the flow-of-control statements found in most procedural programming languages.

S โ†’ if E then S 1 | if E then S 1 else S 2 | while E do S 1

where E is a boolean expression. Consider the statement

if c < d then x = y + z else x = y โ€“ z

-^ t^1 โ†^2 -^ t^2 โ†^ y

  • t 3 ร— t 1 t (^2) -^ t^4 โ†^ x -^ t^5 โ€“^ t^4 t 3 -^ t^1 โ†^2 -^ t^2 โ†^ y -^ t^3 ร—^ t^1 t^2 -^ t^4 โ†^ x -^ t^5 โ€“^ t^4 t^3