CSCI 301 L25 Worksheet

Lecture 25 - Exercises

Part A - Parsing and Left Recursion

Consider the following two grammars:

\[G_1: S \rightarrow aS \mid bS \mid \epsilon\]

\[G_2: S \rightarrow Sa \mid Sb \mid \epsilon\]

  1. Write a regular expression for \(L(G_1)\).
  2. Write a regular expression for \(L(G_2)\).
  3. Find a left-most derivation using \(G_1\) for the string \(aabb\). If you are reading the string left-to-right, what is the largest number of symbols you ever need to look ahead in order to know which rule to apply?
  4. Repeat the same exercise for grammar \(G_2\).

Part B - Eliminating Left Recursion

Rewrite the following rule 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 /