A parser is an important component of a compiler that receives a sequence of tokens from the lexical analysis phase and analyzes their syntactic structure according to the grammar of the language. It checks whether the token sequence follows the rules of the language and typically produces a parse tree or another structured representation.
Parsing techniques are mainly divided into top-down parsing and bottom-up parsing.
In this chapter, you will learn what a parser is, how it processes tokens, the main types of parsing, and the basic concept of top-down parsing.
A parser is a component of a compiler that takes a sequence of tokens generated by the lexical analyzer as input and determines whether the tokens follow the grammar of the language.
The parser generally produces a parse tree that represents the syntactic structure of the input.
The basic process can be represented as:
Source Code
↓
Lexical Analysis
↓
Tokens
↓
Parser
↓
Parse Tree
Parsing techniques are mainly classified into two types:
Top-down parsing starts with the start symbol of the grammar and attempts to derive the input string. Bottom-up parsing starts with the input symbols and works toward the start symbol.
Top-down parsing constructs the parse tree starting from the start symbol and proceeds toward the input symbols.
In this approach, the parser begins with the start symbol of the grammar and repeatedly expands non-terminal symbols until the generated symbols match the input tokens.
Top-down parsing is also commonly associated with recursive-descent parsing and predictive parsing techniques.
The basic process can be represented as:
Start Symbol
↓
Non-Terminals
↓
Input Symbols
The important characteristics of top-down parsing include:
Top-down parsing has some limitations:
For an input string such as:
acdb
the parser uses the grammar rules to construct its corresponding parse tree.

Parse Tree representation of input string "acdb" is as follows:

In this, the parser examines different possibilities when faced with an option. If one possibility does not work then the parser goes back to the previous decision point and tries another possibility.
In this parser, using the first choice to find a match to the input string, if it does not match it fails immediately instead of going back to try another option. Therefore, the parser does not backtrack.
Following is the list of merits of top down parsing.
Following is the list of demerits of top down parsing.
Production
Parse Tree representation of input string "id * id" is as follows:


Bottom up parsing is classified in to various parsing. These are as follows:
The difference between the bottom up and top down parser is as follows.
| Basis | Top down Parse | Bottom up Parse |
|---|---|---|
| 1. Direction | It constructs tree from root to leaves. | It constructs tree from leaves to root. |
| 2. Derivation | It uses leftmost derivation. | It uses rightmost derivation in reverse. |
| 3. Efficiency | It can be slow mostly with backtracking. | It is more efficient for complex grammars. |
| 4. Examples | The various examples of Top down parser are:
| The various examples of Bottom up parser are:
|
Following is the list of various functions performed by parser:
We request you to subscribe our newsletter for upcoming updates.