The following grammar describes (at a high level) part of the syntax of a
typical ALGOL-like language.
(Notation: non-terminals are in CAPITALS and terminals are in lower case
The details of ASSIGNMENT are uninteresting.)
PROGRAM --> procedure STMT_LIST
STMT_LIST --> STMT STMT_LIST
| STMT
STMT --> ASSIGNMENT
| do var = constant to constant begin STMT_LIST end
| if var then STMT_LIST else STMT_LIST endif
As a first step toward understanding a program, it is interesting to
understand the number of times that each statement in the program is
executed. It is equally important to understand the total execution time.
To estimate these numbers for the above language, we can use the following
simple rules:
- the number of times that the body of a do-loop executes can be computed from the two constants that govern the range of its index variable;
- the then-part of an if statement executes half the time; similarly, the else-part executes half the time; and
- an ASSIGNMENT statement takes one unit of execution time.
- Write a set of syntax--directed tranlation rules (using YACC--like
notation or the notation in the text) that annotates each non--terminal
node with the estimated cost (in terms of execution units)
using the rules above. Be clear about what assumptions you are making with
respect to how you compute these values and what attributes are associated
with the different symbols.
- Draw and annotate (with your attributes) this parse tree using
your rules from part (a):
procedure
do i = 1 to 100 begin
if x then
do j = 1 to 50 begin
ASSIGNMENT
end
else
ASSIGNMENT
endif
ASSIGNMENT
end
Solutions