CS471, Programming Language Structure I
Fall, 1998
Examination 1
Closed
Book. Answer all
the questions. The total points for each question or part of a question follows
it in parentheses, thus: (12)
Java has both a compiler (which
produces a "byte-code" version of the program) and an interpreter
that interprets the byte codes. Draw a diagram that shows how a Java program
produces output results. You should include the following features: compiler,
interpreter, run-time library, source code, byte code. (16)
Below is an
x := 1;
loop
y := x + 1;
if y > 10 then exit;
x := y + 2;
end loop;
Look at the statement sequence below,
written in a strongly-typed language. Explain how each statement is handled at
compile and/or run time if:
the language is statically typed (i.e. all variables must have
a type declaration). (12)
the language is dynamically typed (i.e. the declarations can be omitted) (12)
x, y : integer;
x = 1;
y = x + 1;
x = "abcd";
y = x + 1;
In the program below, written in C
syntax, what is the value of g in the body of f1 with:
static scoping (8), and
dynamic scoping? (8)
By changing the order of function
calls, show how g in the body of f1 could have the value 3 with dynamic
scoping. (4)
int g = 1;
void f1();
void f2() {
f1();
}
void f3() {
int g = 2;
f2();
}
void f1() {
/* what is the value of g here? */
}
int main() {
int
g = 3;
f3();
}
Below is a grammar for a portion of
Pascal. Show, by drawing a parse tree, that the string:
begin S1; begin S2; S3 end; begin S1; S3 end end
can be generated by the grammar. (16) Then alter the grammar,
by making a simple change, so that the semi-colon becomes a statement
terminator (as in C), not a separator, as it is in Pascal. (6)
<compound-stmt>
::= begin <stmt-seq> end
<stmt-seq> ::= <stmt> | <stmt> ; <stmt-seq>
<stmt> ::= <simple-stmt> | <compound-stmt>
<simple-stmt> ::= S1 | S2 | S3