0% found this document useful (0 votes)
14 views10 pages

Program 4

The document discusses the concept of left factoring in grammars to resolve ambiguities for top-down parsers. It provides multiple examples of grammars that require left factoring, detailing the steps to transform them into left-factored forms. Each problem illustrates the process of identifying common prefixes and restructuring the grammar accordingly.

Uploaded by

devansh kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Program 4

The document discusses the concept of left factoring in grammars to resolve ambiguities for top-down parsers. It provides multiple examples of grammars that require left factoring, detailing the steps to transform them into left-factored forms. Each problem illustrates the process of identifying common prefixes and restructuring the grammar accordingly.

Uploaded by

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

Wap to implement Left Factoring-

Program no -4
• A → αβ1 / αβ2 / αβ3
• This kind of grammar creates a problematic
situation for Top down parsers.
• Top down parsers can not decide which
production must be chosen to parse the string
in hand.
• To remove this confusion, we use left
factoring.
Example-
Problem-01:

– S -> i E t S | i E t S e S | a
– E -> b

S -> i E t S | i E t S e S | a A->αβ1 | αβ2

α α β2
After applying Left Factoring grammar have following
production
S -> iEtSS’ | a A -> αA’
S’ -> eS | ɛ A’ -> β1 | β2
E-> b
Problem-02:
Do left factoring in the following grammar-
• A → aAB / aBc / aAc

A->αβ1 | αβ2
• β1 =AB, β2=Bc, β3=Ac, α=a

• Step-01: A → aA’
• A’ → AB / Bc / Ac 1) A -> αA’
• 2) A’ -> β1 | β2
Again, this is a grammar with common prefixes.

• Step-02: A’ → AB / Bc / Ac

• β1 =B, β2=c , α=A


• 1) A -> αA’
• 2) A’ -> β1 | β2

• A → aA’
• A’ → AA’’/ Bc
• A’’ → B / c
• This is a left factored grammar.
Problem-03:

• Do left factoring in the following grammar-


• S → bSSaaS / bSSaSb / bSb / a
• Solution- A->αβ1 | αβ2
• β1 = SaaS β2= SaSb, β3=b, α=bS

• Step-01:

• S → bSS’ / a
• S’ → SaaS / SaSb / b
• Again, this is a grammar with common prefixes.
• Step-02:S’ → SaaS / SaSb / b
• Β1=As, β2=Sb α=Sa
• 1) A -> αA’
• 2) A’ -> β1 | β2

• S → bSS’ / a
• S’ → SaS’’ / b
• S’’ → aS / Sb
• This is a left factored grammar.
Problem-04:

Do left factoring in the following


grammar-
• S → aSSbS / aSaSb / abb / b
• Step-01:
• S → aS’ / b
• S’ → SSbS / SaSb / bb
• Again, this is a grammar with common prefixes.
• Step-02:

• S → aS’ / b
• S’ → SAS’’/ bb
• S’’→ SbS / aSb
• This is a left factored grammar.

You might also like