CS 3723-001 Programming Languages
Fall 2002 -- Mid-term Exam
Directions: Use your own paper for answers. When you are done
you may keep this exam sheet and should pick up an answer sheet.
- Consider a shift-reduce parser as we had in a recitation.
- In such a parser, say what is being shifted
and where it is being shifted to.
- Suppose the grammar rule
E ---> E '+' T
is being used for a reduction.
Describe briefly what happens during this reduction.
- How does the parser know whether to shift or to reduce?
- Consider the following slightly modified
grammar rule from the course parser assignment:
W ---> '{' E '?' { S } '}'
- What does the portion { S } mean on the right side?
- What construct is this rule describing?
- Write code or pseudo-code for a function W
that could work as part of a recursive descent parser for a grammar
including this grammar rule. You should assume that a scanner is
given to return tokens, and that separate functions E()
and S() are available. Use any convenient notation
or language. Do not try to supply I/O statements, header files
or other details. Just give the bare code for the parser function
W.
- Suppose you are generating MIPS code for an if-then-else
construct, using the grammar rule
I ---> '[' E '?' { S } ':' { S } ']'
-
Show what additional MIPS instructions must be generated inside
the function I in order to implement the if-then-else.
More specifically, show exactly what MIPS instruction(s) and/or
label(s) (if any) would need to be inserted by the I
function at the four places below labeled
# INSERT ...
# Start of code for an if-then-else
# INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
# Start of code to evaluate an expression E
...
# Result left in a temporary memory location 144 bytes past
# the start of the memory array M.
# INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
# Start of code to evaluate the first portion { S }
...
# INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
# Start of code to evaluate the second portion {S}
...
# INSERT EXTRA LABEL(S) AND/OR INSTRUCTION(S) HERE
(If you don't remember the exact form of the MIPS instructions, just
fake it as well as you can.)
- How does the function I find out that the
result of evaluating E is going to be in a memory
location 144 bytes past the start of M?
- Very briefly say how one can manage to pass a function in
Java as a parameter, even though Java does not directly support
functions as objects that can be passed.
- This is a question about 2-dimensional arrays in C/C++/Java:
- Suppose you declare a variable s
in C or C++ using int s[5][4];.
Is this stored as an array of 1-dimensional arrays
(along with the 1-dimensional arrays), or is it stored just as
a single 1-dimensional array? In C/C++ is it possible to
create an array stored the other way?
- Suppose you declare a variable s
in Java using int[][] s = new int[5][4];
Same two questions as in part a.
Points for each problem: 1-25, 2-25, 3-25, 4-10, 5-15.