Open In App
Related Articles

Parsing | Set 1 (Introduction, Ambiguity and Parsers)

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Parsing is performed at the syntax analysis phase where a stream of tokens is taken as input from the lexical analyzer and the parser produces the parser tree for the tokens while checking the stream of tokens against the syntax errors.

Role of Parser

In the syntax analysis phase, a compiler verifies whether or not the tokens generated by the lexical analyzer are grouped according to the syntactic rules of the language. This is done by a parser. The parser obtains a string of tokens from the lexical analyzer and verifies that the string can be the grammar for the source language. It detects and reports any syntax errors and produces a parse tree from which intermediate code can be generated.

Parser

 


Types of Parsing

The parsing is divided into two types, which are as follows:

1. Top-down Parsing

2. Bottom-up Parsing

Top-Down Parsing

Top-down parsing attempts to build the parse tree from the root node to the leaf node. The top-down parser will start from the start symbol and proceed to the string. It follows the leftmost derivation. In leftmost derivation, the leftmost non-terminal in each sentential is always chosen. 

  1. Recursive parsing or predictive parsing are other names for top-down parsing.
  2. A parse tree is built for an input string using bottom-up parsing.
  3. When parsing is done top-down, the input symbol is first transformed into the start symbol.

The top-down parsing is further categorized as follows:

1. With Backtracking: 

  • Brute Force Technique

2. Without Backtracking: 

  • Recursive Descent Parsing
  • Predictive Parsing or Non-Recursive Parsing or LL(1) Parsing or Table Driver Parsing

Bottom-up Parsing

Bottom-up parsing builds the parse tree from the leaf node to the root node. The bottom-up parsing will reduce the input string to the start symbol. It traces the rightmost derivation of the string in reverse. Bottom-up parsers are also known as shift-reduce parsers.

  1. Shift-reduce parsing is another name for bottom-up parsing.
  2. A parse tree is built for an input string using bottom-up parsing.
  3. When parsing from the bottom up, the process begins with the input symbol and builds the parse tree up to the start symbol by reversing the rightmost string derivations.

Generally, bottom-up parsing is categorized into the following types:

1. LR parsing/Shift Reduce Parsing: Shift reduce Parsing is a process of parsing a string to obtain the start symbol of the grammar.  

2. Operator Precedence Parsing: The grammar defined using operator grammar is known as operator precedence parsing. In operator precedence parsing there should be no null production and two non-terminals should not be adjacent to each other.

FAQs

Q1: What is Parsing?

Answer:

Parsing, syntax analysis, or syntactic analysis is the process of examining a string of symbols that adhere to the rules of a formal grammar, whether they be in natural language, computer languages, or data structures.

Q2: What is the need for Top-down parsing?

Answer:

Top-down parsing can be thought of as an effort to locate the input stream’s left-most derivations by looking for parse trees using a top-down expansion of the provided formal grammar rules. By encompassing all possible right-hand sides of grammar rules, inclusive choice is used to deal with ambiguity.


Last Updated : 20 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads