CFG - Left factoring in recursive nested productions I'm attempting to convert a CFG into an LL(1) grammar for predictive parsing in a compiler. I've been able to left factor and eliminate left recursion and ambiguity for every case in the grammar, with the follow exception. I've provided the simplest case to illustrate the problem below.

S -&gt; a b S | T
T  -&gt; a c T | d T | ϵ


This grammar rightly generate a FIRST(S) - FIRST(T) clash, as both S and T can generate a as a first terminal.

One (clearly wrong) attempt to left-factor the a terminal was to substitute T into the S production.

S -&gt; a b S | a c S | d S | ϵ


This would allow for left factoring like so:

S -&gt; a U | d S | ϵ
U -&gt; b S | c S


However, this grammar is no longer equivalent, as it would accept the string acab when the original grammar would not.

I'm hoping that there is some way to manipulate this grammar that I'm not aware of to eliminate the FIRST(S) and FIRST(T) clash. Is there a method to left-factor a terminal in recursive nested productions as is described above?
You can solve this with regular expressions:

S -&gt; a b S | T


is equivalent to

S -&gt; (a b)* T


and

T -&gt; a c T | d T | ϵ


is equivalent to

T -&gt; (a c | d)*


Subtitute T into the rule for S:

S -&gt; (a b)* (a c | d)*


Now, you can use a standard method to derive a right regular grammar.