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.
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.
- Difference between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser
- Working of Bottom up parser
- Classification of Top Down Parsers
- Difference between Top down parsing and Bottom up parsing
- Shift Reduce Parser in Compiler
- Recursive Descent Parser
- Difference between LL and LR parser
- Parsing ambiguos grammars using LR parser
- Operator grammar and precedence parser in TOC
- Working of ISO-OSI Model
- Working of Compiler Phases with Example
- Working of Lexical Analyzer in compiler
- Top 5 Topics for Each Section of GATE CS Syllabus
- Top 5 PSUs for Computer Science Engineers in India
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.