VOOZH about

URL: https://www.geeksforgeeks.org/compiler-design/introduction-of-parsing-ambiguity-and-parsers-set-1/

⇱ Introduction to Parsers - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Parsers

Last Updated : 29 May, 2026

Parsing is also known as syntactic analysis, is the process of analyzing a sequence of tokens to determine their grammatical structure according to the rules of a language. It organizes tokens into a parse tree or syntax tree, which represents the structural relationship between different parts of the input.

👁 Parsing
Parsing
  • Converts tokens into a structured parse tree
  • Helps understand the grammatical structure of text or code
  • Ensures the input follows syntax rules
  • Used in compilers, interpreters, and NLP applications
  • Supports further tasks like semantic analysis and code execution

Role of Parser

A parser performs syntactic and semantic analysis of source code by converting tokens into a structured intermediate representation while identifying errors in the code.

  • Checks whether code follows grammar and syntax rules
  • Supports context-sensitive analysis such as type checking
  • Generates parse trees or Abstract Syntax Trees (ASTs)
  • Detects and reports syntax errors clearly
  • Attempts basic error recovery during parsing

Types of Parsing

The parsing is divided into two types

👁 parsers
Types of Parsers

1. Top-Down Parsing

Top-down parsing builds the parse tree from the start symbol toward the input symbols.

  • Starts from the start symbol
  • Expands non-terminals using production rules
  • Uses leftmost derivation
  • Also known as recursive descent or predictive parsing
  • Easier to implement for simple grammars

Types of Top-Down Parsing

With Backtracking

  • Tries multiple production rules
  • Backtracks if one rule fails
  • Flexible but slower

Without Backtracking

  • Does not retry other rules once a choice is made.
  • Faster and more efficient.
  • Works only for suitable grammars (e.g., LL grammars).

2. Bottom-Up Parsing

Bottom-up parsing builds the parse tree from input symbols toward the start symbol.

  • Starts from the input string
  • Reduces substrings into non-terminals
  • Uses rightmost derivation in reverse
  • Also known as Shift-Reduce Parsing
  • Efficient for complex grammars and compiler design

Types of Bottom-Up Parsing

Bottom-Up vs Top-Down Parser

Feature

Top-Down Parsing

Bottom-Up Parsing

Direction

Builds the parse tree from root to leaves

Builds the parse tree from leaves to root

Derivation

Uses leftmost derivation

Uses rightmost derivation in reverse

Working Approach

Starts from the start symbol

Starts from the input string

Efficiency

Simpler but can be slower with backtracking

More efficient for complex grammars

Complexity

Easier to implement

More complex to implement

Example Parsers

Recursive Descent Parser, LL Parser

Shift-Reduce Parser, LR Parser

Comment
Article Tags:
Article Tags:

Explore