Left Factoring-: Factored Grammar
Left Factoring-: Factored Grammar
Left factoring is a process by which the grammar with common prefixes is transformed
to make it useful for Top down parsers.
How?
In left factoring,
We make one production for each common prefixes.
The common prefix may be a terminal or a non-terminal or a combination of
both.
Rest of the derivation is added by new productions.
The grammar obtained after the process of left factoring is called as Left
Factored Grammar.
Example-
Problem-01:
Problem-02:
Solution-
Problem-03:
Solution-
Step-01:
A → aA’
A’ → AB / Bc / Ac
Again, this is a grammar with common prefixes.
Step-02:
A → aA’
A’ → AD / Bc
D→B/c
This is a left factored grammar.
Problem-03:
Solution-
Step-01:
S → bSS’ / a
S’ → SaaS / SaSb / b
Again, this is a grammar with common prefixes.
Step-02:
S → bSS’ / a
S’ → SaA / b
A → aS / Sb
This is a left factored grammar.
Problem-04:
Solution-
Step-01:
S → aS’ / b
S’ → SSbS / SaSb / bb
Again, this is a grammar with common prefixes.
Step-02:
S → aS’ / b
S’ → SA / bb
A → SbS / aSb
This is a left factored grammar.
Problem-05:
Solution-
Step-01:
S → aS’
S’ → b / bc / bcd / ∈
Again, this is a grammar with common prefixes.
Step-02:
S → aS’
S’ → bA / ∈
A → c / cd / ∈
Again, this is a grammar with common prefixes.
Step-03:
S → aS’
S’ → bA / ∈
A → cB / ∈
B→d/∈
This is a left factored grammar.
Problem-06:
Solution-