hw3sol2 - CS 6353 Compiler Construction, Homework #3 1....

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 6353 Compiler Construction, Homework #3 1. Consider the following grammar which defines the syntax for a simple array reference (e.g., temp[2,3]). A id [ L ] L int, L L int (a) Write an attributed grammar to compute the address of the array reference. You need to know the base address of the array. It can be obtained by the function address( name ), where name is the array name. Also, you need the size of each dimension of the array. It can be obtained by the function size( name , dim ), where name is the array name and dim is the specific array dimension. Assume that array indices are defined as that in C, starting from 0 for each dimension. Furthermore, you will need to get the value of the actual int and string of the actual id (which is the array name), and we assume that int.value and id.string give you these needed information. In your attributed grammar, you will need three attributes, addr , dim , and name , where addr is used to keep the information in address computation, dim is to keep track of the dimension currently being considered, and name is used to keep track of the array name. Please put the attribute actions next to the production rules above. Try not to use additional attributes and definitely not global variables. Just write the attributed grammar without considering additional techniques. If you need to index the symbols in a production, please do so in the production above. If a production does not need any action, just leave it empty. A id [ L ] A.address = address(id.string) + L.addr; L.name = id.string; L int , L 1 L.dim = L 1 .dim + 1; L 1 .name = L.name; L.addr = value(int)* size(L.name, L 1 .dim) + L 1 .addr; L int L.dim = 0; L.addr = value(int); (b) What is the type of each of the three attributes? Synthesized, L-inherited, or inherited. addr: synthesized dim: synthesized name: L-inherited (c) Can your attributed grammar be evaluated with the parsing process without having to have a separate pass to do the evaluation? If so, discuss how to do that with reasonable details. If not, explain why not. No. Name is L-inherited. The stack will have an arbitrary number of int in it and the location of id.string relative to the top of the stack will not be fixed.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Dragon book, second edition, Exercise 8.4.2. Consider the following program. for (i=2; i<=n; i++) a[i] = TRUE; count = 0; s = sqrt (n); for (i=2; i<=s; i++) if (a[i]) { count++; for (j=2*i; j<=n; j = j+1) a[j] = FALSE; } (a) Translate the program into three address code as defined in Section 8.2, dragon book. (1) i := 2; (2) if (!(i <= n)) goto (6); (3) a[i] := TRUE; (4) i := i + 1; (5) goto (2); (6) count := 0; (7) s := sqrt(n); (8) i := 2; (9) if (!(i <= s)) goto (19); (10) if(!a[i]) goto (17); (11) count := count + 1; (12) j := 2*i; (13) if (!(j <= n)) goto (17); (14) a[j] := FALSE; (15) j := j + 1; (16) goto (13); (17) i := i + 1; (18) goto (9); (19) exit (b) Identify all basic blocks in your three address code. B1: 1
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/25/2012 for the course CS 6413 taught by Professor Ye during the Spring '07 term at University of Texas at Dallas, Richardson.

Page1 / 18

hw3sol2 - CS 6353 Compiler Construction, Homework #3 1....

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online