7

Consider a short gramma bellow

S -> Bc | DB
B -> ab | cS
D -> d | epsilon

The FIRST set is

FIRST(S) ={a,c,d}
FIRST(B) = { a,c }
FIRST(D)= { d, epsilon }

in it the

Follow(S)={ Follow(B) }

and

Follow(B) ={ c , Follow(S) }

my question is that how to resolve this circular dependency ?

Zabi
  • 845
  • 2
  • 9
  • 15

1 Answers1

4

This circular dependency shouldn't be there to start with. this is the algorithm for finding 'follow's:

Init all follow groups to {}, except S which is init to {$}.
While there are changes, for each A∈V do:
  For each Y → αAβ do:
    follow(A) = follow(A) ∪ first(β)
    If β ⇒* ε, also do: follow(A) = follow(A) ∪ follow(Y)

So in your case, you should get:
Follow(S)={c,$}
Follow(B)={c,$}
Follow(D)={a,c}

Eran Zimmerman Gonen
  • 4,375
  • 1
  • 19
  • 31
  • Excuse me, @Eran Zimmerman. I understand your solution, but you didn't say how you resolved the circular dependency. – Cheshie Feb 13 '14 at 18:26
  • I didn't, because there is nothing to resolve here - the circular dependency is a mock one - you aren't supposed to define a follow group via other follow groups. Another way to look at it is to say I resolved the circular dependency by using the algorithm I gave, which resolved it for you (gave you equivalent follow sets that aren't inter-dependent). – Eran Zimmerman Gonen Feb 14 '14 at 11:07
  • Thanks @Eran but I think I didn't get you. Using the algorithm to find Follow(B), I'd say that because `S->DB`, (beta=epsilon) Follow(B) includes Follow(S). Using `B->cS`, (beta=epsilon) we find that Follow(S) includes Follow(B). And that's a circular dependency. Why is it a 'mock one'? Where did I get you wrong...? Thanks again :-) – Cheshie Feb 15 '14 at 17:49
  • 2
    I think I understand now. When the algorithm says you should union with follow(Y), it simply means the current follow(Y), as you've calculated it so far. Also, since this whole algorithm says "while there are changes", then if such a follow(Y) group will change, it will eventually reflect in follow(A). As you can see in this case, follow(B) indeed includes follow(S) (they are in fact equal - both are `{c,$}`). – Eran Zimmerman Gonen Feb 15 '14 at 19:23