Working of top down parser

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.

Example –



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.

Input –

abbcde$

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 abbcde .
  • 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.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.