Error Recovery in Predictive Parsing
Last Updated :
01 Nov, 2022
We know that the Predictive parser performs Left most derivative while parsing the given sentence. Now the given sentence may be a valid sentence or an invalid sentence with respect to the specified grammar. An error is detected during the predictive parsing when the terminal on top of the stack does not match the next input symbol, or when nonterminal X on top of the stack, then the present input symbol is a, and the parsing table entry M [X, a] is considered empty.
It is also possible that An error may occur while performing predictive parsing (LL(1) parsing)
When the terminal symbol is at top of the stack and it does not match the current input symbol. In LL(1) parsing If the top of the stack is a non-terminal A, then the present input symbol is a, and the parsing table entry M [A, a] is considered empty.
So what should a parser do to handle the error?
The parser design should be able to provide an error message (an error message which depicts as much possible information as it can ). It should be recovering from that error case, and it should be able to continue parsing with the rest of the input.
Error Recovery Techniques:
- Panic-Mode Error Recovery: In Panic-Mode Error Recovery the technique is skipping the input symbols until a synchronizing token is found.
- Phrase-Level Error Recovery: Each empty entry in the parsing table is filled with a pointer to a specific error routing take care of that error case.
Panic-Mode Error Recovery in LL(1) Parsing:
Panic-mode error recovery says that all the input symbols are skipped until a synchronizing token is found from the string.
In this recovery method, we use FOLLOW symbols as synchronizing tokens and the "synch" in the predictive parsing table to indicate synchronizing tokens obtained from the nonterminal's FOLLOW sets.
What is the synchronizing token?
The terminal symbols which lie in follow set of non-terminal they can be used as a synchronizing token set for that of non-terminal. In simple panic-mode error recovery for the LL(1)parsing.
All the empty entries are marked as synch to indicate that the parser will skip all the input symbols until a symbol in the following set of the non-terminal A which on the top of the stack. Then Non-terminal A will be popped from the stack by the parser. The parsing continues from that state. For handling the unmatched terminal symbols, the unmatched terminal symbol from the stack by the parser, after which it generates an error message that depicts the unmatched terminal is inserted.
How to select synchronizing set?
There are two ways of selecting synchronizing set:
1. Place all symbols in FOLLOW(A) into the synchronizing set for nonterminal A. We can continue parsing until we skip tokens of an element of FOLLOW(A) is seen and pop it from the stack specifically.
We might add keywords that begin statements to the synchronizing sets for the non-terminals generating expressions.
2. If a nonterminal can generate the empty string, then the production deriving ε can be used as a default. This may produce some delay in error detection, but errors cannot be missed. This approach reduces the number of non-terminals that have to be considered during error recovery.
Whenever we face a case where we find that a terminal that lies on top of a stack is not matching, we can simply pop that specifically and generate a message saying that the terminal was inserted.
Example for Panic Mode Error Recovery:
There are two separate examples explained below by taking two different strings.
1. aab$
2. ceadb$
Panic Mode Recovery ExamplePhrase-level recovery in LL(1) Parsing:
Phrase level recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. At each unfilled entry in the parsing table, it is filled by a pointer to a special error routine that will take care of that error case specifically. These error routines can be of different types like :
- change, insert, or delete input symbols.
- issue appropriate error messages.
- pop items from the stack.
Similar Reads
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ASCII Values Alphabets ( A-Z, a-z & Special Character Table ) ASCII (American Standard Code for Information Interchange) is a standard character encoding used in telecommunication. The ASCII pronounced 'ask-ee', is strictly a seven-bit code based on the English alphabet. ASCII codes are used to represent alphanumeric data. The code was first published as a sta
7 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
What is Vacuum Circuit Breaker? A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read
Python Variables In Python, variables are used to store data that can be referenced and manipulated during program execution. A variable is essentially a name that is assigned to a value. Unlike many other programming languages, Python variables do not require explicit declaration of type. The type of the variable i
6 min read