RTUComputer ScienceYr 2023 · Sem 52023

Q2Compiler Design

Question

10 marks

Explain the concept of Top-Down Parsing in detail. Discuss Recursive Descent Parsing and LL(1) parsing with suitable examples.

Answer

A deep mathematical breakdown of Top-Down Parsing architectures, explicitly differentiating the backtracking inefficiency of Recursive Descent Parsing from the highly deterministic, table-driven execution of LL(1) Predictive Parsing.

Top-Down Parsing is a fundamental grammatical strategy utilized during the Syntax Analysis phase of compilation. Architecturally, it attempts to mathematically construct a Parse Tree starting from the absolute Root (the Start Symbol of the Context-Free Grammar) and continuously expanding downwards toward the Leaf nodes (the terminal tokens generated by the scanner). To successfully parse a string, the parser must rigorously search for a sequence of production rules that derive the exact input string, reading the input strictly from left to right. Top-Down parsers violently reject grammars containing "Left Recursion" (which triggers catastrophic infinite loops) and "Left Factoring" (which causes deterministic confusion).

Recursive Descent Parsing (The Brute Force Approach)

Recursive Descent Parsing is the most primitive form of Top-Down parsing. It is implemented as a set of highly intertwined, recursive functions, where exactly one function is mathematically constructed for every single Non-Terminal in the grammar.

  • Execution Mechanism: When a function representing Non-Terminal is executed, it attempts to match the current input token against the Right-Hand Side (RHS) of 's production rules.
  • The Backtracking Nightmare: If has multiple production rules (e.g., ), a naive Recursive Descent parser will blindly pick the first rule. It will aggressively consume the input token a and call function B. If function B suddenly mathematically fails to match the subsequent tokens, the parser realizes it made a catastrophic error. It must violently "Backtrack": un-consume the token, destroy the partially built tree, and attempt the second rule .
  • Architectural Demerit: This relentless backtracking causes massive exponential time complexity, making standard Recursive Descent completely unviable for commercial compilers.

LL(1) Predictive Parsing (The Deterministic Solution)

To eradicate the catastrophic inefficiency of backtracking, compiler engineers developed LL(1) Predictive Parsing. It is a highly advanced, table-driven Top-Down architecture. - The First L: Scans input Left-to-right. - The Second L: Produces a Leftmost derivation. - The (1): Utilizes exactly 1 token of mathematical lookahead.

Instead of blindly guessing which production rule to use, an LL(1) parser looks exactly one token ahead in the input stream and consults a mathematically rigid 2D Parsing Table (the M-Table). Because the M-Table is constructed using heavily calculated FIRST and FOLLOW sets, the parser knows with absolute, deterministic mathematical certainty exactly which rule to fire.

LL(1) Execution Example

Consider the grammar: 1. 2.

We want to parse the string: a c b.

  • Initialization: The parser utilizes a Stack, initially containing the start symbol $ and S. The input buffer contains a c b $.
  • Step 1: The stack top is S. The lookahead token is a. The parser queries the M-Table at . The table rigorously dictates the rule . The parser pops S and pushes b, A, a onto the stack (in reverse).
  • Step 2: The stack top is now a. The lookahead is a. This is a perfect physical terminal match. The parser pops a from the stack and advances the input pointer to c.
  • Step 3: The stack top is now A. The lookahead is c. The parser queries . Because FIRST(A) includes c derived from the rule , the table commands the rule . The parser pops A and pushes c.
  • Step 4: The stack top is c, lookahead is c. Match. Pop and advance input to b.
  • Step 5: The stack top is b, lookahead is b. Match. Pop and advance input to $.
  • Step 6: The stack is empty (only $), and the input is empty (only $). The parser halts and violently accepts the string as grammatically perfect.

Notice that at no point did the LL(1) parser ever have to guess or backtrack. Its execution is a flawless, linear mathematical progression.

Back to Paper