0% found this document useful (0 votes)
71 views4 pages

LL (1) Parser

Left recursion occurs when the leftmost variable of the right-hand side (RHS) of a grammar production is the same as the variable on the left-hand side (LHS). This can cause problems for parsers. Left recursion is removed by converting the grammar to a right recursive form. Left factoring removes common prefixes from grammar productions to avoid backtracking by the parser. It works by making a new production for the common prefix and adding the remaining derivations by new productions.

Uploaded by

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

LL (1) Parser

Left recursion occurs when the leftmost variable of the right-hand side (RHS) of a grammar production is the same as the variable on the left-hand side (LHS). This can cause problems for parsers. Left recursion is removed by converting the grammar to a right recursive form. Left factoring removes common prefixes from grammar productions to avoid backtracking by the parser. It works by making a new production for the common prefix and adding the remaining derivations by new productions.

Uploaded by

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

TOP-DOWN PARSER: LL(1) PARSER

WHAT IS LEFT RECURSION?

Recursion: It is a process of executing a statement or multiple statements


repeatedly by calling a function itself.
A production of grammar is said to have left recursion if the leftmost variable of its
RHS is same as variable of its LHS
Example: A->Aα|β
                  where β does not begin with an A.

WHY WE NEED TO REMOVE LEFT RECURSION?

Left recursion often poses problems for parser, either because it leads them into
infinite recursion.

REMOVAL OF LEFT RECURSION

Left recursion is eliminated by converting the grammar into a right recursive


grammar.
A -> Aα|β
where β does not begin with an A.
A ->βA’
A’ ->αA’|€

SOME MORE EXAMPLES

Q1.E -> E+T|T


     T -> T*F|F
      F ->(E)|id
Ans: E->TE’
        E’->+TE’|€
        T->FT’
        T’->*FT’|€

Q2.S->Aa
     A->Sb|c

Ans: This is a case of indirect left recursion.


Replace the production of ‘S’ in ‘A’. Now, given grammar becomes-
        A->Aab|c
Using Left recursion
        A->cA’
        A’->abA’|€

Try to solve the given questions

Q.S → (L) / a
  L→L,S/S
Q. A → Ba / Aa / c
     B → Bb / Ab / dw

WHAT IS LEFT FACTORING?

Left factoring is removing the common left factor that appears in two productions
of the same non-terminal.
Left factoring is a process by which the grammar with common prefixes is
transformed to make it useful for Top down parser. It consists in “factoring out”
prefixes which are common to two or more productions.
HOW TO REMOVE LEFT FACTORING?

1.We make one production for each common prefixes.


2.The common prefix may be a terminal or a non-terminal or a combination of
both.
3.Rest of the derivation is added by new productions.

Example: A->αβ1| αβ2


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

WHY NEED TO REMOVE LEFT FACTORING?

It is done to avoid back-tracing by the parser.

SOME MORE EXAMPLES

Q1. A -> qB | qC
Ans: A -> qA’
        A’ -> B | C

Q2.S->iEtS|iEtSeS|€, E->b
Ans: S->iEtSS’|€
         S’->€|eS
         E->b

TRY TO SOLVE GIVEN QUESTIONS

Q. A → aAB / aBc / aAc


Q. S → bSSaaS / bSSaSb / bSb / a

Q3:S → aAd / aB
A → a / ab
B → ccd / ddc

ANS: S → aS’
S’ → Ad / B
A → aA’
A’ → b / ∈
B → ccd / ddc

Q4: S → a / ab / abc / abcd

ANS: S → aS’
S’ → bA / ∈
A → cB / ∈
B → d / ∈

LINK : https://www.gatevidyalay.com/left-factoring-examples-compiler-
design/

You might also like