Working of Bottom up parser (original) (raw)

Last Updated : 15 Jul, 2025

Parsing, also known as syntactic analysis, is the process of analyzing a sequence of tokens to determine the grammatical structure of a program. It takes the stream of tokens, which are generated by a lexical analyzer or tokenizer, and organizes them into a parse tree or syntax tree.

Bottom Up Parsing

As the name suggests, bottom-up parsing works in the opposite way of top-down parsing. While a top-down parser starts from the start symbol and moves down to the input string, a bottom-up parser starts from the input string (terminals) and works its way up to the start symbol.

It does this by looking for parts of the input that match the right-hand side of grammar rules, then replaces them with the left-hand side (the non-terminal), building the parse tree from the bottom to the top.

**Note :
In bottom-up parser, no variable that's why not have any derivation from the bottom but in reverse order it is looking like top-down, when you have rightmost derivation.

**Working of Bottom-up parser

**1. Start with tokens: The parser begins with the terminal symbols (the input tokens), which are the leaves of the parse tree.

**2. Shift and reduce: The parser repeatedly applies two actions:

**3. Repeat until root: The process of shifting and reducing continues until the entire input is reduced to the start symbol, indicating the sentence has been successfully parsed.

Let's consider an example where grammar is given and you need to construct a parse tree by using bottom-up parser technique.

**Example 1:

input string: “id * id”

bootom-up-1

STEPS INVOLVE IN PARSING

**Example 2:

Now, let’s parse the string “3 + 5”:

Now, we’ve reduced the entire input to the start symbol S, meaning the input has been successfully parsed.

**Classification of Bottom-up Parsers

Bottom-up-2

Classification Of Bottom up Parsers