COMPILER DESIGN/Student Engineering Notebook
Page 03·notes
TOP-DOWN SYNTAX ANALYSIS & LL(1) PARSING—~32 MIN READ

FIRST & FOLLOW Computation, Left Recursion & Factoring

“Recursive set rules, eliminating immediate and indirect left recursion, and left factoring transformations.”

1.The Grammar Mechanics of Top-Down Parsing

Top-down syntax analysis attempts to find a leftmost derivation for an input string w starting from the start symbol S.

The Curse of Left Recursion: A grammar is left recursive if there exists a non-terminal A such that A \Rightarrow^+ A\alpha for some string \alpha. A recursive-descent parser attempting to expand A will repeatedly invoke the subroutine for A before consuming any input token, resulting in infinite call stack overflow.

Left Factoring: When two productions for A share a common prefix, e.g., A \to \alpha\beta_1 \mid \alpha\beta_2, a predictive parser seeing lookahead terminal in FIRST(\alpha) cannot determine which production to select without backtracking. Left factoring defers the decision until \alpha has been consumed.

FIRST and FOLLOW Mathematical Formulations:

- FIRST(\alpha) = { a \in \Sigma \mid \alpha \Rightarrow^ a\beta } \cup { \epsilon if \alpha \Rightarrow^ \epsilon }.

- FOLLOW(A) = { a \in \Sigma \cup {\} \mid S \Rightarrow^* \alpha A a \beta }$.

WORKED EXAM ANALYSIS · STEP-BY-STEP DERIVATION

2. Worked Analysis: Comprehensive FIRST & FOLLOW Derivation

⚠️ Pitfall Warning:

Notice that E' and T' are nullable non-terminals resulting from left-recursion elimination.

Consider the standard arithmetic expression grammar with left recursion eliminated:

E -> T E'

E' -> + T E' | epsilon

T -> F T'

T' -> * F T' | epsilon

F -> ( E ) | id

Step 1: Compute FIRST Sets (bottom-up from terminals):

- FIRST(F) = {(, id}

- FIRST(T') = {*, epsilon}

- FIRST(T): T -> F T', so FIRST(T) = FIRST(F) = {(, id}

- FIRST(E') = {+, epsilon}

- FIRST(E): E -> T E', so FIRST(E) = FIRST(T) = {(, id}

Step 2: Compute FOLLOW Sets:

- Start Symbol: FOLLOW(E) contains {}. In F -> ( E ), ')' follows E. Thus, FOLLOW(E) = {, )}.

- In E -> T E': E' is at the right end, so FOLLOW(E') = FOLLOW(E) = {$, )}.

- In E -> T E': symbol following T is E'. Thus, add (FIRST(E') - {epsilon}) = {+} to FOLLOW(T).

Since E' =>* epsilon, add FOLLOW(E) = {$, )} to FOLLOW(T).

Therefore: FOLLOW(T) = {+, $, )}.

- In T -> F T': T' is at the right end, so FOLLOW(T') = FOLLOW(T) = {+, $, )}.

- In T -> F T': symbol following F is T'. Thus, add (FIRST(T') - {epsilon}) = {*} to FOLLOW(F).

Since T' =>* epsilon, add FOLLOW(T) = {+, $, )} to FOLLOW(F).

Therefore: FOLLOW(F) = {*, +, $, )}.

Final Sets:

FIRST(E) = FIRST(T) = FIRST(F) = {(, id}

FIRST(E') = {+, epsilon}, FIRST(T') = {*, epsilon}

FOLLOW(E) = FOLLOW(E') = {$, )}

FOLLOW(T) = FOLLOW(T') = {+, $, )}

FOLLOW(F) = {*, +, $, )}.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: FOLLOW sets never contain ε. Add FOLLOW(A) to FOLLOW(B) whenever B is at the end of an A rule, or followed by something that can vanish.

Worked example: FIRST and FOLLOW

Problem Statement:
For E → T E', E' → + T E' | ε, T → F T', T' → * F T' | ε, F → ( E ) | id, find FIRST(E), FOLLOW(E), FOLLOW(T) and FOLLOW(F).
Step 1: FIRST(F)

F begins with ( or id.

↳ Result: { (, id }
Step 2: FIRST(T) and FIRST(E)

T starts with F, and E starts with T.

↳ Result: { (, id }
Step 3: FOLLOW(E)

E is the start symbol, so $; and E appears inside ( E ).

↳ Result: { ), $ }
Step 4: FOLLOW(T)

FIRST(E') minus ε, which is +; and since E' can vanish, everything in FOLLOW(E).

↳ Result: { +, ), $ }
Step 5: FOLLOW(F)

FIRST(T') minus ε, which is *; and since T' can vanish, FOLLOW(T).

↳ Result: { *, +, ), $ }
Final Answer:FIRST(E) = { (, id }; FOLLOW(E) = { ), }; FOLLOW(T) = { +, ), }; FOLLOW(F) = { *, +, ), $ }
STUDENT PRACTICE · CONCEPT DRILL

Practice: test yourself

Original practice questions in the GATE style, with worked explanations. Not past-paper questions.

Problem 1 · FOUNDATION

In the grammar above, what is FIRST(E')?

Problem 2 · GATE-LEVEL

In the same grammar, what is FOLLOW(E')?

Problem 3 · GATE-LEVEL

A grammar with left recursion, such as E → E + T | T, is:

SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Curated University Lecture Series
Compilers: Principles, Techniques, and Tools ·
LECTURE SERIES
RECOMMENDED YOUTUBE EDUCATORS & FULL-COURSE PLAYLISTS

Curated for clarity, rigorous numerical solving, and semester excellence:

Gate Smashers · Compiler Design↗Instructor: Varun Singla · Covers: Lexical analysis, parsing (LL/LR), SDT, code generation

GATE-focused with worked FIRST/FOLLOW and LR parsing table numericals.

Open YouTube↗
Knowledge Gate · Compiler Design for GATE↗Instructor: Sanchit Jain · Covers: Parsing conflicts, operator precedence, syntax-directed translation

Exam-oriented explanations of ambiguous grammars and parser conflicts.

Open YouTube↗
Stanford · Compilers (Coursera)↗Instructor: Prof. Alex Aiken · Covers: Lexing, parsing, semantic analysis, optimization, code generation

University-depth treatment of the full compiler pipeline — build a real compiler.

Open YouTube↗