The Wayback Machine - https://web.archive.org/web/20250307030121/https://www.geeksforgeeks.org/follow-set-in-syntax-analysis/
Open In App

FOLLOW Set in Syntax Analysis

Last Updated : 28 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

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}

Rules to compute FOLLOW set: 

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 5th rule
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:

  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)

Features of FOLLOW Set

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:

  1. Identifies Valid Successors: It contains all terminal symbols that can immediately follow a non-terminal in any derivation.
  2. End-of-Input Marker ($): For the start symbol of a grammar, the FOLLOW set always includes the end-of-input marker $, indicating the end of parsing.
  3. Assists in Parsing Table Construction: It is used in LL(1) parsing to decide which production rule to apply based on the next input symbol.
  4. Handles Nullable Productions: If a non-terminal can derive an empty string (nullable), the FOLLOW set of that non-terminal contributes to its parent non-terminal.
  5. Ensures Predictive Parsing: By using the FOLLOW set, parsers avoid ambiguity and ensure that only one rule is chosen for a given input.
  6. Closely Linked to FIRST Set: The FOLLOW set works with the FIRST set to resolve grammar rules and predict parsing steps effectively.

Frequently Asked Questions on FOLLOW Set in Syntax Analysis

Can epsilon be in follow set?

No, epsilon (Îĩ) cannot be in the FOLLOW set.

What is first () and follow ()?

The FIRST() set contains terminal symbols that can appear at the beginning of strings derived from a non-terminal. The FOLLOW() set, on the other hand, contains terminal symbols that can appear immediately after a non-terminal in a derivation. These sets help parsers decide which rules to apply during syntax analysis.

What is shift reduce conflict?

A shift-reduce conflict occurs in a parser when it is uncertain whether to apply a shift (move to the next input symbol) or a reduce (apply a production rule) at a given step. This conflict typically happens in LR parsing when the parser cannot decide whether to continue reading input or reduce a rule based on the current input symbol and the stack contents. It leads to ambiguity, making it difficult to choose the correct action.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg