Compute the first and follow sets of the grammar

  • Thread starter Thread starter Jncik
  • Start date Start date
  • Tags Tags
    Sets
Click For Summary
SUMMARY

The discussion focuses on computing the FIRST and FOLLOW sets for the grammar defined by the productions S -> ACB | CbB | Ba, A -> da | BC, B -> g | λ, and C -> h | λ. The computed FIRST sets include First(S) = {a, b, d, g, h, λ} and First(B) = {g, λ}. The FOLLOW sets are also derived, with Follow(S) = {$}, Follow(B) = {a, h, $}, and Follow(A) = {h, g, $}. The participants confirm the correctness of the computations, although there are notes indicating errors in the initial FOLLOW set calculations.

PREREQUISITES
  • Understanding of context-free grammars and production rules
  • Familiarity with the concepts of FIRST and FOLLOW sets in parsing
  • Knowledge of λ (epsilon) transitions in grammars
  • Basic skills in formal language theory
NEXT STEPS
  • Study the algorithm for computing FIRST sets in detail
  • Learn the algorithm for calculating FOLLOW sets in context-free grammars
  • Explore parsing techniques that utilize FIRST and FOLLOW sets, such as LL(1) parsing
  • Practice with additional grammar examples to reinforce understanding of FIRST and FOLLOW set computations
USEFUL FOR

Students of computer science, particularly those studying compiler design, formal languages, and automata theory. This discussion is also beneficial for educators and practitioners involved in teaching or implementing parsing algorithms.

Jncik
Messages
95
Reaction score
0

Homework Statement



compute the first and follow sets of the following grammar

S -> ACB | CbB | Ba
A -> da | BC
B -> g|λ
C -> h|λ

The Attempt at a Solution



First(S) = First(ACB) U First(CbB) U First(Ba)

First(ACB) = First(A) - {λ} U First(CB)
First(CbB) = First(C) - {λ} U {b}
First(B) = {g,λ}
First(C) = {h,λ}
First(A) = {d} U First(B) - {λ} U First(C) = {d,g,h,λ}

hence First(CbB) = {h,b}

First(Ba) = {g,a}
First(ACB) = {d,g,h} U {h,b} U {g,a} = {a,b,d,g,h,e}

Follow sets:

Follow(S) = {$} since its initial

Follow(B) we have to check the following

i) S -> ACB
ii) S -> CbB
iii) S -> Ba
iv) A -> BC

i) we add Follow(S) to follow(B) since we have λ after B

hence we add {$}

ii) we add the same {$}

iii) we add {a}
iv) we add First(C) - {λ} U Follow(A) since we can get an empty string from C

hence we add {h,$}

hence Follow(B) = {a,h,$}

for Follow(A) we check S -> ACB and add First(CB) - {λ} U Follow(S)

First(CB) = First(C) - λ U First(B) = {h} U {g,λ}

hence Follow(A) = {h,g,$}

for Follow(B) we have S -> CbB and S -> Ba and A -> BC

hence we have Follow(B) = First(C) - λ U {a} U First(C) - λ U Follow(S)= {h,a,$}please is this correct?

EDIT: the follow sets are wrong, i ll update in 2 minutes

EDIT2: fixed
 
Last edited:
Physics news on Phys.org
Jncik said:

Homework Statement



compute the first and follow sets of the following grammar

S -> ACB | CbB | Ba
A -> da | BC
B -> g|λ
C -> h|λ

The Attempt at a Solution



First(S) = First(ACB) U First(CbB) U First(Ba)

First(ACB) = First(A) - {λ} U First(CB)
First(CbB) = First(C) - {λ} U {b}
First(B) = {g,λ}
First(C) = {h,λ}
First(A) = {d} U First(B) - {λ} U First(C) = {d,g,h,λ}

hence First(CbB) = {h,b}

First(Ba) = {g,a}
First(ACB) = {d,g,h} U {h,b} U {g,a} = {a,b,d,g,h,e}

Follow sets:

Follow(S) = {$} since its initial

Follow(B) we have to check the following

i) S -> ACB
ii) S -> CbB
iii) S -> Ba
iv) A -> BC

i) we add Follow(S) to follow(B) since we have λ after B

hence we add {$}

ii) we add the same {$}

iii) we add {a}
iv) we add First(C) - {λ} U Follow(A) since we can get an empty string from C

hence we add {h,$}

hence Follow(B) = {a,h,$}

for Follow(A) we check S -> ACB and add First(CB) - {λ} U Follow(S)

First(CB) = First(C) - λ U First(B) = {h} U {g,λ}

hence Follow(A) = {h,g,$}

for Follow(B) we have S -> CbB and S -> Ba and A -> BC

hence we have Follow(B) = First(C) - λ U {a} U First(C) - λ U Follow(S)= {h,a,$}please is this correct?

EDIT: the follow sets are wrong, i ll update in 2 minutes

EDIT2: fixed
Follow of b is wrong
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
Replies
5
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
Replies
34
Views
3K
  • · Replies 1 ·
Replies
1
Views
992