Compute the first and follow sets of the grammar

  • Thread starter Jncik
  • Start date
  • Tags
    Sets
In summary, the first set for the given grammar is {a,b,d,g,h,e} and the follow sets are {a,h,$} for A, {a,h,$} for B, and {$} for S. The follow of b is {a,h,$}.
  • #1
Jncik
103
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
  • #2
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
 

Related to Compute the first and follow sets of the grammar

1. What are first and follow sets in grammar?

First and follow sets are used in computer science to analyze and generate parsers for context-free grammars. They help determine which symbols can be derived from a given non-terminal symbol in a grammar and which symbols can follow a given production rule.

2. Why are first and follow sets important?

First and follow sets are important because they are used in the process of building a parser for a programming language or other formal language. They help to determine the structure and rules of the language, which is necessary for creating a working compiler or interpreter.

3. How do you compute first and follow sets of a grammar?

The process of computing first and follow sets involves analyzing the production rules of a grammar and determining which symbols can be derived from a given non-terminal symbol, as well as which symbols can follow a given production rule. This can be done manually or by using algorithms and tools specifically designed for this purpose.

4. What are some applications of first and follow sets in computer science?

First and follow sets are primarily used in the development of compilers and interpreters for programming languages. They can also be used in other areas of computer science, such as natural language processing and artificial intelligence, to analyze and generate grammars for formal languages.

5. Are there any limitations or challenges when computing first and follow sets?

One limitation of first and follow sets is that they may not be able to handle all types of grammars, such as those with left recursion. Additionally, computing first and follow sets can be a time-consuming and complex process, especially for larger grammars with many production rules. However, there are tools and techniques available to help with this process.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
989
  • Engineering and Comp Sci Homework Help
Replies
28
Views
2K
  • Advanced Physics Homework Help
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
34
Views
816
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Linear and Abstract Algebra
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
787
  • Calculus and Beyond Homework Help
Replies
10
Views
1K
Back
Top