# Classification of Top Down Parser

In this article, we are going to learn about the **Classification of Top Down Parser: with backtracking, without backtracking**.

Submitted by Anusha Sharma, on March 25, 2018

**Top Down Parser** is classified into:

- With backtracking
- Without backtracking

### Bruteforce Technique

**This technique is under top-down parser with backtracking**. In this technique whenever a non terminal is expanding the first time and go with the first alternative and compare with the input string if does not matches go with the second alternative and compare with the input string if does, not matches go with the third alternative and so on and continue with all the alternatives if at least one alternative matches the input string then input string is parsed successfully otherwise the string cannot be parsed.

### LL(1) Parser or Table Driven Parser

LL(1) has 3 components

**1) Input buffer**

It is divided into cells and each cell is capable of holding only one symbol. The tape header always pointing any one symbol at a time. The symbol which is pointed by tape header is called Look ahead symbol.

**2) Parse stack**

It contains the grammar symbols. The grammar symbol is pushed into stack or popped from the stack based on the occurrence of the matching, i.e. if there is no matching between the topmost symbol of stack and look ahead symbol then grammar symbols are pushed into stack if matching occurs between the topmost symbol of stack and look ahead symbol then grammar symbols are popped from the stack.

**3) Parse Table**

It is a two dimensional array of order **m*n**, where **m=number** of non terminals and **n=number** of **terminals+1**. It contains all the productions which are used in the parsing to push the grammar symbols into stack.

## Parsing Process

**Step 1**- Push the start symbol into the stack.**Step 2**- Compare the topmost symbol of a stack with the lookahead symbol.**Step 3**- If matching occurs then tops of the grammar symbol and increment the input pointer and continues the process.**Step 4**- Output the production which is used for the expanding a nonterminal and continue.

### LL(1) Parsing Algorithm

Let x be the grammar symbol or start symbol in the stack and a is the look ahead symbol.

- If x=a=$ then it is successful in parsing.
- If x=a!=$ then pop off and increment the input pointer.
- If x!=a!=$ then m[x,a] has the production x->uvw thus replace by uvw in the reverse order and continue the process.
- Output the production which is used for expanding the non terminal.

### LL(1) Grammar

The grammar whose parse table does not contain multiple entries is called **LL(1) grammar**.

**FIRST** and **FOLLOW** are the function used for the construction of parse table.

### FIRST

First (alpha) is the set of all terminals that may begin in any sentential form which is derived from following rules:

- If alpha is a terminal then, first(alpha)={alpha}
- If alpha is non terminal and defined by the rule then first(alpha)={epsilon}
- If alpha is a non terminal and defined by non null production i.e. alpha=X1X2X3 then, first(alpha)=first(X1)
- first(alpha)=first(X1)Ufirst(X2)
- first(alpha)= first(X1)Ufirst(X2)Ufirst(X3)U{epsilon}

### FOLLOW

Follow(A) is the set of all terminals that may follow to the right of A in any sentential form of the grammar.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions