πŸŽ“ Computer Science & Engineering Portal

Master Engineering Disciplines with Structured Notes

Comprehensive academic lecture notes, exam-oriented unit summaries, laboratory manuals, and previous year question papers designed strictly for university students.

πŸ“‘

University Syllabi

AKTU & AICTE aligned semester guidelines.

πŸ“

Exam Question Papers

Previous 5 years solved university papers.

πŸ’‘

Lab Manuals & Viva

Practical codes with outputs & interview Qs.

Core Subjects & Units Hub

Click on any specific unit to immediately view its lecture notes below

Viewing All Lectures

First and Follow

The construction of a predictive parser is aided by two functions associated with a grammar G. These functions, FIRST and FOLLOW, allow us to fill in the entries of a predictive parsing table for G, whenever possible. Sets of tokens yielded by the FOLLOW function can also be used as synchronizing tokens during panic-mode error recovery.

1. FIRST(a)

First(a) = set of terminals that start a string of terminals derived from a.

Apply following rules until no terminal or Ξ΅ can be added

1. If t  ∈ T, then First( t ) = { t }.  

2. If X ∈ N and X β†’ Ξ΅ exists (nullable), then add Ξ΅ to First( X ).

3. If X ∈ N and X β†’  Y1Y2Y3… Ym, where Y1, Y2, Y3, ... Ym are non‐terminals, then:     for each, I from 1 to m     if Y1 … Yi‐1 are all nullable (or if i = 1)      First( X ) = First( X ) βˆͺ First( Yi )

2. FOLLOW (a) 

Apply the following rules until no terminal or e can be added

1. $  βˆˆ Follow( S ), where S is the start symbol.

2. Look at the occurrence of a non‐terminal on the right-hand side of a production which is followed by something If A β†’ a B b, then First( b ) β€ {e} βŠ† Follow( B )

3. Look at N on the RHS that is not followed by anything, if (A β†’ a B) or (A β†’ a B b and Ξ΅ οƒŽ First( b )), then Follow( A ) βŠ†  Follow( B )


PRACTICE PROBLEMS BASED ON CALCULATING FIRST AND FOLLOW-

 

Problem-01: 

Calculate the first and follow functions for the given grammar-

 

S β†’ aBDh

B β†’ cC

C β†’ bC / ∈

D β†’ EF

E β†’ g / ∈

F β†’ f / ∈

 

Solution- 

The first and follow functions are as follows-

 

First Functions- 

  • First(S) = { a }
  • First(B) = { c }
  • First(C) = { b , ∈ }
  • First(D) = { First(E) – ∈ } βˆͺ First(F) = { g , f , ∈ }
  • First(E) = { g , ∈ }
  • First(F) = { f , ∈ }

 

Follow Functions-

  • Follow(S) = { $ }
  • Follow(B) = { First(D) – ∈ } βˆͺ First(h) = { g , f , h }
  • Follow(C) = Follow(B) = { g , f , h }
  • Follow(D) = First(h) = { h }
  • Follow(E) = { First(F) – ∈ } βˆͺ Follow(D) = { f , h }
  • Follow(F) = Follow(D) = { h }

 

Problem-02: 

Calculate the first and follow functions for the given grammar-

 

S β†’ A

A β†’ aB / Ad

B β†’ b

C β†’ g

 

Solution- 

We have-

  • The given grammar is left recursive.
  • So, we first remove left recursion from the given grammar.

 

After eliminating left recursion, we get the following grammar-

 

S β†’ A

A β†’ aBA’

A’ β†’ dA’ / ∈

B β†’ b

C β†’ g

 

Now, the first and follow functions are as follows-

 

First Functions- 

  • First(S) = First(A) = { a }
  • First(A) = { a }
  • First(A’) = { d , ∈ }
  • First(B) = { b }
  • First(C) = { g }

 

Follow Functions- 

  • Follow(S) = { $ }
  • Follow(A) = Follow(S) = { $ }
  • Follow(A’) = Follow(A) = { $ }
  • Follow(B) = { First(A’) – ∈ } βˆͺ Follow(A) = { d , $ }
  • Follow(C) = NA

 

Problem-03: 

