CSCI 301 L29 Worksheet

Lecture 29 - Exercises

Part A - 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\).

Part B - Eliminating Left Recursion

Rewrite the following grammar to eliminate left recursion:

\[S \rightarrow Sa \mid Sb \mid AB \mid C \mid \epsilon\]

Part C - Left Factoring (Eliminating Common Prefixes)

Consider the following grammar, in which \(+\), \(*\), and \(a\) are terminals: \[ S \rightarrow S S + \mid SS * \mid a \]

Left factor the grammar, rewriting an equivalent grammar such that no rules have a common prefix.

Part D - RPN and Grammar

  1. Rewrite the following infix expression in RPN: \((4 + 4) / (6 - 2)\)

  2. Rewrite the following RPN expressions in infix notation (that is, write out the expression, but don’t evaluate it): 8 4 - 2 *

  3. Rewrite the following RPN expressions in infix notation (that is, write out the expression, but don’t evaluate it): 6 8 3 * + 6 /

  4. Rewrite the following RPN grammar to eliminate left recursion and common prefixes: \[ \begin{align*} S &\rightarrow \_ S \mid SP \mid NS \mid E\\ N &\rightarrow ND \mid D\\ D &\rightarrow 0 \mid 1 \mid \cdots \mid 9\\ P &\rightarrow + \mid - \mid * \mid / \end{align*} \]