Given the following grammar:
S -> L=L
s -> L
L -> *L
L -> id
What are the first and follow for the non-terminals?
If the grammar is changed into:
S -> L=R
S -> R
L -> *R
L -> id
R -> L
What will be the first and follow ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
When I took a compiler course in college I didn’t understand FIRST and FOLLOWS at all. I implemented the algorithms described in the Dragon book, but I had no clue what was going on. I think I do now.
I assume you have some book that gives a formal definition of these two sets, and the book is completely incomprehensible. I’ll try to give an informal description of them, and hopefully that will help you make sense of what’s in your book.
The FIRST set is the set of terminals you could possibly see as the first part of the expansion of a non-terminal. The FOLLOWS set is the set of terminals you could possibly see following the expansion of a non-terminal.
In your first grammar, there are only three kinds of terminals:
=,*, andid. (You might also consider$, the end-of-input symbol, to be a terminal.) The only non-terminals areS(a statement) andL(an Lvalue — a “thing” you can assign to).Think of FIRST(S) as the set of non-terminals that could possibly start a statement. Intuitively, you know you do not start a statement with
=. So you wouldn’t expect that to show up in FIRST(S).So how does a statement start? There are two production rules that define what an
Slooks like, and they both start withL. So to figure out what’s in FIRST(S), you really have to look at what’s in FIRST(L). There are two production rules that define what an Lvalue looks like: it either starts with a*or with anid. So FIRST(S) = FIRST(L) = {*,id}.FOLLOWS(S) is easy. Nothing follows
Sbecause it is the start symbol. So the only thing in FOLLOWS(S) is$, the end-of-input symbol.FOLLOWS(L) is a little trickier. You have to look at every production rule where
Lappears, and see what comes after it. In the first rule, you see that=may followL. So=is in FOLLOWS(L). But you also notice in that rule that there is anotherLat the end of the production rule. So another thing that could followLis anything that could follow that production. We already figured out that the only thing that can follow theSproduction is the end-of-input. So FOLLOWS(L) = {=,$}. (If you look at the other production rules,Lalways appears at the end of them, so you just get$from those.)Take a look at this Easy Explanation, and for now ignore all the stuff about
ϵ, because you don’t have any productions which contain the empty-string. Under “Rules for First Sets”, rules #1, #3, and #4.1 should make sense. Under “Rules for Follows Sets”, rules #1, #2, and #3 should make sense.Things get more complicated when you have
ϵin your production rules. Suppose you have something like this:Now if you want to compute FIRST(D) you can’t just look at FIRST(S), because S may be “empty”. You know intuitively that FIRST(D) is {
static,const,int,float}. That intuition is codified in rule #4.2. Think ofSCTin this example asY1Y2Y3in the “Easy Explanation” rules.If you want to compute FOLLOWS(S), you can’t just look at FIRST(C), because that may be empty, so you also have to look at FIRST(T). So FOLLOWS(S) = {
const,int,float}. You get that by applying “Rules for follow sets” #2 and #4 (more or less).I hope that helps and that you can figure out FIRST and FOLLOWS for the second grammar on your own.
If it helps,
Rrepresents an Rvalue — a “thing” you can’t assign to, such as a constant or a literal. An Lvalue can also act as an Rvalue (but not the other way around).