Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Compiler design Left factoring, Cheat Sheet of Compiler Design

Automata or theory of computation really helps in building Compilers. This is really a good powerpoint presentation.

Typology: Cheat Sheet

2020/2021

Uploaded on 05/08/2022

ankan-pradhan
ankan-pradhan 🇮🇳

4

(1)

4 documents

1 / 15

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COMPILER DESIGN
BCA 5th Semester 2020
Topic: Left-Recursion and Left Factoring
Sakhi Bandyopadhyay
Department of Computer Science and BCA
Kharagpur College
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Compiler design Left factoring and more Cheat Sheet Compiler Design in PDF only on Docsity!

COMPILER DESIGN

BCA 5

th

Semester 2020

Topic: Left-Recursion and Left Factoring Sakhi Bandyopadhyay Department of Computer Science and BCA Kharagpur College

Left-Recursion

  1. Left-Recursion S → Sa / ∈
  2. Right-Recursion S → aS / ∈
  3. General-Recursion S → aSb / ∈ A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as variable of its LHS. A grammar containing a production having left recursion is called as Left Recursive Grammar.

Left-Recursion

Problem-1: Consider the following grammar and eliminate left recursion- A → AAα / β Solution: The grammar after eliminating left recursion is- A → βA’ A’ → AαA’ / ∈ Problem-2: Consider the following grammar and eliminate left recursion- S → S0S1S / 01 Solution: The grammar after eliminating left recursion is- S → 01A A → 0S1SA / ∈

Left-Recursion

Problem-3: Consider the following grammar and eliminate left recursion- A → ABd / Aa / a B → Be / b Solution: The grammar after eliminating left recursion is- A → aA’ A’ → BdA’ / aA’ / ∈ B → bB’ B’ → eB’ / ∈ Problem-4: Consider the following grammar and eliminate left recursion- E → E + E / E x E / a Solution: The grammar after eliminating left recursion is- E → aA A → +EA / xEA / ∈

Left-Factoring

Left-Factoring

Problem-1: Do left factoring in the following grammar- A → aAB / aBc / aAc A → aA’ A’ → AB / Bc / Ac A → aA’ A’ → AD / Bc D → B / c This is a left factored grammar.

Left-Recursion vs Left-Factoring

Left Recursion: A grammar is left recursive if it has a nonterminal A such that there is a derivation A -> Aα | β where α and β are sequences of terminals and nonterminals that do not start with A. While designing a top down-parser, if the left recursion exist in the grammar then the parser falls in an infinite loop, here because A is trying to match A itself, which is not possible. We can eliminate the above left recursion by rewriting the offending production. As- A -> βA' A' -> αA' | epsilon Left Factoring: Left factoring is required to eliminate non-determinism of a grammar. Suppose a grammar, S -> abS | aSb Here, S is deriving the same terminal a in the production rule(two alternative choices for S), which follows non-determinism. We can rewrite the production to defer the decision of S as- S -> aS' S' -> bS | Sb Thus, S' can be replaced for bS or Sb

FIRST and FOLLOW

FIRST

First(α) is a set of terminal symbols that begin in strings derived from α. Consider the production rule- A → abc / def / ghi Then, we have- First(A) = { a , d , g }

FIRST and FOLLOW

FOLLOW

Follow(α) is a set of terminal symbols that appear immediately to the right of α.

  • (^) For the start symbol S, place $ in Follow(S).
  • (^) For any production rule A → αB, Follow(B) = Follow(A)
  • (^) For any production rule A → αBβ, If ∈ ∉ First(β), then Follow(B) = First(β) If ∈ ∈ First(β), then Follow(B) = { First(β) – ∈ } ∪ Follow(A)

FIRST and FOLLOW

∈ may appear in the first function of a non-terminal. ∈ will never appear in the follow function of a non-terminal. Before calculating the first and follow functions, eliminate Left Recursion from the grammar, if present.