Open In App

Difference Between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser

Last Updated : 23 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Syntax analysis is an important task of compilers. Parsing techniques can be used for the analysis of the structure of programming languages. Out of these techniques, Recursive Predictive Descent Parsing and Non-Recursive Predictive Descent Parsing are two well-known top-down methods. Although both intend to analyze strings according to grammar rules and regulations, they employ entirely different strategies, especially in the utilization of backtracking and parsing tables. 

What is a Recursive Predictive Descent Parser?

Recursive Predictive Descent Parser is one of the top-down parsing techniques, which comprises a set of recursive procedures for the evaluation of input strings. Every operation corresponds to a non-terminal within the grammar, and it is the job of an operation to expand on this non-terminal. The parser tries to find the best match according to the syntax rules, and if this match fails, there is backing into other possible productions. This approach is also famous for getting a parse tree defined by some input since it recurs in nature. 

Key Characteristics

  • Backtracking: In case of the failure of one derivation, the parser resets it to another derivation. 
  • Procedural Implementation: Generally, a recursive function is determined for each non-terminal in its grammar. 
  • Parse Tree Generation: The way that a parser builds procedure calls is that a parse tree is in effect self-defined as the sequence of the procedure calls. 

What is Backtracking?

It means, that if one derivation of production fails, the syntax analyzer restarts the process using different rules of the same production. This technique may process input string more than once to determine right production. Top- down parser start from root node (start symbol) and match input string against production rules to replace them (if matched). 

Example of CFG

S -> aAb | aBb
A -> cx | dx
B -> xe

For an input string – read, a top-down parser, will behave like this. 

It will start with S from production rules and will match its yield to left-most letter of input, i.e. ‘a’. The very production of S (S -> aAb) matches with it. So top-down parser advances to next input letter (i.e., ‘d’). The parser tries to expand non-terminal ‘A’ and checks its production from left (A -> cx). It does not match with next input symbol. So top-down parser backtracks to obtain next production rule of A, (A -> dx). 

Now parser matches all input letters in an ordered manner. The string is accepted. 

Recursive Predictive Descent Parser

What is Non-Recursive Predictive Descent Parser?

A non-recursive predictive descent Parser, commonly referred to as the predictive parser, does not require backtracking, thanks to a parsing table. This parser employs the use of a ‘look-ahead’ pointer to decide which among the various production rules to implement depending on an input symbol that is to follow. To make sure that no backtracking is needed, this method puts some restrictions on the grammar, which is of a class called LL(k) grammar. 

 Key Characteristics

  • No Backtracking: This principle is regularly used in the construction of the predictive parser in opposition with the backtracking with the help of a parsing table and look-ahead symbols. 
  • LL(k) Grammar: It only recognizes a limited subset of grammars known as the LL(k) grammars so as to achieve deterministic parse. 
  • Parsing Table: A table is formed so as to assist the parser in determining which production is the best to apply in relation to the current input and the element on top of the stack.

Non-Recursive Predictive Descent Parser

Predictive parsing uses a stack and a parsing table to parse input and generate a parse tree. Both stack and input contains an end symbol $ to denote that stack is empty and input is consumed. The parser refers to parsing table to take any decision on input and stack element combination. There might be instances where there is no production matching input string, making parsing procedure to fail. 

Difference between Recursive Predictive Descent Parser and Non-Recursive Predictive Descent Parser

Recursive Predictive Descent Parser

Non-Recursive Predictive Descent Parser

It is a technique which may or may not require backtracking process. It is a technique that does not require any kind of backtracking.
It uses procedures for every non-terminal entity to parse strings. It finds out productions to use by replacing input string.
It is a type of top-down parsing built from a set of mutually recursive procedures where each procedure implements one of non-terminal s of grammar. It is a type of top-down approach, which is also a type of recursive parsing that does not uses technique of backtracking.
It contains several small functions one for each non- terminals in grammar. The predictive parser uses a look ahead pointer which points to next input symbols to make it parser back tracking free, predictive parser puts some constraints on grammar.
It accepts all kinds of grammars. It accepts only a class of grammar known as LL(k) grammar.

Conclusion 

Recursive and Non-recursive predictive descent parsers are two very vital methods in syntactical analysis. The recursive variant is more general in that it can accept many grammars, but it may include backtracking, thus it is slower. On the other hand, the non-recursive predictive parser that is based on a parsing table is characterized by better efficiency. Since there is no need for a backtracking though, it is suitable only for LL(k) grammars. 



Next Article

Similar Reads