Professional Documents
Culture Documents
Quiz 1 Answers
================================
PROBLEM 1
---------
Is it possible for the assignment statement
x.x = x;
Answer:
PROBLEM 2
---------
For each of the following Carlos fragments, tell whether it is a lexical error,
syntax error, static semantic error, dynamic semantic error, or no error.
a) int x; int x;
b) int f(int x) {int x = x;}
c) char* `ohana = "family";
d) string pet = "dog"; pet = "rat";
e) string pet = "cat"; pet[0] = 'r';
f) int f(int x) {} int g() {f(1,2,3,4);}
Answer:
a) static semantic.
b) static semantic (but note this is NOT AN ERROR in C!)
c) lexical ("`" is not an allowable character). There is also
no char*, so syntax is also a possible answer.
d) no error
e) static semantic - strings are immutable in Carlos
f) static semantic (too many arguments to f)
PROBLEM 3
---------
Sketch the AST for the Java fragment:
void q(String... v) {
for (int y : f(x)) {
x = p.data[0] * (2< 5|-x *1);
}
}
Answer:
method
/ / | \
void q param block
/ \ |
... v for
| // \ \
String __/ | \ \____
/ / \ \
int y call =
/ \ / \
f x x *
/ \
[] |
/ | / \
. 0 < *
/ \ / \ / \
p data 2 5 - 1
|
x
PROBLEM 4
---------
Consider a language for describing turtle graphics. An example program in
this language is:
deg color 1 0 0
left 90 forward 4 color 0 0 1 [ left 90
forward 1.5 ] right 90 forward 1.5
This program draws the letter T with a red vertical line of size 4 units and
topped with
a 3 unit blue line. A program is a sequence of instructions. The instructions are:
Answer:
Note that NUM is a primitive token, and note that the value constraint on
color arguments is not specified in the syntax; we're leaving that to the
static semantic description. The grammar is LL(1) and non-ambiguous.
PROBLEM 5
---------
Write a regular expression for the language of "positive hexadecimal numerals
divisible by 256". Is it possible to implement a parser for this language in
JavaCC using only numeric lookahead values? If so, what is the find the smallest
lookahead value you would need?
Answer:
0*[1-9A-Fa-f][0-9A-Fa-f]*00
That's not LL(1), it's LL(3). You have to lookahead 3 symbols to know
whether to take the final two zeros of the right alternative (the third
symbol is <EOF>). I think 3 is the minimum we can get.
PROBLEM 6
---------
Is this grammar an LL grammar?
A -> B C
B -> a | b?c?
C -> c | BA
If you find that this grammar is not LL, make one that is (that defines the same
language of course).
Answer:
It's not LL(k) for any k. You can have a derivation like this:
so there's left recursion. You can expand like that forever until you
do anything useful.
A -> (a|b|c)*c
PROBLEM 7
---------
EBNF generally uses
Answer: