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.
Related
below is the grammar that i am using for a calculator language and my attempt at finding the follow set and the first set of the grammar.
I would love help in figuring out what i am doing wrong when trying to figure out these sets because I feel like i am not doing them correctly at all (at least for the follow sets)
Grammar
program → stmt_list $$$
stmt_list → stmt stmt_list | ε
stmt → id = expr | input id | print expr
expr → term term_tail
term_tail → add op term term_tail | ε
term → factor fact_tail
fact_tail → mult_op fact fact_tail | ε
factor → ( expr ) | number | id
add_op → + | -
mult_op → * | / | // | %
First set
first(p) = {id, input, print}
first(stmt_list) = {id, input, print, e}
first(s) = {id, input, print}
first(expr) = {(, id, number}
first(term_tail) = {+, -, e}
first(term) = {(, id, number}
first(fact_tail) = {, /, //, %, e}
first(factor) = {(, id, number}
first(add_op) = {+, -}
first(mult_op) = {, /, //, %}
Follow Set
follow(p) = {$}
follow(stmt_list) = {$}
follow(stmt) = {id, input, print}
follow(expr) = {(, id, number, ), input, print, , /, //, %}
follow(term_tail) = {), (, id, number, print, input}
follow(term) = {+, -}
follow(factor) = {, /, //, %}
follow(add_op) = {}
follow(mult_op) = {}
follow(fact_tail) = {*, /, //, %, +, -}
You have certain mistakes in First as well
first(p) = {id, input, print,e}
it will include epsilon
* is missing in the next two -
first(fact_tail) = { *,/, //, %, e} first(mult_op) = {*, /, //, %}
fact_tail → mult_op fact fact_tail | ε
Iam assuming here you actually mean
fact_tail → mult_op factor fact_tail | ε
Follow
follow(stmt) = {id, input, print,$}
if you refer to
stmt_list → stmt stmt_list | ε
then stmt is followed by first of stmt_list which includes e so string generated will end, hence stmt is followed by $
follow(expr) = {(, id, number, ), input, print, , /, //, %}
I don't know how you got this, follow of expr is equal to follow of stmt and )
follow(expr) = {id, ), input, print,$}
follow(term_tail) is equal to follow(expr)
follow(term) = {+,-,),id,input,print,$}
follow(fact_tail) is equal to follow(term)
follow(factor) = first(fact_tail)
follow(add_op) = first(term)
follow(mult_op) = first(factor)
I am working on trying to compute the FOLLOW set of the following grammar:
E -> TX
T -> int Y | ( E )
X -> + E | ε
Y -> * T | ε
I have calculated the following FOLLOW set so far:
follow (E) = {$} U {)}
follow (Y) = follow (T)
follow (T) = follow (Y)
follow (X) = follow (E) = {$, )}
follow (E) = first ()) = {)}
I know that the follow (T) / follow (Y) contains {+,$,)} but I am struggling to get to that point.
Any assistance in explaining the method here would be greatly helpful.
Note: I have followed these rules
1) If A is the start symbol put $ in Follow (A)
2) If there is a production B -> αAb, then Follow (A) = First (b)
3) If there is a production B -> aA or B -> αAb where First (b) is ε, add Follow (A) = Follow (B)
I have figured it out (and spent the better part of the afternoon)!
So the rules I'm following for anyone who finds this are:
follow(E) = follow(T)
follow(E) = first ())
follow(X) = follow(E)
follow(Y) = follow(T)
**follow(T) = first(X)** //the important one!
Following these rules you can build the sets:
follow(E) = {$, )}
follow(T) = {$, ), +}
follow(X) = {$, )}
follow(Y) = {$, ), +}
Which concludes the follow sets for the grammar!
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, (, ), $}
Let G be a grammar such that:
S -> aBa
B -> bB | \epsilon
where \epsilon represents the empty string.
After computing FIRST and FOLLOW, is there a way to tell if G is LL(1) without resorting to the parsing table?
After computing the FIRST and FOLLOW sets for the variables of G, you can compute the length 1 lookahead sets LA(1) for the variables and rules of G. Then G is strong LL(1) iff the following condition holds:
LA(1)(A -> wi) partition LA(1)(A) for each variable A such that A -> wi is a rule.
Alternatively, you can prove that G is strong LL(1) from the definition of a strong LL(k) grammar without computing the FIRST and FOLLOW sets. This is oftentimes easier and less tedious than computing FIRST and FOLLOW for small grammars like G.
I don't have a book handy, so there might be an error in some of these definitions or computations. But this is how I would approach the problem. Computing the FIRST and FOLLOW sets gives:
FIRST(1)(S) = trunc(1)({x : S =>* x AND x IN Σ*})
= trunc(1)({ab^na : n >= 0})
= {a}
FIRST(1)(B) = trunc(1)({x : B =>* x AND x IN Σ*})
= trunc(1)({b^n : n >= 0})
= {ε,b}
FOLLOW(1)(S) = trunc(1)({x : S =>* uSv AND x IN FIRST(1)(v)})
= trunc(1)({x : x IN FIRST(1)(ε)})
= trunc(1)(FIRST(1)(ε))
= {ε}
FOLLOW(1)(B) = trunc(1)({x : S =>* uBv AND x IN FIRST(1)(v)})
= trunc(1)({x : x IN FIRST(1)(a)})
= trunc(1)(FIRST(1)(a))
= {a}
Computing the length 1 lookahead sets for the variables and rules gives:
LA(1)(S) = trunc(1)(FIRST(1)(S)FOLLOW(1)(S))
= trunc(1)({a}{ε})
= trunc(1){a}
= {a}
LA(1)(B) = trunc(1)(FIRST(1)(B)FOLLOW(1)(B))
= trunc(1)({ε,b}{a})
= trunc(1){a,b}
= {a,b}
LA(1)(S -> aBa) = trunc(1)(FIRST(1)(a)FIRST(1)(B)FIRST(1)(a)FOLLOW(1)(S))
= trunc(1){a}
= {a}
LA(1)(B -> bB) = trunc(1)(FIRST(1)(b)FIRST(1)(B))
= trunc(1){b}
= {b}
LA(1)(B -> ε) = trunc(1)(FIRST(1)(ε)FOLLOW(1)(b))
= trunc(1)({ε}{a})
= {a}
Since LA(1)(B -> ε) and LA(1)(B -> bB) partition LA(1)(B) and LA(1)(S -> aBa) trivially partitions LA(1)(S), G is strong LL(1).
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.