CSCI 301 L24 Worksheet

Lecture 24 - Exercises

Grammar 1: \(\Sigma = \{a, b\}, V = \{S\}, S = S\) \[ \begin{align*} S &\rightarrow \epsilon\\ S &\rightarrow aSa\\ S &\rightarrow bSb \end{align*} \]

Grammar 2: \(\Sigma = \{a, b\}, V = \{S\}, S = S\) \[ \begin{align*} S &\rightarrow \epsilon\\ S &\rightarrow aSbS\\ S &\rightarrow bSaS\\ \end{align*} \]

Part A - Leftmost, Rightmost Derivations; Ambiguous Grammars

  1. Give a leftmost derivation of \(abab\) in Grammar 2 above.
  2. Give a rightmost derivation of \(abab\) in Grammar 2 above.
  3. Are their parse trees the same?
  4. Show that Grammar 2 above is ambiguous by finding more than one left-most derivation for the string \(abaabb\).

Part B - Parsing and Left Recursion

  1. Consider the grammar \(S \rightarrow aS \mid bS \mid \epsilon\). Find a derivation for the string \(aabb\). At each step, how many characters of the input string did you need to look at to know which rule to apply?
  2. Repeat the same exercise for the grammar \(S \rightarrow Sa \mid Sb \mid \epsilon\).