Left Factoring | Left Factoring Examples
Definition:
Why is Left Factoring Needed?
Top-down parsers (e.g., recursive descent and LL parsers) struggle with ambiguous or left-recursive grammars. Left factoring helps by making the decision-making process for selecting a production deterministic. It allows the parser to determine the correct production by reading a fixed number of symbols ahead.
Left Factoring Process
If a non-terminal has multiple productions with a common prefix, such as:
Where:
- is the common prefix.
- are different suffixes.
- is an alternative production.
We refactor the grammar as:
Here,
is a new non-terminal that represents the choices after the common prefix .
Example 1: Left Factoring a Grammar
Consider the following grammar:
Here, both productions of start with if E then S, making it difficult for a top-down parser to decide which production to choose.
Using left factoring, we rewrite it as:
Now, the parser can decide by first parsing if E then S and then checking whether else S follows.
Example 2: Left Factoring a Simple Grammar
Consider the grammar:
Here, both productions begin with a. We left-factor it as:
This refactored grammar ensures that the parser first matches a, then decides between b and c.
Advantages of Left Factoring
- Removes Ambiguity: Helps in resolving ambiguous choices in top-down parsing.
- Improves Parsing Efficiency: Enables LL(1) parsing by ensuring that the decision for which production to choose is clear from the first token.
- Enhances Readability: The grammar becomes more structured and easier to understand.
PRACTICE PROBLEMS BASED ON LEFT FACTORING-
Problem-01: Do left factoring in the following grammar-
S β iEtS / iEtSeS / a
E β b
Solution-
The left factored grammar is-
S β iEtSSβ / a
Sβ β eS / β
E β b
Problem-02: Do left factoring in the following grammar-
A β aAB / aBc / aAc
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: Do left factoring in the following grammar-
S β bSSaaS / bSSaSb / bSb / a
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: Do left factoring in the following grammar-
S β aSSbS / aSaSb / abb / b
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: Do left factoring in the following grammar-
S β a / ab / abc / abcd
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: Do left factoring in the following grammar-
S β aAd / aB
A β a / ab
B β ccd / ddc
Solution- The left factored grammar is-
S β aSβ
Sβ β Ad / B
A β aAβ
Aβ β b / β
B β ccd / ddc
Labels: CD Unit-2, Compiler Design
Discussion & Queries (<$I18NNumComments$>):
<$CommentPager$>
- <$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>
<$CommentPager$>
<$BlogCommentBody$>
<$BlogCommentDeleteIcon$>