




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Circle the time of your section: Fri 10:00 Fri 11:00 Fri 2:
Problem Max points Points 1 6 2 30 3 20 4 44 TOTAL 100
1 Miscellaneous (6 points)
(a) Fill the missing right-hand side for the production of A in the grammar below such that the rules form a LL(1) grammar. S → A a A | A b A
A → Solution: A →
Consider the following grammar:
S → a S | b S | a b a A
A → ε | a A | b A
(b) Is the above grammar LL(1)? Why or why not? Solution: It is not LL(1) because on non-terminal S and lookahead a there are two produc- tions possible (a S and a b a A).
(c) Show an NFA that accepts the language of this grammar. Solution: The solution is shown below. You can solve this problem without figuring out the language before: each non-terminal corresponds to an NFA state, transitions mark the accepting states, each production corresponds to a transition. This works for all grammars whose productions have at most one non-terminal on the right-hand-side, occurring at the end.
a S
a, b
A
b a
a, b
In the following questions the alphabet is { 0 , 1 }. Write TRUE if the language is regular and FALSE otherwise. Explain your answers in one or two sentences.
(c) The language of strings with no occurrences of the substring “010”. Solution: TRUE. The complement of a regular language is regular. Take the DFA that recognizes the 010 substring and swap rejecting and accepting states.
(d) The language of strings with more occurrences of 0 than 1.
Solution: FALSE. This would require counting up for a 0 and down for a 1. Unbounded counting cannot be done with finite automata.
(e) The language of strings whose length is a multiple of 3. Solution: TRUE. Counting modulo N requires only N states.
(f) [Harder] The language of strings whose number of occurrences of 0 minus number of occur- rences of 1 is a multiple of 3. Solution: TRUE. This is also a counting modulo 3 problem, with counting up for a 0 and down for a 1.
(g) [Harder] The language of strings of terminals and non-terminals that can appear on the parsing stack during an LR(1) parse (stack is read from the bottom of the stack). Here the alphabet is the set of terminals and non-terminals of the grammar being parsed. Solution: TRUE. After all we use a DFA to process the contents of the stack and decide whether to shift or to reduce. Take than DFA and make every state an accepting state.
3 LL Parsing (20 points)
Consider the following grammar with terminals (, [, ), and ].
S → T S | [S]S | )S | T → (X) X → T X | [X]X |
(a) [Long] Fill in the table with the First and Follow sets for the non-terminals.
First Follow S ( [ ) $ ]
T ( ( [ ) ] $
X ( [ ) ]
(b) [Depends on (a)] Fill in the LL(1) parsing table.
( [ ) ] $
S T S [ S ] S ) S
T ( X )
X T X [ X ] X
(c) [Depends on (b)] Is this grammar LL(1)? Solution: Yes, because every cell in the table above has at most one entry.
E int , $/if/+
reduce by E E if E on $/if/+ reduce by^ E^ ^ E^ +^ E^ on^ $/if/+
int int
reduce by E int on $/if/+
ACCEPT on $
0 int
1
2
3
4
(^5 )
if (^) E E E , $/if/+
E (^) E
E
if if + +
(d) [Does not depend on (b)] Assume that we disambiguate the grammar by saying that + is left-associative and binds more tightly than if, and if is right associative. Show the correct parse tree for the string int + int if int + int if int Solution:
S
E
E
E (^) E
int int
if (^) E
E (^) if E
E + E
int int
int
(e) [Depends on (b)] For each of the conflicts that you have identified above, show how it must be resolved to achieve the disambiguation described above. For a shift/reduce conflict say whether we should shift or reduce. For a reduce/reduce conflict say which reduction we should choose. Solution: State 5: shift on “if”, shift on “+”. State 6: reduce on “if”, reduce on “+”.