CS200 Fall 2008 HW 1 due 9/9/08 at 9:30AM
Hand-in before start of lecture
1. Construct a grammar for the Java switch statement. Use <expr> (which denotes
arbitrary Java expressions) as the starting non-terminal. You may also assume that
some non-terminals have already been defined (i.e., <var> for variables, <expr>,
and non-terminals for legal values of all basic types). [Note: your grammar is not
responsible for checking type consistency.]
2. Construct a grammar for any length bit strings (alphabet is {0,1}) that start with
00 and end with one or more 1s.
3. Given the following grammar:
<S> = ab<S> | bc<S>| bb<S> | a | cb
1. Is โbcbbaโ a legal sentence?
2. List all legal sentences of 5 characters or less.
4. Is โ+ * 5 โ 3 / 2 + + 6 1 โ 8 7โ a legal prefix expression? If yes, to what does it
evaluate? If no, fix it, with as little change as possible, so that it is.
5. For the recursive descent parser provided in class (and on the website),
a. will the code conclude that โ* + a b โ c / a bโ is a legal expression?
b. show how the execution stack will change as the code runs for the
expression โ* a + b cโ. Every time a method is invoked, show a stack
snapshot that includes the name of that method and its argument(s) at the
top. [Note: The execution stack keeps track during program execution of
what methods have been invoked from where in the code so that the
program can return to the calling method when the called method returns.]