HW 2
HW 2
Homework #2
Due Time: March 4th, 2019
1. (16 points) Answer the following questions about languages and grammars.
2. (12 points) Answer the following questions about context free and regular grammars.
3. (10 points) Given the following grammar, construct the First sets for each RHS (right hand
side) and Follow sets for each non-terminal symbol.
A → BAc | F E
B → bEF | g
E →e|ε
F → f | EH
H→h
1
4. (16 points) For each of the below grammars, answer the following questions:
1) Is the grammar LL(1)? If so, write the LL(1) parse table. If not, point out the conflict
using First and Follow sets.
2) Is the grammar LL(k)? If so, show how extra lookahead resolves the conflict. If not, point
out the unresolved conflict using First and Follow sets.
? Note: LL(k) is LL with a finite amount of lookahead.
3) Is the grammar ambiguous? If yes, find the input that produces two or more left-derivations.
? Note LL(1) ⊂ LL(k) ⊂ L(Unambiguous). So, (2) needs answering only if (1) is false. (3)
needs answering only if both (1) and (2) are false.
a) S → [S | A
A → [A] | ε
b) S → ABc
A→a|ε
B →b|ε
c) S → ABBA
A→a|ε
B →b|ε
d) S → aAbc|bAc
A→b|ε
5. (16 points) Given the following grammar, answer the below questions:
E → E + E | id
6. (30 points) For each of the below grammars, answer the following questions:
(1) Is the grammar SLR(1)? If so, draw a DFA and the corresponding parse table. If not,
point out the conflict.
(2) Is the grammar LALR(1)? If so, show how the lookahead component resolves the con-
flict. If not, show why it is not resolved.
(3) Is the grammar LR(1)? If so, show how state splitting resolves the conflict. If not, show
why it is not resolved.
? Note SLR(1) ⊂ LALR(1) ⊂ LR(1). So, (2) needs answering only if (1) is false. (3) needs
answering only if both (1) and (2) are false.
2
a) (10 points) Σ={v, =, ;, +, (, )}.
S → v = A;
A→PE
P→Pv = |ε
E→E + T|T
T→v|(A)