What are Parsing Techniques in Compiler Design?

Compiler DesignProgramming LanguagesComputer Programming

Parsing is known as Syntax Analysis. It contains arranging the tokens as source code into grammatical phases that are used by the compiler to synthesis output generally grammatical phases of the source code are defined by parse tree. There are various types of parsing techniques which are as follows −

  • Top-Down Parser

It generates the Parse Tree from root to leaves. In top-down parsing, the parsin begins from the start symbol and changes it into the input symbol.

An example of a Top-Down Parser is Predictive Parsers, Recursive Descent Parser.

Predictive Parser − Predictive Parser is also known as Non-Recursive Predictive Parsing. A predictive parser is an effective approach of implementing recursivedescent parsing by manipulating the stack of activation records explicitly. The predictive parser has an input, a stack, a parsing table, and an output. The input includes the string to be parsed, followed by $, the right-end marker.

Recursive Descent Parser − A top-down parser that executes a set of recursive procedures to process the input without backtracking is known as recursive-descent parser, and parsing is known as recursive-descent parsing.

  • Bottom-Up Parser

It generates the Parse Tree from leaves to root for a given input string. In Grammar, the input string will be reduced to the starting symbol.

Example of Bottom-Up Parser is Shift Reduce Parser, Operator Precedence Parser, and LR Parsers.

Shift Reduce Parser − Shift reduce parser is a type of bottom-up parser. It uses a stack to influence the grammar symbols. A parser goes on changing the input symbols onto the stack until a handle comes on the top of the stack. When a handle occurs on the top of the stack, it implements reduction.

Operator Precedence Parser − The shift-reduce parsers can be generated by hand for a small class of grammars. These grammars have the property that no production on the right side is ϵ or has two adjacent non-terminals. Grammar with the latter property is known as operator grammar.

LR Parsers − The LR Parser is a shift-reduce parser that creates use of deterministic finite automata, identifying the set of all viable prefixes by reading the stack from bottom to top. It decides what handle, if any, is available.

A viable prefix of a right sequential form is that prefix that includes a handle, but no symbol to the right of the handle. Thus, if a finite state machine that identifies viable prefixes of the right sentential form is generated, it can guide the handle selection in the shift-reduce parser.

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

  • Simple LR Parser (SLR) − It is very easy to implement but it fails to produce a table for some classes of grammars.

  • Canonical LR Parser (CLR) − It is the most powerful and works on large classes of grammars.

  • Look Ahead LR Parser (LALR) − It is intermediate in power between SLR and CLR.

raja
Updated on 29-Oct-2021 06:57:11

Advertisements