Skip to content
Related Articles

Related Articles

Error Recovery in LR Parsing

Improve Article
Save Article
  • Last Updated : 08 Mar, 2022
Improve Article
Save Article

LR parser is a bottom-up parser. It is always used to deal with context-free Grammar (CFGs). It is generally used by computer programming language compilers as well as other associated tools also. LR parser produces leftmost derivation while using the input taking from left to right. By building up from the leaves, it helps to reduce the top-level grammar productions that is why it is called a bottom-up parser. LR parsers are the most powerful parsers among all deterministic parsers which are used widely nowadays. LR parsers are basically of 4 types:

  1. LR(0) parser
  2. SLR parser
  3. LALR parser
  4. CLR parser

Error Recovery in LR Parsing:

When there is no valid continuation for the input scanned thus far, LR parsers report an error. Before notifying a mistake, a CLR parser never performs a single reduction and an SLR or LALR may do multiple reductions, but they will never move an incorrect input symbol into the stack.

When the parser checks the table and discovers that the relevant action item is empty, an error is recognized in LR parsing. Goto entries can never be used to detect errors.

LR Parser Basically Uses the Mentioned Two Techniques to Detect Errors:

  1. Syntactic Phase recovery
  2. Panic mode recovery

Syntactic Phase Recovery:

Syntactic Phase Recovery Follows the Given Steps:

  1. Programmer mistakes that call error procedures in the parser table are determined based on the language.
  2. Creating error procedures that can alter the top of the stack and/or certain symbols on input in a way that is acceptable for table error entries.

There are some of the errors that are detected during the syntactic phase recovery:

  1. Errors in structure
  2. Missing operator
  3. Misspelled keywords
  4. Unbalanced parenthesis

Panic Mode Recovery:

This approach involves removing consecutive characters from the input one by one until a set of synchronized tokens is obtained. Delimiters such as or are synchronizing tokens. The benefit is that it is simple to implement and ensures that you do not end up in an infinite loop. The drawback is that a significant quantity of data is skipped without being checked for additional problems.

Panic mode recovery follows the given steps:

  1. Scan the stack until you find a state ‘a’ with a goto() on a certain non-terminal ‘B’ (by removing states from the stack).
  2. Until a symbol ‘b’ that can follow ‘B’ is identified, zero or more input symbols are rejected.

Example of Error recovery using LR Parser:

Consider the following grammar for detecting the errors:

E → E + E 
E → E * E 
E → ( E ) 
E → id

Step 1: Firstly make the parsing table for the given grammar:

Parsing Table

Parsing Table for the above example

Step 2: Let the string given to parse through it is:

String: id+)$

Step 3: Now working of parser on the given string is described below:

           STACK                      INPUT        
0                      id+)$
0id3                         +)$
0E1                         +)$
0E1 + 4                            )$
0E1 + 4                             $
0E1 + 4id3                             $
0E1 + 4E7                             $
0E1                             $

Step 4: Output is given in the form of error is detected:

Action(4, )) = error i.e.,
“unbalanced right parenthesis” c2
removes right parenthesis “missing
operand” e2 pushes id3 on stack

Thus, the error is detected using LR parser in this way.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!