Calculate the first and follow functions for the given grammar-

 

S β†’ (L) / a

L β†’ SL’

L’ β†’ ,SL’ / ∈

 

Solution- 

The first and follow functions are as follows-

 

First Functions- 

  • First(S) = { ( , a }
  • First(L) = First(S) = { ( , a }
  • First(L’) = { , , ∈ }

 

Follow Functions- 

  • Follow(S) = { $ } βˆͺ { First(L’) – ∈ } βˆͺ Follow(L) βˆͺ Follow(L’) = { $ , , , ) }
  • Follow(L) = { ) }
  • Follow(L’) = Follow(L) = { ) }

 

Problem-04: 

Calculate the first and follow functions for the given grammar-

 

S β†’ AaAb / BbBa

A β†’ ∈

B β†’ ∈

 

Solution-

The first and follow functions are as follows-

 

First Functions- 

  • First(S) = { First(A) – ∈ } βˆͺ First(a) βˆͺ { First(B) – ∈ } βˆͺ First(b) = { a , b }
  • First(A) = { ∈ }
  • First(B) = { ∈ }

 

Follow Functions- 

  • Follow(S) = { $ }
  • Follow(A) = First(a) βˆͺ First(b) = { a , b }
  • Follow(B) = First(b) βˆͺ First(a) = { a , b }

 

Problem-05: 

Calculate the first and follow functions for the given grammar-

 

E β†’ E + T / T

T β†’ T x F / F

F β†’ (E) / id

 

Solution- 

We have-

  • The given grammar is left recursive.
  • So, we first remove left recursion from the given grammar.

 

After eliminating left recursion, we get the following grammar-

 

E β†’ TE’

E’ β†’ + TE’ / ∈

T β†’ FT’

T’ β†’ x FT’ / ∈

F β†’ (E) / id

 

Now, the first and follow functions are as follows-

 

First Functions- 

  • First(E) = First(T) = First(F) = { ( , id }
  • First(E’) = { + , ∈ }
  • First(T) = First(F) = { ( , id }
  • First(T’) = { x , ∈ }
  • First(F) = { ( , id }

 

Follow Functions- 

  • Follow(E) = { $ , ) }
  • Follow(E’) = Follow(E) = { $ , ) }
  • Follow(T) = { First(E’) – ∈ } βˆͺ Follow(E) βˆͺ Follow(E’) = { + , $ , ) }
  • Follow(T’) = Follow(T) = { + , $ , ) }
  • Follow(F) = { First(T’) – ∈ } βˆͺ Follow(T) βˆͺ Follow(T’) = { x , + , $ , ) }

 

Problem-06: 

Calculate the first and follow functions for the given grammar-

 

S β†’ ACB / CbB / Ba

A β†’ da / BC

B β†’ g / ∈

C β†’ h / ∈

 

Solution- 

The first and follow functions are as follows-

 

First Functions- 

  • First(S) = { First(A) – ∈ } βˆͺ { First(C) – ∈ } βˆͺ First(B) βˆͺ First(b) βˆͺ { First(B) – ∈ } βˆͺ First(a) = { d , g , h , ∈ , b , a }
  • First(A) = First(d) βˆͺ { First(B) – ∈ } βˆͺ First(C) = { d , g , h , ∈ }
  • First(B) = { g , ∈ }
  • First(C) = { h , ∈ }

 

Follow Functions-

 Follow(S) = { $ }

  • Follow(A) = { First(C) – ∈ } βˆͺ { First(B) – ∈ } βˆͺ Follow(S) = { h , g , $ }
  • Follow(B) = Follow(S) βˆͺ First(a) βˆͺ { First(C) – ∈ } βˆͺ Follow(A) = { $ , a , h , g }
  • Follow(C) = { First(B) – ∈ } βˆͺ Follow(S) βˆͺ First(b) βˆͺ Follow(A) = { g , $ , b , h }

Labels: ,

Discussion & Queries (<$I18NNumComments$>):

<$CommentPager$>
<$I18NAtCommentTimeWithPermalink$>, <$I18NCommentAuthorSaid$>

<$BlogCommentBody$>

<$BlogCommentDeleteIcon$>
<$CommentPager$>