The Wayback Machine - https://web.archive.org/web/20261119103006/https://www.geeksforgeeks.org/compiler-design/introduction-of-lexical-analysis/

Introduction of Lexical Analysis

Last Updated : 16 Nov, 2026

Lexical analysis, also known as scanning, is the first phase of a compiler. It reads source code character by character and groups the characters into meaningful units called tokens. These tokens are then passed to the syntax analysis phase for further processing.

lexical_analysis
Lexical Analysis

Token

A tokens is a sequence of characters that represents a basic unit of meaning in a programming language. Tokens are identified by the lexical analyzer and passed to the parser for syntax analysis.

Categories of Tokens

The common categories of tokens in programming languages include:

1. Keywords

Reserved words that have predefined meanings in a programming language and cannot be used as identifiers.

  • Examples in C: if, else, for, while, int, void, return.
  • Their meanings are defined by the programming language.

2. Identifiers

Names given to variables, functions, arrays, and other user-defined program elements.

Rules for identifiers in C:

  • Must begin with a letter or underscore (_).
  • Can contain letters, digits, and underscores.
  • Are case-sensitive.
  • Cannot be a keyword.

Examples: count, _sum, totalValue.

3. Constants and Literals

Fixed values represented directly in the source code.

Examples:

  • Integer literal: 10
  • Floating-point literal: 3.14
  • Character literal: 'a'
  • String literal: "Hello"

4. Operators

Symbols used to perform operations on operands.

Examples:

  • Arithmetic: +, -, *, /
  • Relational: <, >, ==
  • Logical: &&, ||
  • Assignment: =, +=

5. Special Symbols (Punctuators)

Symbols used to structure and separate different parts of a program.

Examples:

  • ; : Statement terminator
  • , : Separator
  • { } : Block delimiters
  • : Array indexing symbols
  • ( ) : Parentheses

Lexeme

A lexeme is the actual sequence of characters in the source code that matches a token pattern. Each lexeme is identified by the lexical analyzer and assigned a token type.

Example:

Consider the following statement:

float abs_zero_Kelvin = 273;

Its lexemes and corresponding tokens are:

Lexeme

Token

float

KEYWORD

abs_zero_Kelvin

IDENTIFIER

=

ASSIGNMENT OPERATOR

273

INTEGER LITERAL

;

SEMICOLON

Lexemes and Tokens Representation

Consider the following statement:

while (a >= b - 2);

The lexical analyzer identifies the following lexemes and tokens:

Lexeme

Token

while

WHILE

(

LPAREN

a

IDENTIFIER

>=

RELATIONAL OPERATOR

b

IDENTIFIER

-

ARITHMETIC OPERATOR

2

INTEGER LITERAL

)

RPAREN

;

SEMICOLON

Working of Lexical Analyzer

Tokens in a programming language are defined using regular expressions. The lexical analyzer (scanner) uses a Deterministic Finite Automaton (DFA) to recognize these tokens because DFAs can identify regular languages efficiently.

Each final state of the DFA represents a specific token type. The process of converting regular expressions into a DFA can be automated, making token recognition fast and systematic.

The Lexical Analyzer can also detect errors such as:

  • Invalid characters
  • Incorrect identifiers

It reports errors along with the line number and column number.

Examples of Lexical Analysis

Example 1: Tokenize an Assignment Statement

Input:

a = b + c;

Token sequence:

IDENTIFIER ASSIGNMENT IDENTIFIER
ARITHMETIC_OPERATOR IDENTIFIER SEMICOLON

Each identifier can be associated with an entry in the symbol table containing information about the corresponding variable.

Example 2: Tokenize a C Program

Input:

int main()
{
int a, b;
a = 10;
return 0;
}

Valid tokens:

'int' 'main' '(' ')' '{'
'int' 'a' ',' 'b' ';'
'a' '=' '10' ';'
'return' '0' ';'
'}'

The lexical analyzer ignores whitespace and comments (except where they separate tokens or are needed for source-position tracking) and passes the recognized tokens to the syntax analyzer.

Exercises: Count the Number of Tokens

Question 1: Count the number of tokens in the following C program.

int main()
{
int a = 10, b = 20;
printf("sum is:%d", a+b);
return 0;
}

Answer: 27 tokens.

Question 2: Count the number of tokens in the following function declaration.

int max(int i);

Answer: 7 tokens.

The tokens are:

  1. int
  2. max
  3. (
  4. int
  5. i
  6. )
  7. ;
Comment

Explore