Removing Unit productions
📌 What are Unit Productions?
A unit production is a production of the form:
where A and B are variables (nonterminals).
Example:
A → BThese are unit productions.
They are considered redundant because instead of going through A → B → ..., we can directly substitute A with the right-hand side productions of B.
📌 How to Remove Unit Productions
We systematically replace them with equivalent non-unit productions:
Step 1: Identify all unit productions
-
Find all rules of the form A → B.
Step 2: Compute unit pairs
A unit pair (A, B)
means that using only unit productions.
-
For each pair
(A, B)
, we allowA
to produce everything B produces (except ε, unless S is nullable).
Step 3: Replace unit productions
For every unit pair (A, B)
and every non-unit rule B → α
, add a rule A → α
.
Step 4: Delete the original unit productions
Keep only the newly added useful rules.
📌 Example (your grammar after ε-removal)
We had:
Step 1: Identify unit productions
-
A → B
-
A → C
-
C → D
Step 2: Compute unit pairs
-
(A, B)
-
(A, C)
-
(A, D)
(because A → C and C → D) -
(C, D)
Step 3: Replace them
-
From
B → b
, and(A, B)
⇒ addA → b
. -
From
C → D
andD → d
, and(A, D)
⇒ addA → d
. -
From
C → D
and(A, C)
⇒ add all of D’s non-unit rules (D → d
) ⇒ addA → d
(already added). -
From
(C, D)
⇒ addC → d
.
Step 4: Remove unit productions
Final grammar without unit productions:
After fully cleaning (removing last C → D
):
✅ Now we have a grammar with:
-
No ε-productions
-
No unit productions
Example
Given grammar
1) Identify unit productions
Unit productions are of the form X → Y
where both X and Y are variables:
-
S → B
-
B → A
-
A → B
2) Compute unit-reachability (unit-closure)
Find which variables are reachable from which using only unit productions.
-
From
S
:S → B
andB → A
, andA → B
(loop), soS
reaches{B, A}
(and itself implicitly). -
From
A
:A → B
andB → A
⇒A
reaches{B, A}
. -
From
B
:B → A
andA → B
⇒B
reaches{A, B}
.
So the unit pairs (nontrivial) are:
-
S
→{A, B}
-
A
→{A, B}
-
B
→{A, B}
(We usually include the reflexive pair X→X
, but what matters is that each variable can "inherit" non-unit productions from A and B.)
3) Collect all non-unit productions (RHS not a single variable)
These are the rules we will copy along unit edges:
-
From
A
:A → a
,A → b c
-
From
B
:B → b b
-
From
S
:S → A a
is already non-unit (keep as is)
4) For each unit pair (X → Y) add X → α
for every non-unit Y → α
-
Since
S
reachesA
andB
, add toS
the non-unit productions ofA
andB
:-
From
A
: addS → a
,S → b c
-
From
B
: addS → b b
-
-
Since
A
reachesA
andB
, add toA
:-
From
B
: addA → b b
(it already hasa
andbc
)
-
-
Since
B
reachesA
andB
, add toB
:-
From
A
: addB → a
,B → b c
(andB
already hasb b
)
-
5) Remove all original unit productions
Delete S → B
, B → A
, A → B
.
6) Final grammar (no unit productions)
All productions now have either terminal(s) or a mixture (like A a
) on the right-hand side — no rule of the form X → Y
remains.
Comments
Post a Comment