Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator that aids in organising the linear text structure in accordance with the grammar. A grammar is a collection of established rules.

You can also say that the parser is the stage of the compiler that accepts a token string as input and transforms it into the matching Intermediate Representation (IR) using the available grammar. Syntax Analyzer is another name for the parser.

Types of Parsing in compiler Design

Parsing is broadly classified into two types:

1. Top-Down Parser

2. Bottom-Up Parser

Top-Down Parser

The top-down parser expands the non-terminals and starts from the start symbol and finishes on the terminals. This parser build a parse for the provided input text using grammatical productions. The leftmost derivation is used in this parsing technique.

Top-Down Parser is further classified into two types:

  • Recursive descent parser
  • Non-recursive descent parser

Recursive Descent Parser

It is a type of top-down parser which begins with the non-terminal and constructs the parse tree from top to down.

A Recursive Descent Parser that doesn't require Backtracking. That’s why it is known as a Predictive Parser. A correctly written grammar remove the left recursion and left factoring from it which can be read by a recursive descent parser.

Example:

Before removing left recursion   After removing left recursion
E –> E + T | T
T –> T * F | F
F –> ( E ) | id  
E –> T E’
E’ –> + T E’ | e
T –> F T’
T’ –> * F T’ | e
F –> ( E ) | id  

Note:

Here e is Epsilon

Example:

Grammar: E --> i E'
E' --> + i E' | e

Non-recursive descent parser

Predictive parsing is a type of recursive-descent parsing that doesn't involve any backtracking. Other names for it include LL(1) parser, predictive parser, parser without backtracking, and dynamic parser.

Instead of backtracking, it creates the parse tree using a parsing table.

It can anticipate which production will be used to replace an input string.

Predictive parser employs a look-ahead pointer, which links to the next input symbols, to complete its responsibilities. Predictive parser placed various restrictions on grammar and only accepts a LL(k) grammar which make a backtracking free parser.

It parses input and creates a parse tree using a stack and a parsing table. Input and stack both have an end symbol ($) that indicates the input has been used up and the stack is empty.

Bottom-up Parser

Bottom-up Parser is a parser that uses grammatical productions to construct the parse tree for the specified input string while compressing the non-terminals. It concludes that it begins with non-terminals and ends with the start symbol.

It employs the reverse of the rightmost derivation.

Shift-Reduce Parsing

For bottom-up parsing, shift-reduce parsing employs two distinct phases. Shift-step and reduce-step are the names of these steps.

Shift Step:

The next input symbol, known as the shifted symbol, is added by the input pointer during the shift step. The shifted symbol is placed on the stack. The parse tree treats the shifted symbol as a single node.

Reduce Step:

Reduce-step occurs when the parser detects a full grammar rule (RHS) and changes it to (LHS). When a handle is present at the top of the stack, then this parsing happens. A POP function on the stack is used to decrease, which pops off the handle and substitutes an LHS non-terminal symbol in its place.

It is further classified into two:

  • LR parser
  • Operator precedence parser

LR parser

A highly common bottom-up parser for context-free grammar used by computer programming language compilers and other related tools is called the LR parser.

It generates the rightmost derivation by reading their input from left to right.

It seeks to decrease the top-level grammar production by building up from the leaves, which is why it is known as a bottom-up parser.

There are three types of LR Parsers which are as follows:

  • Simple LR Parser (SLR) -This type of LR parser is simple to use and it cannot build a table for all kinds of grammar.
  • Canonical LR Parser (CLR) - It is the most effective and applies to a wide range of grammars.
  • Look Ahead LR Parser (LALR) -  It is intermediate in power between SLR and CLR.

Operator precedence parser

Operator Precedence Parsing is also a type of Bottom-Up Parsing that can be used to the class of Grammars known as Operator Grammar.

Basically, it produces the parse tree from the grammar and string that are provided. This parser requires only two consecutive non-terminals with the absence of epsilon on the right side of any production rule

Operator grammars can use the operator precedence parsing strategies.

Operator grammar can be exist if there isn't a production rule on the right side. A grammar is said to be operator grammar if the following two properties are satisfied:

1. Right side has no ε(Epsilon).

2. No two non-terminals appear sequentially, that is, there should be a terminal between two non-terminal.


Related Topics

Syntax-Directed Translation

Syntax-Directed Translation A context-free-grammar with some additional rules is known as a syntax-directed definition. In SDT, attributes are associated with grammar symbols and rules are associated with productions. The attributes can...

3 minutes read.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Run-Time Environments

Run-Time Environments Storage Organization Every target program has its own logical address, and an executable program runs in it. The logical address space has the location for each program value. The...

2 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes read.

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

3 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

3 minutes read.

Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for...

3 minutes read.

Shift Reduce Parsing in Compiler Design

Like the bottom-up parsing, the shift reduce parser also builds the parse tree from the leaves (bottom) to the root (up). The LR parser is a more versatile variation of...

4 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

3 minutes read.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

Syntax Analysis Compiler Design

Syntax Analysis This article will describe the parsing method used in the compiler. The grammatical rule of programming language can be constructed with the help of context-free grammars or BNF (Backus–Naur...

3 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.