I have the following grammar on which I am trying to learn how to do first and follow. I think I have the FIRST correct. However, the FOLLOW is confusing due to the nonterminal C.
Here is the grammar:
S --> ABC
A --> a | Cb |ε
B --> C | dA | ε
C --> e | f
For the FIRST:
First(S) = First(A)-{ε} + First(C) = { a,f, e, ε}
First(B) = First(C) = {d,e,f,ε}
For the FOLLOW:
Follow(S) = {ε}
Follow(A) = First(B)-{ε} + First(C) = {a,e,f}
Follow(B) = Follow(C) = Follow(S) = { $}
Follow(C) = Follow(B) = Follow(S) = {b, $}
I’m having issues since there are two C one in production A and B?
Am I close to having this?
I think the First already is wrong.
As A and B are optional, and C has a non-empty first:
First(S) = First(A) + First(B) + First(C) - {ε}
First(A) = {a} + First(C) + {ε}
First(B) = First(C) + {d, ε}
First(C) = {e, f}
=>
First(A) = {a, e, f, ε}
First(B) = {d, e, f, ε}
=>
First(S) = {a, d, e, f}
First when occurrence is followed by nt,
Follow of rule when at end.
Follow(S) = {$}
Follow(A) = First(B) - {ε} + Follow(B) = {d, e, f}
Follow(B) = First(C) = {e, f}
Follow(C) = Follow(S) + Follow(B) + {b} = {b, e, f, $}
(I hope I got this right.)
In detail:
Follow(S) = {$} [Start rule]
Follow(A) = First(B) - {ε} [S --> A.BC]
+ First(C) [S --> AB.C as B or First(B) contains ε]
+ Follow(B) [B. --> C | dA. | ε]
Follow(B) = First(C) [S --> AB.C as C does not contain ε stops]
Follow(C) = Follow(S) [S. --> ABC.]
+ {b} [A --> a | C.b |ε]
+ Follow(B) [B. --> C. | dA | ε]
The dot on the LHS like X. --> ... results in Follow(S);
The dot on the RHS like ... --> ... .X ... results in First(X) - {ε}
While there is an ε-production for X, continue: ... --> ... X. ...
Mind, that these are "my" rules, your book might use a slightly different algebra.
I'm answering the following for the grammar. As the firsts are already answered correctly.
follow(S) = {$};
follow(A) = {$,d,e,f};
follow(B) = {$,e,f};
follow(C) = {$,b,e,f};
Reason for including '$' in follow of A, B and C is. If there is a production A ---> ABC in the grammar, where C is nullable then everything in Follow(A) will be Follow of(B).
Related
I have a grammar that looks like this
G = ({A, B, C},{a, c},P, A),
P = {
1. A → BaC,
2. B → aB
3. B → ε,
4. C→c
}
and I want to resolve the FIRST(2.) = {a} FOLLOW(B) = {a} conflict. But with every attempt I fail because I am not able to use the terminal symbol absorption after which the grammar looks like this
G = ({A, B, C, [Ba]},{a, c},P, A),
P = {
1. A → [Ba]C,
2. B → aB
3. B → ε,
4. C → c
5. [Ba] → aBa | a
}
but the problem remains still the same
Suppose I have the following CFG:
S->aACD|BcAe
A->b | EPSILON
B->cf | d
C->fe
Now I apply the FIRST rule on the CFG:
FIRST(S)=FIRST(aAcd) U FIRST(BcAe)
={a} U FIRST(BcAe)
= {a} U FIRST(B)-{EPSILON} U FIRST(cAe)
= {a} U FIRST(B)-{EPSILON} U {c}
= {a} U FIRST(Cf) U FIRST(d) -{EPSILON} U {c}
= {a,f,d,c,EPSILON}
FIRST(A)=FIRST(b) U FIRST(EPSILON)= ={B,EPSILON}
FIRST(B)=FIRST(Cf) U FIRST(d)={d,f}
FIRST(C)=FIRST(fe)={F}
Now I apply the FOLLOW rule on the CFG:
FOLLOW(S)={$}
FOLLOW(A)={c,e}
FOLLOW(B)={c}
FOLLOW(C)={f}
Is there any wrong? If it is wrong please show me how to do it.
Your work above (question) suggest that you are not good in basic concepts. So you can make use of the this tutorial.
Grammer :
S->aACD|BcAe
A->b | EPSILON
B->cf | d
C->fe
No production means EPSILON so,
D-> EPSILON
On applying first rule :
FIRST(A)=FIRST(b) U FIRST(EPSILON)= ={b,EPSILON}
FIRST(B)=FIRST(cf) U FIRST(d)={c} U {d} = {c,d}
FIRST(C)=FIRST(fe)={f}
FIRST(D)={EPSILON}
FIRST(S)=FIRST(aACD) U FIRST(BcAe)
={a} U FIRST(BcAe)
= {a} U FIRST(B)
= {a} U {c,d}
= {a,c,d}
on applying follow rule :
FOLLOW(S)={$}
FOLLOW(A)= FIRST(C) U FIRST(e) = {f,e}
FOLLOW(B)={c}
FOLLOW(C)={$}
FOLLOW(D)={$}
In my current compilers course, I've understood how to find the first and follow sets of a grammar, and so far all of the grammars I have dealt with have contained epsilon. Now I am being asked to find the first and follow sets of a grammar without epsilon, and to determine whether it is LR(0) and SLR. Not having epsilon has thrown me off, so I don't know if I've done it correctly. I would appreciate any comments on whether I am on the right track with the first and follow sets, and how to begin determining if it is LR(0)
Consider the following grammar describing Lisp arithmetic:
S -> E // S is start symbol, E is expression
E -> (FL) // F is math function, L is a list
L -> LI | I // I is an item in a list
I -> n | E // an item is a number n or an expression E
F -> + | - | *
FIRST:
FIRST(S)= FIRST(E) = {(}
FIRST(L)= FIRST(I) = {n,(}
FIRST(F) = {+, -, *}
FOLLOW:
FOLLOW(S) = {$}
FOLLOW(E) = FOLLOW(L) = {), n, $}
FOLLOW(I) = {),$}
FOLLOW(F) = {),$}
The FIRST sets are right, but the FOLLOW sets are incorrect.
The FOLLOW(S) = {$} is right, though technically this is for the augmented grammar S' -> S$ .
E appears on the right side of S -> E and I -> E, both of which mean that the follow of that set is in the follow of E, so: FOLLOW(E) = FOLLOW(S) ∪ FOLLOW(I) .
L appears on the right hand side of L -> LI, which gives FOLLOW(L) ⊇ FIRST(I) , and E -> (FL), which gives FOLLOW(L) ⊇ {)} .
I appears on the right side of L -> LI | I , which gives FOLLOW(I) = FOLLOW(L) .
F appears on the right side in E -> (FL) , which gives FOLLOW(F) = FIRST(L)
Solving for these gives:
FOLLOW(F) = {n, (}
FOLLOW(L) = FIRST(I) ∪ {)} = {n, (, )}
FOLLOW(I) = {n, (, )}
FOLLOW(E) = {$} ∪ {n, (, )} = {n, (, ), $}
Grammar:
E -> TE’
E’ -> +TE’ | ε
T -> FΤ’
Τ’ -> *FΤ’ | ε
F -> (E)| id
Functions:
1. FIRST(F) = FIRST(T) = FIRST(E) = {(, id}
2. FIRST(E’) = {+, ε}
3. FIRST(T’) = {*, ε}
4. FOLLOW(E) = FOLLOW(E’) = {), $}
5. FOLLOW(T) = FOLLOW(T’) = {+, ), $}
6. FOLLOW(F) = {*, +, ), $}
Here is the grammar and the functions from my lectures...Can someone explain me how FOLLOW works??? I understood how FIRST work but FOLLOW is very difficult to understand...
Have a look at Wikipedia's FIRST_and_FOLLOW_sets
.
FOLLOW(E):
You look for any references of E.
Here (E) and union all following terminals and the FIRST-set of the following nonterminals.
Here only the following terminal ).
FOLLOW(F):
F is referenced by FT, *FT'. So FOLLOW(F) is the union of FIRST(T) = {(, id}* and FIRST(T') = {*, ε}.
Finally, FOLLOW(F) = {(, id, *, ε}.
here FOLLOW(F) is find by this way:
T-->FT' means FOLLOW(T) IS subset of FOLLOW(F)
T'-->*FT' means FIRST(T') contain epsilon then except epsilon and add other values to set.
I want to know if my FIRST and FOLLOW set I made for this grammar is correct or not
S -> TS'
S' -> +TS' | -TS' | epsilon
T -> UT'
T' -> *UT' | /UT' | epsilon
U -> VX
X -> ^U | epsilon
V -> (W) | -W | W | epsilon
W -> S | number
FIRST(S) = FIRST(T) = FIRST(U) = FIRST(V) = FIRST(W) = { ( , - , + , number , epsilon }
FIRST(T') = { *, / , epsilon}
FIRST(S') = { + , - , epsilon}
FIRST(X) = { ^ , epsilon}
FOLLOW(S) = FOLLOW(S') = FOLLOW(V) = {$}
FOLLOW(T) = {+ , - , $ }
FOLLOW(T')= {+, - , $ }
FOLLOW(U) = FOLLOW(X) = { * , / , + , - ,$ }
FOLLOW(W) = { ) , $ }
Just a remark:
You said:
FIRST(U) = FIRST(V)
Which is correct, but, V can be epsilon which means FIRST(U) = FIRST(V) + FIRST(X)
And X can be epsilon to.
Those epsilons can be quite frustrating sometimes.
There is a little more to say.
Just a few rules:
- Capitals are nonterminal
- lowercase are terminals
- epsilon is used for an empty rule
- $ is used to note the end of the input.
First(a) = {a}
First(A,B) = First(A), if epsilon is not in First(A)
First(A,B) = First(A) + First(B), if epsilon in First(A)
First(A|B) = First(A) + First(B)
Follow(T) includes $ if T is the start symbol
Follow(T) includes First(A) if there is a rule with ..TA..
Follow(T) includes Follow(A) if there is a rule A -> ..T
Follow(T) includes Follow(A) if there is a rule A -> ..TB and B can be epsilon
Follow(T) never includes epsilon
Example:
E = TE'
E' = +TE'|epsilon
T = FT'
T' = *FT' | epsilon
F = (E) | id
First(E) = First(T) = First(F) = {(, id}
First(E') = {+, epsilon}
First(T) = First(F) = {(, id}
First(T') = {*, epsilon}
First(F) = {(, id}
Follow(E) = {$, )}
Follow(E') = Follow(E) = {$, )}
Follow(T) = First(E') + Follow(E') = {$, ), +}
Follow(T') = Follow(T) = {$, ), +}
Follow(F) = First(T') + Follow(T') + Follow(T) = {*, $, ), +}
Your grammar is much more complex and a bit weird (are you sure there are no mistakes in the grammar?) but you can follow the rules.