notesonly.in

One notebook for every subject — open it anywhere.

Log in

Syntax Analysis: Top-Down and Bottom-Up Parsing Guide.

Syntax analysis, or parsing, is the compiler phase that takes the token stream from the lexer and checks whether it follows the grammar of the language. It organizes tokens into a parse tree revealing the nested structure of the program — which expression belongs inside which statement.

Parsing matters because structure determines meaning: in 3 + 4 x 2, the parse tree decides whether the answer is 11 or 14. Every compiler, JSON reader, and SQL engine parses its input, and the two great parsing families — top-down and bottom-up — are a fundamental trade-off every language tool navigates.

Key Concepts

  • Context-free grammar: productions like "E becomes E + T" defining how language constructs are built from tokens.
  • Parse tree: root is the start symbol, interior nodes are grammar variables, leaves are tokens in order.
  • Top-down parsing: starts from the start symbol and expands productions downward to the tokens; includes recursive descent and LL parsers.
  • Bottom-up parsing: starts from the tokens and reduces them upward to the start symbol via shift-reduce actions; includes LR parsers.
  • Ambiguity: one token stream with two different parse trees — parsers need unambiguous grammars or explicit precedence rules.

How It Works

Top-down parsing predicts: given upcoming tokens, it chooses a production for the current grammar symbol and recursively parses each part — assembling from the big picture down. It cannot handle left recursion (rules like "E becomes E + T") and predictive LL parsers use one lookahead token.

Bottom-up parsing reduces: it shifts tokens onto a stack and, whenever the stack top matches a production's right side, reduces it to the left-side symbol — joining small parts first. LR parsers accept far more grammars, including left-recursive ones, which is why production compilers favor them.

Worked Example

Parse 3 + 4 x 2 with grammar: E becomes E + T or T; T becomes T x F or F; F becomes a number. Bottom-up shift-reduce:

  1. Shift 3, reduce to F, then to T: stack holds T(3). Actions: 3.
  2. Shift +, shift 4, reduce to F, then to T: stack is T(3), +, T(4). Actions: 7.
  3. Shift x, shift 2, reduce to F: stack is T(3), +, T(4), x, F(2). Actions: 10.
  4. Reduce T x F to T (4 x 2 = 8), T to E, E + T to E (3 + 8 = 11). Total actions: 13.

The tree forces multiplication before addition: the value is 11, not 14. The layered grammar (E for addition, T for multiplication) encodes precedence directly — no extra rules needed.

Common Misconceptions

  • Any grammar works with any parser. Left recursion sends naive top-down parsers into infinite recursion; ambiguity yields two trees — grammar and parser must match.
  • Parse trees equal abstract syntax trees. A parse tree keeps every grammar detail; an AST drops scaffolding like parentheses, keeping only meaningful operations.
  • Top-down is simpler, therefore better. Recursive descent is easy to hand-write, but bottom-up LR parsers accept far more grammars and power most real compilers.

Real-World Applications

  • Compilers: GCC and Clang use hand-written recursive descent; many tools use generated LALR parsers.
  • Data formats: every JSON and XML parser builds a tree from tokens with these techniques.
  • Databases: SQL engines parse queries into trees before optimizing them.
  • IDEs: completion, refactoring, and error recovery depend on incremental parsing.

Syntax analysis converts a flat token stream into a tree capturing the program's structure. Top-down parsers predict from the grammar downward; bottom-up parsers reduce from the tokens upward — two strategies for one goal: proving code well-formed and revealing how its pieces fit.