- Consider the following two Postscript programs, with the image
each produces at the right:
%!PS-Adobe-2.0
/r 200 def
/iter 36 def
/theta 130 def
/star {
r 0 moveto
0 1 iter { % for
pop
r theta cos mul r theta sin mul rlineto
theta rotate
} for
stroke
} def
200 400 translate
star
showpage
%!PS-Adobe-2.0
/r 200 def
/iter 36 def
/theta 130 def
/star {
r 0 moveto
0 1 iter { % for
pop
r theta cos mul r theta sin mul rlineto
theta rotate
} for
stroke
} def
0 setlinejoin % joinlines at a point
200 400 translate
0 setgray
6 setlinewidth
gsave star grestore
0.9 setgray
4 setlinewidth
star
showpage
- Explain what the construct 0 1 iter { ... } for does.
- What is the pop inside the construct above for?
- The function star draws at the origin.
Exactly where on the page would this figure appear.
- Explain what pair of numbers
r theta cos mul r theta sin mul
refers to, using ordinary math notation (not postscript).
- The second program calls star twice.
Why is there a gsave ... grestore around the
first call, but not the second?
- Explain how the second program gets the effect shown.
(0 setgray sets the color to black.)
- Consider the following grammar:
P ----> E '$'
E ----> E '+' T | T
T ----> T '*' F | F
F ----> '(' E ')' | L
L ----> 'a' | 'b' | 'c' | ...
- Which are the terminal symbols? Which are the non-terminal
symbols?
- Is this grammar ambiguous? (Yes or No)
- Give the parse tree for the sentence:
( a + b ) * c $.
- Consider the diagram for an automaton that I will write on the
board.
- Is this an NFA or a DFA? How do you know?
- Use the algorithm in class to convert this to a DFA
accepting the same language.
- By any method, even ordinary English, describe the language
accepted by this automaton. (But be careful.)
- Using the reference sheet about the Quadruple Machine to
help you, translate the following simple program
by hand into quadruples. (All you need for an answer is the
list of quadruples.)
c = 1;
a = 4*c;
k = 3;
t = a/(8*k+1);
WRITE(t);
WRITEC(10); /* a newline */
- This is a question about translating arithmetic expressions
into quadruples. (It is about the portion of program that would
carry out the translation that you gave in the previous problem.)
- What special quantity is returned by many of the
functions carrying out the translation?
- How is a constant like 47 handled?
(What quadruple(s) is(are) generated?
Where is(are) it(they) generated?
What is returned after it(they) is(are) generated?)
- How is a single variable name like a in the
previous question handled. (Similar questions to the above.)
- This question is about how you might translate a goto
statement into quadruples. Suppose (as we did in class) that our
source language has a goto label statement, where
label is any identifier, and it occurs just
once followed by a colon in front of a statement. (One can have as
many of the gotos as one likes.) So the source might look like
the following, leaving out other statements, where there are statements
between the gotos:
goto L1;
...
goto L1;
...
goto L1;
...
L1: /* some statement */
...
goto L1;
...
goto L1;
- Explain what form the quadruples will take for these statements.
- Explain how the translation will take place, using a single pass.
(You must mention backpatching.)
- Consider the grammar rule
for an IF-ELSE statment:
ifstmt ----> "IF" "(" expr ")" stmt [ "ELSE" stmt ]
- Give the code for the function ifstmt
in your recursive descent parser. (Just give this code in rough form, using
whatever kind of helpful pseudocode you need. You can employ
the notation that you actually used in your own parser if you wish.)
- Given a specific IF-ELSE statement, show what form
the translating quadruples might take. This can include
things like "quads to evaluate expr", and "quads to evaluate stmt", etc.
You should focus on the jump quadruples that are needed.
- Show in outline form how to translate this general IF
statement into quadruples, as you did (may have done) for the
project for this course.
This amounts to additions to the code for the ifstmt
function in your parser.
You should focus particularly on how you keep track of
the numbers of key quadruples, and how you carry out the
backpatching. You can assume a function
nextQuad() that gives the quadruple number
of the next quadruple to be generated.
(Again, you can use any convenient pseudocode, and helpful
additional functions, as needed. You can also use the notation
you used in your own code, as long as you explain it.)
- Describe briefly how the storage needed for the call of
a function is handled in C/C++/Java at run time.
You should include parameters, the return address, local
variable, and storage that is created with malloc()
or new().