I am working on a problem and the grammar is below.
$A\rightarrow AA$*
$\rightarrow AA+$
$\rightarrow a$
The formula for elimination is
$A\rightarrow Aα|β$
$A\rightarrow βA'$
$A'\rightarrow αA'$
I just don't know how to use that formula if I have two AA instead of just one A
$A->aA'$
$A'\rightarrow A+A'$
$\rightarrow A*A'$
$\rightarrow a$
However, I still have one $A$ in there. How do I get rid of that?
Thank you
How do I eliminate left recursion in discrete math
1k Views Asked by Bumbble Comm https://math.techqa.club/user/bumbble-comm/detail AtThere are 3 best solutions below
On
Is this the grammar you've come up with?
A->aA'
A'->A+A'
A'->A*A'
A'->a
If so, this grammar does not have any left-recursion, so it looks like you're already finished.
(However, shouldn't the last rule produce the empty string instead of "a"?)
On
As you mention, the formula for removing left-recursion (and replacing it with right-recursion) involves replacing all instances of this rule: \begin{gather} A \rightarrow A\alpha\ |\ \beta\\ \end{gather} with this rule: \begin{gather} A \rightarrow \beta A'\\ A' \rightarrow \alpha A'\ |\ \epsilon \end{gather}
Your grammar is given by the following rules: \begin{gather} A \rightarrow AA*\ |\ AA+\ |\ a\\ \end{gather}
Here, we must eliminate left-recursion in two places, using the above rule for each case:
- $A \rightarrow AA*\ |\ a$ (here, $\alpha = A*$ and $\beta=a$) \begin{gather} A \rightarrow a A'\\ A' \rightarrow A*A'\ |\ \epsilon \end{gather}
- $A \rightarrow AA+\ |\ a$ (here, $\alpha = A+$ and $\beta=a$). \begin{gather} A \rightarrow a A'\\ A' \rightarrow A+A'\ |\ \epsilon \end{gather}
Combining these, we obtain: \begin{gather} A \rightarrow a A'\\ A' \rightarrow A+A'\ |\ A*A'\ |\ \epsilon \end{gather}
I hope it can help you.
$ A\to AA*|AA+|a $
we can write it as:
$A\to AB|AC|a \\ B \to A* \\ C \to A+ $
$A\to AB|a :\\ \,\,\,A\to aA'\\ \,\,\,\,A' \to A*A'|\epsilon$
$A \to AC|a :\\ \,\,\,A \to aA'\\ \,\,\,\,A' \to A+A'|\epsilon$
combine those two:
$A \to aA'\\ A' \to A+A'|A*A'|\epsilon$