Next: Exercise 3.
Up: Quiz6
Previous: Exercise 1.
Some languages such as PL1 permit a list of names to be given a list of attributes.
The grammar G below abstracts the problem.
D |
![$\displaystyle \longmapsto$](img4.png) |
namelist attriblist![$\displaystyle \bf ;$](img5.png) |
namelist |
![$\displaystyle \longmapsto$](img4.png) |
, namelist |
namelist |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf id$](img6.png) |
attriblist |
![$\displaystyle \longmapsto$](img4.png) |
A attriblist |
attriblist |
![$\displaystyle \longmapsto$](img4.png) |
A |
A |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf long$](img7.png) |
A |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf double$](img8.png) |
A |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf single$](img9.png) |
A |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf integer$](img10.png) |
A |
![$\displaystyle \longmapsto$](img4.png) |
![$\displaystyle \bf float$](img11.png) |
|
|
where D, namelist,
attriblist and A are nonterminals
and where id, long, double, single, integer, float and ;
are terminals. As usual id is the identifier token.
For instance each of the following lines is a declaration
generated by G.
i, j single integer;
u, v, w double float;
Give a syntax-directed definition
for declarations generated by G.
Here are some hints.
- You may simply write N and T instead of namelist and
attriblist respectively.
- The code in the actions does not need to be C code but may be any pseudo-code
that is convenient to you.
- You may consider that id has an attribute
.name (as usual)
giving the lexical value of id.
- Moreover you may assume that every variable in the symbol table may be
associated with a list of attributes by an instruction like
enter(
.name, T.list)
- The instruction T.list := makelist(A.attrib) will
create the list consisting of the single item A.attrib.
- The instruction T.list := cons(A.attrib, T.list) will
add the element A.attrib at the beginning of the list T.list.
Answer 2
![\fbox{
\begin{minipage}{12 cm}
\begin{center}
\begin{tabular}{llll}
D & $\longm...
...$\ $A.attrib$\ := {\bf float} $\}$\ \\
\end{tabular}\end{center}\end{minipage}}](img13.png)
Next: Exercise 3.
Up: Quiz6
Previous: Exercise 1.
Marc Moreno Maza
2004-12-02