CS 6353 Compiler Construction, Homework #3
Consider the following grammar which defines the syntax for a simple array reference (e.g.,
id [ L ]
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(
is the array name. Also, you need the size of each dimension of the array. It
can be obtained by the function size(
is the array name and
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
In your attributed grammar, you will need three attributes,
is used to keep the information in address computation,
is to keep track of the
dimension currently being considered, and
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.
id [ L ]
A.address = address(id.string) + L.addr;
L.name = id.string;
int , L
L.dim = L
.dim + 1; L
.name = L.name;
L.addr = value(int)* size(L.name, L
.dim) + L
L.dim = 0; L.addr = value(int);
(b) What is the type of each of the three attributes? Synthesized, L-inherited, or 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.