CS143 Midterm Spring 2016
CS143 Midterm Spring 2016
Spring 2016
• Please read all instructions (including these) carefully.
• There are 5 questions on the exam, all with multiple parts. You have 80 minutes to work
on the exam.
• The exam is open note. You may use laptops, phones and e-readers to read electronic
notes, but not for computation or access to the internet for any reason.
• Please write your answers in the space provided on the exam, and clearly mark your
solutions. Do not write on the back of exam pages or other pages.
• Solutions will be graded on correctness and clarity. Each problem has a relatively simple
and straightforward solution. You may get as few as 0 points for a question if your solution
is far more complicated than necessary. Partial solutions will be graded for partial credit.
NAME:
In accordance with both the letter and spirit of the Honor Code, I have neither given nor
received assistance on this examination.
SIGNATURE:
1
1. Regular Languages and DFAs (10 points)
(a) Write a regular expression for the set of strings over {0, 1} that do not contain a
sequence of 3 (or more) zeros consecutively, anywhere in the string. Examples:
• In the set of strings: 0, 100111111101, 0100100
• Not in the set of strings: 000, 1101000001
(0? 0? 1)∗ 0? 0?
(b) Give a DFA that recognizes the same set of strings. Your DFA need not be complete
(it need not have transitions for all inputs in all states).
q0 0 q1 0 q2
start
1
1
2
2. Top-Down Parsing (20 points)
S is the start symbol. The terminals are IF, THEN, ELSE, AND, TRUE, FALSE, ( and
). Now, answer the following:
(a) Compute the first sets for all non-terminals and follow sets for all symbols of the
grammar. Write your answers below.
FOLLOW(IF) = ( FOLLOW(THEN) = (
FOLLOW(ELSE) = ( FOLLOW(AND) = (
3
(b) Fill in the LL(1) parsing table for the grammar. Put only the production number in
the table, not the right-hand side.
( ) IF THEN ELSE TRUE FALSE AND $
S 1 2 2
X 4 3
E 5 6
Y 7 8 7
4
3. Grammars (10 points)
(a) Give a context-free grammar for the set of all strings over the alphabet {a, b, c}
that are palindromes. (A palindrome is a string that reads the same forwards as
backwards: the empty string, aa and abccba are all examples of palindromes.)
S → aSa
| bSb
| cSc
| a
| b
| c
|
S → S and S
| S or S
| T
| a
T → a
In this grammar S and T are the non-terminals and S is the start symbol; and, or,
and a are terminal symbols. How many parse trees are there for the string: a and
a or a? Justify your answer.
There are 16 possible parse trees given the above grammar.
First, either and can have higher precedence than or or or can have higher precedence
than and. This gives two possible parse trees to produce the and and or.
What remains is the three S non-terminals. Each S non-terminal produce the termi-
nal a via the production S → a or the production S → T → a. Each S non-terminal
can choose a production independently from the others so there are 23 = 8 parses.
In total, we have 2 ∗ 23 = 16 possible parse trees.
5
4. Bottom-Up Parsing (20 points)
S → M id | T M E | −E | T − id
T → E×T |E
E → ( id )
M → +|−
Note that the terminals are {id, +, −, ×, (, )}. For your convenience, here are the first and
follow sets of the non-terminals.
FIRST(S) = {+, −, (} FOLLOW(S) = {$}
FIRST(T ) = {(} FOLLOW(T ) = {+, −}
FIRST(E) = {(} FOLLOW(E) = {$, ×, +, −}
FIRST(M ) = {+, −} FOLLOW(M ) = {id, (}
S 0 → S·
S
q0 0
S → ·S
S → ·M id q2
S → ·T M E S →−·E
S →·−E −
start M → −·
S → ·T − id E → ·(id)
M → ·+
M → ·−
...
State q2 has a potential shift-reduce conflict, between shifting ‘(’ due to item E → ·(id)
and reducing M → −·. This is an actual conflict iff ‘(’ is in the FOLLOW set of M .
We can see above that this is indeed the case, which means we have found a shift-reduce
conflict according to SLR(1) rules.
6
(More space for question 4.)
7
5. Semantic Actions (20 points)
In Python, reading from a variable before it has been assigned is an error. In this problem
you will construct an undefined-variable checker for a small Python-like language using
semantic actions. For full credit, your semantic actions should do all of the following (you
may find it helpful to look at the grammar on the next page):
Fill in the semantic actions in the alloted space. You should operate on the set attributes
using standard set operations (e.g., union, intersection, membership, etc.). Do not be
concerned about the order of attribute evaluation—assume the parser can figure out a
correct order in which to evaluate your assignments to attributes.
Use bison syntax for the attributes: $i.[attr] refers to an attribute of the i-th symbol
of the production, $$.[attr] refers to an attribute of the production’s result. Otherwise
use any reasonable and clear programming notation.
8
stmt : var ’= ’ expr
{
if ($3. refd 6⊆ $$. in_defs )
print (" A variable may be undefined ");
$$. out_defs = $$. in_defs ∪ {$1. name };
}
| stmt ’; ’ stmt
{
$1. in_defs = $$. in_defs ;
$3. in_defs = $1. out_defs ;
$$. out_defs = $3. out_defs ;
}
| var
{
$$. refd = {$1. name };
}
| int_const
{
$$. refd = ∅;
}
9
This scratch page intentionally left blank!
10