![]() |
VOOZH | about |
The FOLLOW set in Syntax Analysis is a group of symbols that can come right after a non-terminal in a grammar. It helps parsers figure out what should appear next in the input while checking if the grammar is correct. The FOLLOW set is important for building parsing tables, especially in LL(1) parsers, which use it to decide how to process rules during syntax checking.
For example, if you're analyzing a sentence in a programming language, the FOLLOW set tells the parser what valid symbols can follow a specific rule or variable. It also includes the end-of-input symbol ($) to show when the input string ends. This makes the FOLLOW set an essential tool in creating reliable parsers for programming languages.
Example:
S ->Aa | Ac
A ->b
S S
/ \ / \
A a A c
| |
b b
Here, FOLLOW (A) = {a, c}
1) FOLLOW(S) = { $ } // where S is the starting Non-Terminal
2) 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) | id
FIRST set
FIRST(E) = FIRST(T) = { ( , id }
FIRST(Eβ) = { +, Π }
FIRST(T) = FIRST(F) = { ( , id }
FIRST(Tβ) = { *, Π }
FIRST(F) = { ( , id }
FOLLOW Set
FOLLOW(E) = { $ , ) } // Note ')' is there because of rule 3 (the propagation of FOLLOW(E) through the non-terminal Eβ)
FOLLOW(Eβ) = FOLLOW(E) = { $, ) } // See 1st production rule
FOLLOW(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 -> aBDh
B -> cC
C -> bC | Π
D -> EF
E -> g | Π
F -> f | Π
FIRST set
FIRST(S) = { a }
FIRST(B) = { c }
FIRST(C) = { b , Π }
FIRST(D) = FIRST(E) U FIRST(F) = { g, f, Π }
FIRST(E) = { g , Π }
FIRST(F) = { f , Π }
FOLLOW Set
FOLLOW(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|Ba
A -> da|BC
B-> g|Π
C-> h| Π
FIRST set
FIRST(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 Set
FOLLOW(S) = { $ }
FOLLOW(A) = { h, g, $ }
FOLLOW(B) = { a, $, h, g }
FOLLOW(C) = { b, g, $, h }
Note:
The FOLLOW set in syntax analysis has several important features that make it essential for parsing algorithms, especially in predictive parsers like LL(1). Here are its key features:
$): For the start symbol of a grammar, the FOLLOW set always includes the end-of-input marker $, indicating the end of parsing.