programming4us
programming4us
DESKTOP

EXAMPLES for Syntax-Directed Definitions and Translations

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
7/24/2010 8:01:09 PM
6.12 EXAMPLES
Following are additional examples of syntax-directed definitions and translations.
EXAMPLE 1
Start example

Generate the three-address code for the following C program:

main()
{ int i = 1;
int a[10];
while(i <= 10)
a[i] = ;
}
End example

The three-address code for the above C program is:

  1. i = 1

  2. if i <= 10 goto(4)

  3. goto(8)

  4. t1 = i * width

  5. t2 = addr(a) width

  6. t2[t1] = 0

  7. goto(2)

where width is the number of bytes required for each element.

EXAMPLE 2
Start example

Generate the three-address code for the following program fragment:

while (A < C and B > D) do
if A = 1 then C = C+1
else
while A <= D do
A = A + 3
End example

The three-address code is:

  1. if a < c goto(3)

  2. goto(16)

  3. if b >d goto(5)

  4. goto(16)

  5. if a = 1 goto(7)

  6. goto(10)

  7. t1 = c+1

  8. c = t1

  9. goto(1)

  10. if a <= d goto

  11. goto(1)

  12. t2 = a+3

  13. a = t2

  14. goto(10)

  15. goto(1)

EXAMPLE 3
Start example

Generate the three-address code for the following program fragment, where a and b are arrays of size 20 � 20, and there are four bytes per word.

End example

begin
add = 0;
i = 1;
j = 1;
do
begin
add = add + a[i,j] * b[j,i]
i = i + 1;
j = j + 1;
end
while i <= 20 and j <= 20;
end

The three-address code is:

  1. add = 0

  2. i = 1

  3. j = 1

  4. t1 = i * 20

  5. t1 = t1 + j

  6. t1 = t1 * 4

  7. t2 = addr(a) 84

  8. t3 = t2[t1]

  9. t4 = j * 20

  10. t4 = t4 + i

  11. t4 = t4 * 4

  12. t5 = addr(b) 84

  13. t6 = t5[t4]

  14. t7 = t3 * t6

  15. t7 = add + t7

  16. t8 = i + 1

  17. i = t8

  18. t9 = j + 1

  19. j = t9

  20. if i <= 20 goto(22)

  21. goto NEXT

  22. if j <= 20 goto(4)

  23. goto NEXT

EXAMPLE 4
Start example

Consider the program fragment:

sum = 0
for(i = 1; i<= 20; i++)
sum = sum + a[i] + b[i];

and generate the three-address code for it. There are four bytes per word.

End example

The three address code is:

  1. sum = 0

  2. i = 1

  3. if i <= 20 goto(8)

  4. goto NEXT

  5. t1 = i+1

  6. i = t1

  7. goto(3)

  8. t2 = i * 4

  9. t3 = addr(a) 4

  10. t4 = t3[t2]

  11. t5 = i * 4

  12. t6 = addr(b) 4

  13. t7 = t6[t5]

  14. t8 = sum + t4

  15. t8 = t8 + t7

  16. sum = t8

  17. goto(5)


Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us
programming4us
 
 
programming4us