Prerequsite – Recursive Descent Parser
1. Recursive Predictive Descent Parser :
Recursive Descent Parser is a top-down method of syntax analysis in which a set of recursive procedures is used to process input. One procedure is associated with each non-terminal of a grammar. Here we consider a simple form of recursive descent parsing called Predictive Recursive Descent Parser, in which look-ahead symbol unambiguously determines flow of control through procedure body for each non-terminal. The sequence of procedure calls during analysis of an input string implicitly defines a parse tree for input and can be used to build an explicit parse tree, if desired. In recursive descent parsing, parser may have more than one production to choose from for a single instance of input there concept of backtracking comes into play.
It means, if one derivation of a production fails, syntax analyzer restarts process using different rules of 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).
To understand this, take following 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.
2. Non-Recursive Predictive Descent Parser :
A form of recursive-descent parsing that does not require any back-tracking is known as predictive parsing. It is also called as LL(1) parsing table technique since we would be building a table for string to be parsed. It has capability to predict which production is to be used to replace input string. To accomplish its tasks, predictive parser uses a look-ahead pointer, which points to next input symbols. To make parser back-tracking free, predictive parser puts some constraints on grammar and accepts only a class of grammar known as LL(k) grammar.
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 back tracking.|
|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 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.|
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.