CS 434 Exam 1 Spring 2004 Solution 1. Show this grammar is ambiguous by drawing 3 distinct parse trees for the string xyz. S A B C D E F → → → → → → → AB | CD | E xAy | zB | xC | yDz | xEz | F yF | 2. List every string with length exactly 6 that is generated by the grammar of problem 1. xxxxxx xxxxyz yyyyyy xyyyyz zzzzzz xyzzzz xxxyyy yyyzzz xxxzzz xxyyzz 3. Construct a context-free grammar that generates arithmetic expressions using operators –, +, /, * with the precedence levels and associativities as specified below. Assume that arbitrary operands are denoted by id, so this string could be generated: id+(id–id*id)/id Operator Precedence Associativity – 1st (highest) left + 2nd right / 3rd right th * 4 (lowest) left S T U V W → → → → → S*T U/T V+U V–W (S) | | | | | T U V W id 4. List every string with length at most 5 that is accepted by this finite-state machine: 01 10 0001 1110 0010 1101 0100 1011 1000 0111 5. Draw a deterministic finite-state machine that accepts strings over alphabet {@, #, $} such that the difference (number of @’s minus number of #’s) is not a multiple of 3. Example: @$@#$@#$@ should be accepted because (4 – 2) is not a multiple of 3. Hint: this DFSM can be drawn with as few as 3 states. 6. List every string with length at most 4 that is generated by this regular expression: (ba*c | ca*b)+ bc cb bac cab baac caab bcbc cbcb bccb cbbc 7. Construct a regular expression that generates the strings over alphabet {a, b} that do not contain substring aa and also do not contain substring bb. (ab)* | (ab)*a | (ba)* | (ba)*b 8. Suppose you have downloaded from the Internet a de-compiler that converts MIPS machine code into Java, and this de-compiler is written in MIPS machine code. Suppose you also have access to a MIPS machine. Draw a tombstone diagram that shows how to construct a de-compiler from MIPS to Java that is written in Java. MIPS Java MIPS MIPS MIPS Java Java Java MIPS MIPS 9. Identify a syntax error, a scope error, and a type error in this C++ code. Briefly but precisely describe each error. class B { int w; public: void g ( ) B (int a) { w=a; } B * j ( ) { return w; } int f ( ) { return a; } }; // Syntax error: missing ‘;’ or ‘{}’ // Type error: ‘w’ is int, not B* // Scope error: ‘a’ is undefined 10. First explain a restriction in the C++ language that seems to indicate that a single-pass compiler will be used. Next explain a feature of the C++ language that appears to indicate that a multi-pass compiler must be used. Briefly justify your answers. Single-pass: Most names must be declared before they can be used. – C++ ordinary functions must be declared before they can be called. – C++ classes must be declared before they can be used as type names. Multi-pass: Some names can be used before they are declared (forward references). – C++ instance variables can be accessed before they are declared. – C++ member functions can be invoked before they are declared.