- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# What are FIRST and FOLLOW and how they are computed?

FIRST and FOLLOW are two functions associated with grammar that help us fill in the entries of an M-table.

**FIRST ()**− It is a function that gives the set of terminals that begin the strings derived from the production rule.

A symbol c is in FIRST (α) if and only if α ⇒ cβ for some sequence β of grammar symbols.

A terminal symbol a is in FOLLOW (N) if and only if there is a derivation from the start symbol S of the grammar such that S ⇒ αNαβ, where α and β are a (possible empty) sequence of grammar symbols. In other words, a terminal c is in FOLLOW (N) if c can follow N at some point in a derivation.

**Benefit of FIRST ( ) and FOLLOW ( )**

It can be used to prove the LL (K) characteristic of grammar.

It can be used to promote in the construction of predictive parsing tables.

It provides selection information for recursive descent parsers.

**Computation of FIRST**

FIRST (α) is defined as the collection of terminal symbols which are the first letters of strings derived from α.

FIRST (α) = {α |α →∗ αβ for some string β }

**If X is Grammar Symbol, then First (X) will be −**

- If X is a terminal symbol, then FIRST(X) = {X}
- If X → ε, then FIRST(X) = {ε}
- If X is non-terminal & X → a α, then FIRST (X) = {a}
- If X → Y
_{1}, Y_{2}, Y_{3}, then FIRST (X) will be

(a) If Y is terminal, then

FIRST (X) = FIRST (Y_{1}, Y_{2}, Y_{3}) = {Y_{1}}

(b) If Y_{1} is Non-terminal and

If Y_{1} does not derive to an empty string i.e., If FIRST (Y_{1}) does not contain ε then, FIRST (X) = FIRST (Y_{1}, Y_{2}, Y_{3}) = FIRST(Y_{1})

(c) If FIRST (Y_{1}) contains ε, then.

FIRST (X) = FIRST (Y_{1}, Y_{2}, Y_{3}) = FIRST(Y_{1}) − {ε} ∪ FIRST(Y_{2}, Y_{3})

Similarly, FIRST (Y_{2}, Y_{3}) = {Y_{2}}, If Y_{2} is terminal otherwise if Y_{2} is Non-terminal then

FIRST (Y

_{2}, Y_{3}) = FIRST (Y_{2}), if FIRST (Y_{2}) does not contain ε.If FIRST (Y

_{2}) contain ε, thenFIRST (Y

_{2}, Y_{3}) = FIRST (Y_{2}) − {ε} ∪ FIRST (Y_{3})

Similarly, this method will be repeated for further Grammar symbols, i.e., for Y_{4}, Y_{5}, Y_{6} … . Y_{K}.

**Computation of FOLLOW**

**Follow (A) is defined as the collection of terminal symbols that occur directly to the right of A.**

FOLLOW(A) = {a|S ⇒* αAaβ where α, β can be any strings}

**Rules to find FOLLOW**

If S is the start symbol, FOLLOW (S) ={$}

If production is of form A → α B β, β ≠ ε.

(a) If FIRST (β) does not contain ε then, FOLLOW (B) = {FIRST (β)}

Or

(b) If FIRST (β) contains ε (i. e. , β ⇒* ε), then

FOLLOW (B) = FIRST (β) − {ε} ∪ FOLLOW (A)

∵ when β derives ε, then terminal after A will follow B.

- If production is of form A → αB, then Follow (B) ={FOLLOW (A)}.

- Related Articles
- What are A-shares and how are they traded?
- What are contra entries and how are they recorded?
- What are decorators and how are they used in JavaScript?
- How are the exception values computed?
- What are assertions in Python and how are they carried out?
- What are Digital Signatures and how do they work?
- How are measures computed in data mining?
- What Are Krugerrands and What Do They Mean?
- How are iOS and Android similar? How are they different?
- What are Pagoda’s and why they were built?
- What is explosion, and why they are bad?
- What are ball bearings? How and where are they used? Explain with an image.
- What are native methods in Java and where are they used?
- What are private keys and public keys? How do they work?
- What Are Root Nodules ? How They Are Helpful