What is Top-Down Parsing?

Compiler DesignProgramming LanguagesComputer Programming

In top-down parsing, the parse tree is generated from top to bottom, i.e., from root to leaves & expand till all leaves are generated.

It generates the parse tree containing root as the starting symbol of the Grammar. It starts derivation from the start symbol of Grammar & performs leftmost derivation at each step.

Drawback of Top-Down Parsing

  • Top-down parsing tries to identify the left-most derivation for an input string ω which is similar to generating a parse tree for the input string ω that starts from the root and produce the nodes in a pre-defined order.

  • The reason that top-down parsing follow the left-most derivation for an input string ω and not the right-most derivation is that the input string ω is scanned by the parser from left to right, one symbol/token at a time. The left-most derivation generates the leaves of the parse tree in the left to right order, which connect the input scan order.

  • In the top-down parsing, each terminal symbol produces by multiple production of the grammar (which is predicted) is connected with the input string symbol pointed by the string marker. If the match is successful, the parser can sustain. If the mismatch occurs, then predictions have gone wrong.

  • At this phase it is essential to reject previous predictions. The prediction which led to the mismatching terminal symbol is rejected and the string marker (pointer) is reset to its previous position when the rejected production was made. This is known as backtracking.

  • Backtracking was the major drawback of top-down parsing.

Types of Top-Down Parsing

There are two types of top-down parsing which are as follows −

  • Top-Down Parsing with Backtracking

In Backtracking, the parser can make repeated scans of input. If the required input string is not achieved by applying one production rule, then another production rule can be applied at each step to get the required string.

  • Top-Down Parsing without Backtracking

Once, the production rule is applied, it cannot be undone.

Predictive Parser − Predictive Parser is also known as Non-Recursive Predictive Parsing. A predictive parser is an effective approach of implementing recursivedescent parsing by handling the stack of activation records explicitly. The predictive parser has an input, a stack, a parsing table, and an output. The input includes the string to be parsed, followed by $, the right-end marker.

Recursive Descent Parser − A top-down parser that implements a set of recursive procedures to process the input without backtracking is known as recursive-descent parser, and parsing is known as recursive-descent parsing.

Published on 29-Oct-2021 06:59:41