



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 instructions and questions for the cs164 midterm i exam held in spring 2004. The exam covers topics such as finite automata and regular expressions, ll parsing, lr parsing and ambiguity, and grammars. Students are required to solve multiple-choice and open-ended questions within a given time limit.
Typology: Exams
1 / 7
This page cannot be seen from the preview
Don't miss anything!




Circle the time of your section: Tue 3:00 Tue 4:00 Wed 10:00 Wed 11:00 Wed 1:00 Wed 2:
Problem Max points Points 1 20 2 15 3 50 4 15 TOTAL 100
1 Finite Automata and Regular Expressions (20 points)
(a) Draw a 4-state DFA over the alphabet Σ = {a, b} that accepts the language consisting of strings with an even number of a (this includes zero a), or an odd number of b.
(b) Consider the following DFA over the alphabet Σ = {a, b}.
i. Label the transitions of the following NFA so that it accepts the same language as the DFA. Your NFA should transform to the given DFA by applying the NFA-to-DFA conversion algorithm given in lecture.
ii. Write the regular expression for this language. Among the several possible answers, write the one that would transform to this NFA by applying the regular expression-to-NFA conversion algorithm given in lecture.
3 LR Parsing and Ambiguity (50 points)
Consider the following grammar over the terminals +, − (the negation operator) and id.
S → E E → E + E | − E | id
Below is a partial DFA for this grammar.
(a) Complete the above DFA. You need to do the following:
(b) For each state with a conflict, list the state, the lookahead token, and the type of conflict (i.e. shift-reduce conflict, or reduce-reduce conflict).
(c) Draw all the parse trees for the string id + − id + id.
(d) Is this grammar ambiguous? Why or why not?
4 Grammars (15 points)
Consider two LL(1) grammars, each with a single non-terminal, and without productions. The non-terminal for the first grammar is A and for the second one is B. Consider a third grammar obtained by merging the productions of the two grammars and adding the production S → A | B. Answer the following questions, and explain your answers.
(a) Is it possible to derive the empty string from S in the third grammar?
(b) Write the necessary and sufficient condition(s) that the two original grammars must satisfy, in order to ensure that the third grammar is unambiguous. Do not give examples of grammars for A and B, but give instead a general condition that applies to all grammars of the form mentioned above.
(c) Write the necessary and sufficient condition(s) that the two original grammars must satisfy, in order to ensure that the third grammar is LL(1). Do not give examples of grammars for A and B, but give instead a general condition that applies to all grammars of the form mentioned above.