Sei sulla pagina 1di 10

SYNTAX ERROR

HANDLING
Compiler construction
Phases of compiler
Source program Lexical analysis
Token error

Syntax analysis Front end


Parse tree doesn’t generate
properly syntax mistakes
Semantic
Symbol analysis Error
table Handling
manager Intermediate code generator

Back end Code optimization

Target code generator


Error handling
1. Find Error • Syntax error occurs during syntax
analysis phase
2. Diagnosed error • It found during the execution of the
3. Error Recovery program.
 some syntax errors can be
(number == 200)
If (number = 200) • Error in structure (parse tree not
• Syntax error Count<<“ no: is equal generated properly)
to 200; • Missing operators ( using = when == is
Else needed)
Count<< “no: is not
• Unbalanced parenthesis ( not closing
equal to 200; the program by using parenthesis)
Syntax error

• In this case the syntax warning will be come


• In this code if expressions used the different sign which is actually an assignment
operator (=) not the relational operator(== shows equality) which tests for equality
• Example 2:
• Float x = 1.2 ( semicolon is missing)
• Example 3
• Errors in expressions
• X = (3+5 (parenthesis is missing)
These all errors tends to syntax error
Lexical error / semantic error
• Include miss spellings of identifiers keywords and operator
• Example
Int xyz;
Printf (“xyz1”)
• Semantic error
Includes the mismatch between operator and operand
Int msg()
{
Float
Switch (op)
{
----------
-------------
}
Printf(“hello Pakistan”); $
Error recovery strategies
•Panic mode
•Phrase level recovery
•Error production
•Global correction
Panic mode recovery
• The parser discards the input symbol one at a time until one of the designated set of
synchronizing token is found

Sync tokens which meanings are very


clear which tells that after semicolon
So what parser did sync token which program is ended. And after that
are already decided e.g we write from new line you can analyze the
begin and end in pseudo code so syntax
end is an sync token which is used in
panic mode recovery process If we use semicolon at the end of the
statement so we must know that line
Simple and it doesn’t go to the will be ended after semicolon so
infinite loop when we found the ending node so
we can analyze new statmenet
This is useful when multiple errors
encounter in same statement is rare.
Phrase level recovery
• Perform local corrections on the remaining input when error is discovered
• Replaces the prefix of the remaining input from some strings so as to perform some
correction locally.
• And which allow parser to continue execution to the remaining program.
• If parser founds that there is some syntax error occurs or its bot following the correct
syntax so it will use phrase recovery mechanism
• Parser will replace the incorrect input into the correct syntax E.g
If semicolon is expected but it writes coma so parser will replace it its called the local
correction
If some particular semicolon is missing it will insert it e.g auto correction in some word
processing sotwares
Insertion deletion or updating of any character.
Drawback: correction is difficult specially in the cases where actual error
has occur before the point of detection
Error production

• Some common error are known to the compiler design that may occur in the code
• In addition the designer can create augmented grammar to be used as a production
that generate erroneous construct when error is occur
• Example
5X
5*X
Global corrections
• The parser consider a program as whole and its tries to figure out that what program
indented to do.
• Parser tries to find the closest match for it which is error free
• When an erroneous statement is fed it allows a parse tree for some closest minimal
changes in the code
• This practice isn’t using yet because of its time taking and complexity.

Potrebbero piacerti anche