Download CS200 Fall 2008 Homework 1: Grammar and Parsing - Prof. Adele E. Howe and more Assignments Computer Science in PDF only on Docsity! CS200 Fall 2008 HW 1 due 9/9/08 at 9:30AM Name: 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.] [20 points] <expr> -> switch (<expr>) { <case> } <case> -> case <value> : <casedo> <case> | case <value> : <casedo> | default : <casedo> | case <value> : <case> <casedo> -> <expr> | <expr> break; <value> -> <byte> | <short> | <char> | <int> | <enum> |<expr> 2. Construct a grammar for any length bit strings of that start with 00 and end with one or more 1s. [20 points] <S> -> 00<A> <A> -> 0<A> | 1<A> | 1 3. Given the following grammar: <S> = ab<S> | bc<S>| bb<S> | a | cb 1. Is โbcbbaโ a legal sentence? [5 points] yes 2. List all legal sentences of 5 characters or less. [15 points] a, cb, aba, bca, bba, abcb, bccb, bbcb, ababa, abbca, abbba, bcbca, bcbba, bcaba, bbaba, bbbca, bbbba 4. Is โ+*5-3/2++61-87โ a legal prefix expression (assume all operators are binary)? If yes, to what does it evaluate? If no, fix it, with as little change as possible, so that it is. [10 points] Not legal. +*5-3/2++61-879 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? [10 points] Yes. 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. [20 points] 0. main. 1. Parse(โ* a + b cโ); main. 2. main. 3. start(); main. 4. expression(โ โ);start();main. 5. ID(โ โ);expression(โ โ);start();main. 6. expression(โ โ);start();main. 7. operator(โ โ);expression(โ โ);start();main. 8. expression(โ โ);start();main. 9. expression(โ โ); expression(โ โ);start();main. 10. ID(โ โ);expression(โ โ); expression(โ โ);start();main.