



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The solutions to the cs164 midterm i exam held in spring 2003. It includes questions on context-free grammars, ll parsing, lr(1) parsing, and regular expressions and finite automata. Students are expected to read the instructions carefully and write their answers in the provided spaces.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




NAME and LOGIN:
SID or SS#:
Circle the time of your section: 11:00 12:00 1:00 2:00 3:00 4:
Problem Max points Points 1 15 2 15 3 35 4 35 TOTAL 100
1 Context-Free Grammars (15 points)
Consider the following grammar. S → S S S → 0 S →
a) Write a regular expression for the language that is accepted by this grammar. Answer: 0 ∗
b) Give at least one reason why this grammar is not LL(1). Answer:
c) Which single production can you remove from this grammar to make it LL(1)? Does this transformation change the language recognized by the grammar? Answer: S → S S The remaining grammar is trivially LL(1), but it describes a different language.
d) Is it possible to make the original grammar LL(1) by adding one production? If your answer is yes, show one such production. If your answer is no, explain why not. Answer: No, adding productions does not eliminate ambiguity nor the left-recursion.
e) Write an LL(1) grammar for the language recognized by the original grammar.
S → | 0 S
3 LR(1) Parsing (35 points)
Consider the following grammar:
S → B B → B E + | − E E → e | E e |
Below is a partial DFA for the grammar above. Notice we did not have to add a new start state as is usually done for LR(1) parsers.
a) Complete state 0 by performing closure on the item listed. Don’t forget to include lookahead symbols for the new items.
b) Fill in all elements of states 3 and 8, and the lookahead items in states 1, 5 and 7.
c) Fill in the missing transition labels (4 of them).
d) Fill in the following table the reduce productions and lookahead tokens for the given states. Write “not a reduce state” if that state does not have a reduction action. State Production Lookahead Symbols
0 not a reduce state
2 E → $/e/+
4 B → − E $/e/+
e) For each state with a shift-reduce conflict, list the state, the lookahead token and the pro- duction that are in conflict. Write “none” if there are no shift-reduce conflicts.
Answer:
2 E → e 3 E → e 4 B → − E e
f) For each state with a reduce-reduce conflict, list the state, and the conflicting productions. Write “none” if there are no reduce-reduce conflicts. Answer: none
g) For each of the following configurations of the LR(1) parser, say what action is taken by the parser (one of “shift”, “reduce with production ...”, “error” or “accept”) and write the next configuration (except in the case of an accept or error). You can assume that in the case of a shift-reduce conflict the parser chooses to shift. Configuration Action Next Configuration
−. e + $ shift −e. $
BEe. e + $ reduce E → E e BE. e + $
Be. $ error
B. $ accept
e) Consider the following NFA over the alphabet {a, b}. Convert this NFA to a DFA. For each DFA state write the set of the NFA states that it corresponds to. Remember to mark the start state.
Answer:
f) Draw a deterministic finite automaton for the language over the alphabet { 0 , 1 } that consists of the binary numbers that are divisible by 3 (allowing leading 0’s). The empty string is also part of the language. This question is harder that others in the exam. Do not spend a lot of time on it unless you have checked your answers on the other questions. Hints: There is a solution with 3 states. Make sure you verify your answer on the binary representation of a few numbers like 3,6,9,15. Answer:
State 0 represents multiples of 3, state B multiples of 3 plus 1, and state C multiples of C plus 2. Here is how to to find out where the arrow 0 from state B goes: the numbers at B can be written as 3k + 1. Adding a 0 at the end mutiplies the number by 2, resulting in 6k + 2, which is a multiple of 3 plus 2, thus it goes to state C.