VOOZH about

URL: https://www.geeksforgeeks.org/compiler-design/lexical-error/

⇱ Lexical Error - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lexical Error

Last Updated : 23 Jul, 2025

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:

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:  

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

3. Unmatched string

Example:  

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

4. Spelling Error

5. Replacing a character with an incorrect character.

Other lexical errors include 

6. Removal of the character that should be present.

7. Transposition of two characters.

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.
Comment
Article Tags:

Explore