Open In App

Lexical Error

Improve
Improve
Like Article
Like
Save
Share
Report

When the token pattern does not match the prefix of the remaining input, the lexical analyzer gets stuck and has to recover from this state to analyze the remaining input. In simple words, a lexical error occurs when a sequence of characters does not match the pattern of any token. It typically happens during the execution of a program.

Types of Lexical Error:

Types of lexical error that can occur in a lexical analyzer are as follows:

1. Exceeding length of identifier or numeric constants.

Example:

C++




#include <iostream>
using namespace std;
 
int main() {
 
    int a=2147483647 +1;
    return 0;
}


This is a lexical error since signed integer lies between −2,147,483,648 and 2,147,483,647  

2. Appearance of illegal characters

Example:  

C++




#include <iostream>
using namespace std;
 
int main() {
 
    printf("Geeksforgeeks");$
    return 0;
}


This is a lexical error since an illegal character $ appears at the end of the statement.

3. Unmatched string

Example:  

C++




#include <iostream>
using namespace std;
 
int main() {
/* comment
    cout<<"GFG!";
    return 0;
}


This is a lexical error since the ending of comment “*/” is not present but the beginning is present.

4. Spelling Error

C++




#include <iostream>
using namespace std;
 
int main() {
 
    int 3num= 1234;    /* spelling error as identifier
                       cannot start with a number*/
    return 0;
}


5. Replacing a character with an incorrect character.

C++




#include <iostream>
using namespace std;
 
int main() {
 
    int x = 12$34; /*lexical error as '$' doesn't
                     belong within 0-9 range*/
    return 0;
}


Other lexical errors include 

6. Removal of the character that should be present.

C++




#include <iostream> /*missing 'o' character
                     hence lexical error*/
using namespace std;
 
int main() {
 
    cout<<"GFG!";
    return 0;
}


7. Transposition of two characters.

C++




#include <iostream>
using namespace std;
 
int mian()
{
    /* spelling of main here would be treated as an lexical
       error and won't be considered as an identifier,
       transposition of character 'i' and 'a'*/
    cout << "GFG!";
    return 0;
}


Error Recovery Technique

When a situation arises in which the lexical analyzer is unable to proceed because none of the patterns for tokens matches any prefix of the remaining input. The simplest recovery strategy is “panic mode” recovery. We delete successive characters from the remaining input until the lexical analyzer can identify a well-formed token at the beginning of what input is left. 

Error-recovery actions are:

  1. Transpose of two adjacent characters.
  2. Insert a missing character into the remaining input.
  3. Replace a character with another character.
  4. Delete one character from the remaining input.


Last Updated : 14 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads