In this video, we will be covering what is Non-deterministic grammar, left factoring & left recursion in detail.
Let us first see what is non-deterministic grammar.
Grammar having common prefixes is called non-deterministic grammar. Such grammar requires backtracking due to confusion, which is actually a costly process. After tracing the first input “A”, there are two available options. So, the confusion is to go with b1, b2, or b3. Due to this confusion, Top-Down Parsers don’t prefer to parse Non-Deterministic Grammar.
The process of removing this confusion is called eliminating non-determinism or Left Factoring. Most of the parsers avoid parsing grammar with non-deterministic behaviour, so we must remove non-determinism from the grammar before passing the grammar to parser.
It is a procedure of factoring out common prefixes as long as possible. It is generally used in case when it’s not clear which of two available options should be used to expand LHS non-terminal. This grammar that is obtained after left-factoring is known as Left Factored Grammar. With left factored grammar compiler no longer needs backtracking. As backtracking is a costly procedure, so it can be avoided using left factored grammar. Left factoring can be applied to the grammar multiple times until the grammar becomes deterministic and unambiguous.
Any Grammar G (V, T, P, S) can be said as left-recursive grammar if it is having production of the form: A → A α | β, where V means variable, T means Terminal, P means production rules, and S means Starting Symbol.
It is called left-recursive because the LHS of the production is again occurring at first place on the RHS of the production. Such left recursion is said to be a problematic condition for the Top-down parsers.
So, it is very necessary to eliminate left recursion from grammar.
Introduction of parsing ambiguity & parsers: https://www.geeksforgeeks.org/introduction-of-parsing-ambiguity-and-parsers-set-1/
Removing direct & indirect left recursion in grammar: https://www.geeksforgeeks.org/removing-direct-and-indirect-left-recursion-in-a-grammar/