True/False (2 points each, 20 points total)
Short answer questions (5 points each,15 points total)
1. What is the postfix expression equivalent to the following prefix expression?
+ + * A B + C D E
2. What is the infix expression equivalent to the following postfix expression? (Warning! You may need to add parentheses.)
C D G / + M Q / * Z +
3. What’s the difference between static and dynamic type checking? Name one advantage and one disadvantage of dynamic type checking.
(15 points)
What is the output of the following Modula program? The program compiles correctly as shown. (You don’t have to worry about leading or trailing blanks.)
MODULE puzzle;
FROM InOut IMPORT WriteLn, WriteCard;
VAR a, b : CARDINAL;
PROCEDURE P1 ( VAR a: CARDINAL; b: CARDINAL);
BEGIN
a := a + 1;
WriteCard(a,5);
WriteLn;
WriteCard(b,5);
WriteLn
END P1;
PROCEDURE P2 (x: CARDINAL; VAR y: CARDINAL);
BEGIN
WriteCard(a,5);
WriteLn;
WriteCard(b,5);
WriteLn;
P1(y, x);
END P2;
BEGIN
a := 2;
b := 3;
P1(a, b+2);
WriteCard(a,5);
WriteLn;
WriteCard(b,5);
WriteLn;
P2(a, b);
END puzzle.
(15 points)
Most programming languages allow some sort of array. In most languages, integers are used as subscripts. In C, single characters can be used as subscripts. In Pascal and Modula, enumeration types may also be used as subscripts. In still other languages, character strings (not just single characters) can be used as subscripts. How might a language facility that allows arrays to be indexed by character strings be efficiently implemented?
(15 points)
Given the following BNF grammar, draw a parse tree for the string baababb
S ::= a B
S ::= b A B
A ::= a
A ::= A A b
B ::= b
B ::= b S
B ::= a B B
(20 points)
The following grammar is ambiguous. Find a string that demonstrates this, and draw two of that string’s parse trees. (Hint: at least one such string is four characters or less in length.)
S ::= a B
S ::= a a B
B ::= b
B ::= a b