CSE 401 Wi13 Homework 2 -
Grammars and LR Parsers
Due: Monday, February 4 by 11:59 pm. Please use the dropbox linked from the CSE401 homework web page to submit your homework online. Any common format like pdf or doc/docx is fine, or you can submit a scanned copy of your work as long as it is legible. We suggest you show your work to help us award partial credit if appropriate, and for TA sanity.
You should do this assignment individually.
- (Aho, Sethi, Ullman, 1st edition) Consider the grammar:
S ::= aSbS | bSaS | epsilon
- Show that this grammar is ambiguous by constructing two leftmost derivations for the sentence abab.
- Show that this grammar is ambiguous by constructing two rightmost derivations for the sentence abab.
- Construct the corresponding parse trees for your derivations.
- Given the following grammar:
S ::= ( S ) | [ S ] | x
- Build an LR(0) parser DFA for this grammar. Be sure to show the items for each state of your automaton.
- Build the LR(0) parse tables (action and goto) for this grammar.
- Show the steps that an LR(0) shift-reduce parser goes through when it parses
([(x)]). That is, show the contents of the stack and remaining input
at each step.
- Show the steps that an LR(0) shift-reduce parser goes through when it attempts to parse ([x)]. Does it identify an error? When?
- The following is a grammar that generates epithets (E). An epithet is
a description (D). A description can be either a trait (T) or a simile
Similies are of the form "trait like animal" (A).
E ::= D
D ::= T | S
S ::= T like A
T ::= quick | strong
A ::= bunny | ox
- Construct the LR(0) state diagram and parse table for this grammar.
- Calculate FIRST, FOLLOW, and nullable for each non-terminal.
- Construct the SLR parse table.
- Is this grammar LR(0)? SLR?
- (Appel) Write a grammar for English sentences using the words
time arrow banana flies like a an the fruit
and the semicolon. Be sure to include all the senses (noun, verb, etc.) of
each word. Then show that this grammar is ambiguous by exhibiting more than
one parse tree for the sentence "time
flies like an arrow; fruit flies like a banana".)
Hint: See this discussion thread from a couple years ago for some tips.