Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hello Ninjas, we know that compiler design is divided into many phases. These phases work in unison for the efficient design of the compiler. In all the phases, some errors occur and are reported to the user.
Error detection and handling is a tedious task. It has several methods that are implemented to handle errors efficiently. Panic mode recovery in compiler design is one such method.
In this article, we will learn about panic mode recovery in compiler design. We will also see some examples related to it.
The following diagram shows the different types of errors in the compiler design.
Panic mode recovery in compiler design is used to handle syntactic errors.
Syntactic errors include:
Misspelled Keyword
Missing Operator
Error in a defined structure
Unbalanced Parenthesis
Panic Mode Recovery In Compiler Design
It is the most simple and easy-to-implement error-handling method. Panic mode recovery is used to handle syntax errors by the parser. In panic mode recovery, as soon as the compiler discovers a syntax error, it enters panic mode. As a result, it starts to discard the input string until it finds a symbol from which it can resume its normal operation. The symbols that define the normal state for the compiler after entering panic mode are called synchronization symbols.
The basic idea behind panic mode recovery in compiler design is to discard until an optimal operation scenario occurs.
Working of Panic Mode Recovery
The following steps can help understand the working of panic mode recovery in compiler design:
The parser starts parsing the given input.
As soon as a syntax error occurs, it starts discarding the input symbols one at a time.
It continues to discard the input symbols until it encounters a synchronizing token.
The synchronizing symbols are a designated set of delimiters. The synchronization token indicates the end of an input statement. Examples of synchronization symbols are semicolons, braces, and other punctuation marks.
Once the synchronization token is found, the parser continues discarding the previous invalid tokens.
No synchronization token may be found. In that scenario, the parser continues to discard the token until a block, class, or even function ends.
To understand the above steps have a look at the following example.
Code in C
int coding_ninjas, 10coding_ninjas, code_studio, #CN;
Here the parsing begins with int, then coding_ninjas is parsed as a valid lexeme. As soon as one is encountered(in 10coding_ninjas), the elements are dropped until a synchronization token is met. Here ", "acts as a synchronization token.
Now, code_studio is parsed as a valid lexeme. Lastly, "#" generates a syntax error, and panic mode recovery starts. The elements are dropped until "; "is encountered. Here, "; "or semicolon acts as a synchronization token. Therefore we saw the panic mode recovery in the compiler design in preventing syntax errors.
Advantages Of Panic Mode Recovery
Panic mode recovery has the following advantages:
It is easy to implement, helping to remove syntax errors.
If there are fewer errors in the statement, it is the best choice.
It can never be hindered by falling into loop traps.
Panic Mode Recovery in LL(1) Parser
The Panic mode recovery used in a non-recursive parser uses the following steps.
We pop the terminal from the stack when the non-terminal tends to epsilon or when the LL(1) lookup table says POP.
Discard the terminal from the string when the LL(1) lookup table says SCAN.
Consider the following grammar.
E → TE’
E’ → +TE’ | -TE’ | e
T → FT’
T’ → *FT’ | /FT’ | e
F → GG’
G → %F | e
G → (E) | id
Consider e as epsilon here.
Now we find the first and follow of the above expressions.
First
Expression
Follow
{(, id}
E → TE’
{$, )}
{+, -, e}
E’ → +TE’ | -TE’ | e
{$, )}
{(, id}
T → FT’
{+, -, $, )}
{*, /, e)
T’ → *FT’ | /FT’ | e
{+, -, $, )}
{(, id}
F → GG’
{*, /, +, -, $, )}
{%, e}
G → %F | e
{*, /, +, -, $, )}
{(, id}
G → (E) | id
{%, *, /, +, -, $, )}
Now, we construct a LL(1) parsing table for the above expression considering the panic mode recovery. The SCAN here refers to discarding the element while parsing. The POP here refers to the Popping of the first element of the parsing stack.
(
)
+
-
*
/
%
id
$
E
E→TE’
POP
SCAN
SCAN
SCAN
SCAN
SCAN
E→TE’
POP
E’
SCAN
E’→e
E’→+TE’
E’→+TE’
SCAN
SCAN
SCAN
SCAN
E’→e
T
T→FT’
POP
POP
POP
SCAN
SCAN
SCAN
T→FT’
POP
T’
SCAN
T’→e
T’→e
T’→e
T’→*FT’
T’→/FT’
SCAN
SCAN
T’→e
F
F→GG’
POP
POP
POP
POP
POP
SCAN
F→GG’
POP
G’
SCAN
G’→e
G’→e
G’→e
G’→e
G’→e
G’→%F
SCAN
G’→e
G
G→(E)
POP
POP
POP
POP
POP
POP
G→id
POP
From the above-created table, we can parse any input string. For any syntax error generated, the parser can handle it using the panic mode recovery.
Example- Input String * ) $
Consider the parsing stack as above.
We now proceed as
T is matched with the first element of the string * ) $.
The table value for row E and column * is SCAN. Therefore we discard the element *.
Now stack top has T and input has ) as the first element. The match is looked at in the table. It is POP, so the stack's first element is popped. T is popped out.
The top stack element is E', and the element is ). The match is looked at in the table. It is E’→e. The current stack top is replaced with e. Since e is epsilon, the stack's top element is ).
The top stack element is ), and the element is ). Here both non-terminals are the same. Therefore, the stack is popped once, and the parser moves to the next string element.
The top stack element is T', and the element is $. The match is looked at in the table. It is e; therefore, the stack is popped once.
The top stack element is E', and the element is $. The match is looked at in the table. It is e; therefore, the stack is popped once.
The top stack element is $, and the element is $. Since both matches, the stack is popped. Now the stack is empty, and the input string is parsed. Therefore the parsing is done considering the panic mode recovery.
Frequently Asked Questions
What are the syntax errors?
These are the errors that occur when the source code of a program is found to violate the rules of defined syntax for a programming language. The compiler detects such errors during the parsing phase.
What is error recovery in compiler design?
Error recovery in compiler design refers to the process wherein a compiler tends to recover after an error has been encountered. Recovery can include either discarding or addition of some portion of code.
Can a compiler detect all the errors?
The compiler can detect and recover from many errors but not all. The runtime error can be one such error that passes the compilation but fails during the runtime. Some semantic errors are also not detected during compilation time.
Conclusion
In this article, we discussed and learned about panic mode recovery in compiler design. We saw types of errors in compiler design and particularly talked about syntax errors. We learned the working of panic mode recovery. Lastly, we discussed a detailed example of an LL(1) parser using panic mode recovery in compiler design.
To learn more about error handling and panic mode recovery in compiler design, you can check out our other blogs