![]() |
VOOZH | about |
Parsing is a process in which the syntax of a program is checked by a parser and a parse tree is constructed for the same. There are two main approaches to parsing in compilers: based on them, I have come across two parsing methods namely the top-down parsing and the bottom-up parsing. Both techniques are applied to a source code and they in a manner work in inversion to each other, In knowing the contrast between these two approaches, one can easily decide what parsing method should be used depending on the nature of the language to be compiled.
Top-down parsing on the other hand is a parsing technique whereby the parse tree is grown from the top down from the start symbol as the root moving down to the terminal symbols at the bottom. The parser attempts to translate the input string applying all the productions of the given grammar with the help of a start symbol and making iterations for the non-terminal symbols.
The bottom up parsing is a parsing strategy in which the parse tree is constructed in a bottom of the tree and gradually travel upwards to the root. A parser attempts to analyze the input string and put it into the from of the start symbol by applying reverse production rules.
| Top-Down Parsing | Bottom-Up Parsing |
|---|---|
| It is a parsing strategy that first looks at the highest level of the parse tree and works down the parse tree by using the rules of grammar. | It is a parsing strategy that first looks at the lowest level of the parse tree and works up the parse tree by using the rules of grammar. |
| Top-down parsing attempts to find the left most derivations for an input string. | Bottom-up parsing can be defined as an attempt to reduce the input string to the start symbol of a grammar. |
| In this parsing technique we start parsing from the top (start symbol of parse tree) to down (the leaf node of parse tree) in a top-down manner. | In this parsing technique we start parsing from the bottom (leaf node of the parse tree) to up (the start symbol of the parse tree) in a bottom-up manner. |
| This parsing technique uses Left Most Derivation. | This parsing technique uses Right Most Derivation. |
| The main leftmost decision is to select what production rule to use in order to construct the string. | The main decision is to select when to use a production rule to reduce the string to get the starting symbol. |
| Example: Recursive Descent parser. | Example: Shift Reduce parser. |
Top down as well as bottom up parsing play an essential role in syntax analysis in the field of compiler design. Top down parsing is one of the easiest and the simplest techniques that may be used in parsing but it has weaknesses when applied in complicated grammars and the left recursion. Conversely, bottom-up parsing generates licenses that are more powerful and can works with complicated grammar, but it is hard to implement. By following the above guidelines the most suitable parsing strategy is selected based on the specific need of the particular programming language, and the grammar used in the language.