Num1337
Num1337

Reputation: 1

Computing FIRST and FOLLOW sets for a grammar

I have to compute the FIRST and FOLLOW sets of the following grammar:

A -> B C
B -> A x | x
C -> y C | y

According to my understanding I get the following computation:

Firstly we remove left recursion

A -> B C
B -> x B'
B' -> C x B' | ε
C -> y C | y

Follow (A) = {$}

But in the book, the answer for Follow (A) = {x,$}

Why? Did they not remove left recursion?

Upvotes: 0

Views: 708

Answers (1)

Thomas Kammeyer
Thomas Kammeyer

Reputation: 4507

You're right about the contents of FOLLOW(A) for these two grammars, as far as I can see, and to a superficial inspection you didn't change the language of the grammar when you eliminated left recursion.

Why did you feel the need to eliminate left recursion? Top-down parsing may not work on left-recursive grammars, but FIRST and FOLLOW are still well-defined. So you don't need to eliminate left recursion just to compute those sets and I'm guessing they didn't do so in your text.

And you probably already realize this, but for completeness I'll add that eliminating left recursion definitely does change the parse trees and sentential forms and, importantly here, FIRST and FOLLOW sets of a grammar without changing L(G). That's actually the point of doing that elimination.

So bottom-line: I think they probably just didn't eliminate left recursion in your book, even though I can't say for sure from what's here.

Upvotes: 0

Related Questions