What is Bottom-up Parsing?

Bottom-up parsing can be represented as an attempt to reduce the input string ω to the start symbol of a grammar by discovering out the right-most derivation of ω in reverse. This is similar to generating a parse tree for the input string ω by starting with leaves and proceeding toward the root i.e., attempting to generate the parse tree from the bottom-up.

This contains searching for the substring that connect the right side of any of the production of the grammar. This substring is restored by the left-hand side nonterminal of the production if this replacement leads to the generation of the sentential form that comes one step before the right-most derivation.

This process of replacing the right-side of the production by the left side nonterminal is known as reduction. Hence, the reduction is nothing more than performing derivations in reverse.

Types of Bottom-Up Parser

There are three types of the bottom-up parser which are as follows −

Shift Reduce Parser − Shift reduce parser is a type of bottom-up parser. It can require a stack to influence grammar symbols. A parser goes on shifting 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.