100% found this document useful (1 vote)
7K views

Left Factoring-: Factored Grammar

Left factoring is a process that transforms grammars containing common prefixes into an equivalent grammar more suitable for top-down parsing. It involves identifying common prefixes between productions, creating a new production for each unique prefix, and distributing the remainders to new productions. The document provides examples of applying left factoring to several grammars step-by-step and discusses how it results in a left-factored grammar.

Uploaded by

Muna Majeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
7K views

Left Factoring-: Factored Grammar

Left factoring is a process that transforms grammars containing common prefixes into an equivalent grammar more suitable for top-down parsing. It involves identifying common prefixes between productions, creating a new production for each unique prefix, and distributing the remainders to new productions. The document provides examples of applying left factoring to several grammars step-by-step and discusses how it results in a left-factored grammar.

Uploaded by

Muna Majeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Left Factoring-

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:

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-


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

Problem-03:

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

To gain better understanding about Left Factoring,


Watch this Video Lecture

Next Article- Relationship With Left Recursion

Get more notes and other study material of Compiler Design.


Watch video lectures by visiting our YouTube channel LearnVidFun.
Summary
Formatted: Font: (Default) Arial, 10
pt, Font color: Gray-80%, Complex
Script Font: Arial, 10 pt
Article Name
Left Factoring | Left Factoring Examples
Description
In compiler design, left factoring is a process to transform the grammar with common prefixes.
Left Factoring Examples. Problems to perform left factoring on given grammars.
Author
Akshay Singhal
Publisher Name
Gate Vidyalay
Publisher Logo

You might also like