# FOLLOW Set in Syntax Analysis

Last Updated : 05 Apr, 2024

FOLLOW set in compiler design are used to identify the terminal symbol immediately after a non-terminal in a given language. FOLLOW set is also used to avoid backtracking same as the FIRST set. The only difference is FOLLOW set works on vanishing non-terminal on the right-hand side so that decision-making gets easier for the compiler while parsing

Follow(X) to be the set of terminals that can appear immediately to the right of Non-Terminal X in some sentential form.
Example:

`S ->Aa | AcA ->b        S                  S       /  \              /   \    A    a            A     c    |                 |    b                 b   Here, FOLLOW (A) = {a, c}`

### Rules to compute FOLLOW set:

`1) FOLLOW(S) = { \$ }   // where S is the starting Non-Terminal2) If A -> pBq is a production, where p, B and q are any grammar symbols,   then everything in FIRST(q)  except Ð„ is in FOLLOW(B).3) If A->pB is a production, then everything in FOLLOW(A) is in FOLLOW(B).4) If A->pBq is a production and FIRST(q) contains Ð„,    then FOLLOW(B) contains { FIRST(q) â€“ Ð„ } U FOLLOW(A) `

Example 1:

`Production Rules:E -> TEâ€™Eâ€™ -> +T Eâ€™|Ð„T -> F Tâ€™Tâ€™ -> *F Tâ€™ | Ð„F -> (E) | idFIRST setFIRST(E) = FIRST(T) = { ( , id }FIRST(Eâ€™) = { +, Ð„ }FIRST(T) = FIRST(F) = { ( , id }FIRST(Tâ€™) = { *, Ð„ }FIRST(F) = { ( , id }FOLLOW SetFOLLOW(E)  = { \$ , ) }  // Note  ')' is there because of 5th ruleFOLLOW(Eâ€™) = FOLLOW(E) = {  \$, ) }  // See 1st production ruleFOLLOW(T)  = { FIRST(Eâ€™) â€“ Ð„ } U FOLLOW(Eâ€™) U FOLLOW(E) = { + , \$ , ) }FOLLOW(Tâ€™) = FOLLOW(T) =      { + , \$ , ) }FOLLOW(F)  = { FIRST(Tâ€™) â€“  Ð„ } U FOLLOW(Tâ€™) U FOLLOW(T) = { *, +, \$, ) }`

Example 2:

`Production Rules:S -> aBDhB -> cCC -> bC | Ð„D -> EFE -> g | Ð„F -> f | Ð„FIRST setFIRST(S) = { a }FIRST(B) = { c }FIRST(C) = { b , Ð„ }FIRST(D) = FIRST(E) U FIRST(F) = { g, f, Ð„ }FIRST(E) = { g , Ð„ }FIRST(F) = { f , Ð„ }FOLLOW SetFOLLOW(S) = { \$ } FOLLOW(B) = { FIRST(D) â€“ Ð„ } U FIRST(h) = { g , f , h }FOLLOW(C) = FOLLOW(B) = { g , f , h }FOLLOW(D) = FIRST(h) = { h }FOLLOW(E) = { FIRST(F) â€“ Ð„ } U FOLLOW(D) = { f , h }FOLLOW(F) = FOLLOW(D) = { h } `

Example 3:

`Production Rules:S -> ACB|Cbb|BaA -> da|BCB-> g|Ð„C-> h| Ð„FIRST setFIRST(S) = FIRST(A) U FIRST(B) U FIRST(C) = { d, g, h, Ð„, b, a}FIRST(A) = { d } U {FIRST(B)-Ð„} U FIRST(C) = { d, g, h, Ð„ }FIRST(B) = { g, Ð„ }FIRST(C) = { h, Ð„ }FOLLOW SetFOLLOW(S) = { \$ }FOLLOW(A)  = { h, g, \$ }FOLLOW(B) = { a, \$, h, g }FOLLOW(C) = { b, g, \$, h }`

Note:

1. Ð„ as a FOLLOW doesnâ€™t mean anything (Ð„ is an empty string).
2. \$ is called end-marker, which represents the end of the input string, hence used while parsing to indicate that the input string has been completely processed.
3. The grammar used above is Context-Free Grammar (CFG). The syntax of a programming language can be specified using CFG.
4. CFG is of the form A -> B, where A is a single Non-Terminal, and B can be a set of grammar symbols ( i.e. Terminals as well as Non-Terminals)

Previous
Next