0% found this document useful (0 votes)
130 views8 pages

Compiler Design: Examples of First & Follow Prof. Jigar Dalvadi

The document discusses finding the first and follow sets for context-free grammars. It provides examples of calculating first and follow for specific grammars and constructing parsing tables to determine if grammars are LL(1). The examples show determining first and follow sets for nonterminals, using these to populate parsing tables, and analyzing if grammars are LL(1) based on the tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
130 views8 pages

Compiler Design: Examples of First & Follow Prof. Jigar Dalvadi

The document discusses finding the first and follow sets for context-free grammars. It provides examples of calculating first and follow for specific grammars and constructing parsing tables to determine if grammars are LL(1). The examples show determining first and follow sets for nonterminals, using these to populate parsing tables, and analyzing if grammars are LL(1) based on the tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

COMPILER DESIGN

EXAMPLES OF FIRST & FOLLOW

Prof. Jigar Dalvadi


Find first and follow for grammar:
S → aBDh
B → cC
C → bC / ∈
D → EF
E→g/∈
F→f /∈
Solution: First & Follow
S → aBDh B → cC C → bC / ∈ D → EF E→g/∈
F→f /∈

First(S) = { a } • Follow(S) = { $ }
First(B) = { c } • Follow(B) = { g , f , h }
First(C) = { b , ∈ } • Follow(C) = { g , f , h }
First(D) = { g , f , ∈ } • Follow(D) = { h }
First(E) = { g , ∈ } • Follow(E) = { f , h }
First(F) = { f , ∈ } • Follow(F) = { h }
First(S) = { a } • Follow(S) = { $ }
First(B) = { c } • Follow(B) = { g , f , h }
First(C) = { b , ∈ } • Follow(C) = { g , f , h }
Solution: First(D) = { g , f , ∈ } • Follow(D) = { h }
First(E) = { g , ∈ } • Follow(E) = { f , h }
Parsing Table First(F) = { f , ∈ } • Follow(F) = { h }

a b c g e f h $
S S → aBDh

B B → cC

C C → bC C→∈ C→∈ C→∈

D D → EF D → EF D→∈

E E→g E→∈ E→∈

F F→f F→∈
Find first and follow for grammar:
S → AaAb
S → BbBa
A→ε
B→ε
Solution: Parsing Table
• First(S) = { a , b } a b $
• First(A) = { ∈ }
• First(B) = { ∈ } S S → AaAb S → BbBa

A A→∈ A→∈
• Follow(S) = { $ }
• Follow(A) = {a, b} B B→∈ B→∈
• Follow(B) = {a, b }
S → UVW
U→ (S) | aSb | d Check whether grammar is LL(1) or not ?
V→ aV | ε
W→ cW | ε
Parsing Table
Solution:
a b c d ( ) $
• First(S) = { ( , a , d }
• First(U) = { ( , a , d } S→ S→ S→
S UVW UVW UVW
• First(V) = { a , ∈ }
• First(W) = {c , ∈ } U U→ aSb U→ d U→ (S)

Follow(S) = { ) , b , $}
Follow(U) = {a, c , ) , b , $} V V→ aV V→∈ V→∈ V→∈ V→∈

Follow(V) = {c , ) , b , $}
W W→∈ W→ cW W→∈ W→∈
Follow(W)={) , b , $}
S → AB |eDa
A → ab |c
B → dC
C → eC | ε
D → fD | ε LL(1) or not ?

a b c d e f $
First(S)={a,c,e} Follow(S)={$}
S→ S→ S→
First(A)={a,c} Follow(A)={d} S
AB AB eDa
First(B)={d} Follow(B)={$} A→ A→
A
First(C)={e, ε} Follow(C)={$} ab c
B→
First(D)={f, ε} Follow(D)={a} B
dC
C→ C→
C
eC ε
D→ D→
D
ε fD
Self Practice:

S->Aa/bAc/Bc/bBa
A->d
B->d

You might also like