In this article, we are going to cover working of top down parser and will see how we can take input and parse it and also cover some basics of top down.
Pre-requisite – Parsing
Top Down Parser :
- In top down technique parse tree constructs from top and input will read from left to right. In top down, In top down parser, It will start symbol from proceed to string.
- It follows left most derivation.
- In top down parser, difficulty with top down parser is if variable contain more than one possibility selecting 1 is difficult.
Working of Top Down Parser :
Let’s consider an example where grammar is given and you need to construct a parse tree by using top down parser technique.
S -> aABe A -> Abc | b B -> d
Now, let’s consider the input to read and to construct a parse tree with top down approach.
Now, you will see that how top down approach works. Here, you will see how you can generate a input string from the grammar for top down approach.
- First, you can start with S -> a A B e and then you will see input string a in the beginning and e in the end.
- Now, you need to generate
- Expand A-> Abc and Exapnd B-> d.
- Now, You have string like aAbcde and your input string is abbcde.
- Expand A->b.
- Final string, you will get abbcde.
Given below is the Diagram explanation for constructing top down parse tree. You can see clearly in the diagram how you can generate the input string using grammar with top down approach.