What is the difference between Backtracking and Non- Backtracking?

Top-Down Parsing with Backtracking

In Top-Down Parsing with Backtracking, Parser will attempt multiple rules or production to discover the match for input string by backtracking at every step of derivation. So, if the used production does not give the input string as needed, or it does not match with the needed string, then it can undo that shift.

Top-Down Parsing without Backtracking

As backtracking looks more powerful by which we can select different alternatives. But backtracking cannot be applied or implemented so easily in parsing. There are two types of Top-Down Parsing without Backtracking, which are as follows −

  • Recursive Descent Parser
  • Predictive Parser

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. The recursive procedures can be simply to write and adequately effectively if written in a language that executes the procedure call effectively.

There is a procedure for each non-terminal in the grammar. It can consider a global variable lookahead, holding the current input token and a procedure match (Expected Token) is the action of recognizing the next token in the parsing process and advancing the input stream pointer, such that lookahead points to the next token to be parsed. Match () is effectively a call to the lexical analyzer to get the next token.

Predictive Parser

Predictive Parser is also known as Non-Recursive Predictive Parsing. A predictive parser is an effective approach of executing recursive-descent 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.

The stack includes a sequence of grammar symbols, preceded by $, the bottom-ofstack marker. Initially, the stack includes the start symbol of the grammar preceded by $. The parsing table is a two-dimensional array M[A, a] where ‘A’ is a nonterminal, and ‘a’ is a terminal or the symbol $.

Let us see the comparison between Top-Down Parsing with Backtracking and Top-Down Parsing without Backtracking

Top-Down Parsing with BacktrackingTop-Down Parsing without Backtracking
The parser can try all alternatives in any order till it successfully parses the string.The parser has to select correct alternatives at each step.
Backtracking takes a lot of time, i.e., exponential time to perform the parsing.It takes less time.
A Grammar can have left recursion.Left Recursion will be removed before doing parsing.
A lot of overhead will be there while undoing things.It can run less overhead.
Information, once inserted, will be removed from the symbol table while backtracking.Information will not be removed.
It can be difficult to find where actually error has occurred.It can easy to find the location of the error.