Lexical Analysis in simple words – token generation, blank space, symbol table, lexical error

Lexical Analysis in simple words – token generation, blank space, symbol table, lexical error



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

Lexical Analysis in Simple Words

Lexical Analysis is the first phase of a compiler that breaks down the source code into smaller meaningful units called tokens. This process is done by a program called the Lexical Analyzer (Lexer).

 How Lexical Analysis Works?

 The source code is read character by character.
 Meaningful groups of characters are converted into tokens.
 Unnecessary characters like spaces, tabs, and comments are removed.
 A symbol table is created to store identifiers and keywords.
 If an unknown symbol is found, a lexical error is reported.

 Example of Token Generation

Input Code:

int x = 10;

Lexical Analyzer Output (Tokens):

Keyword: int
Identifier: x
Operator: =
Number: 10
Symbol: ;

Each part of the code is classified into a specific token type.

 Handling Blank Spaces & Comments

Whitespace (spaces, tabs, new lines) is ignored by the lexical analyzer.
Comments (//, /* ... */) are removed, as they don’t affect program execution.

Example:

int a = 5; // This is a comment

 The lexer ignores "// This is a comment" and processes only:

Keyword: int
Identifier: a
Operator: =
Number: 5
Symbol: ;

 Symbol Table

 A symbol table stores identifiers (variable names, function names) and their attributes like data type, memory location, scope, etc.
 It helps the compiler track variables efficiently.

Example Symbol Table:

Identifier Type Memory Location
x int 1001
y float 1002

 Lexical Errors

Errors occur when the lexer finds an unknown or invalid character.

Example:

int @x = 10;

Lexical Error: “Unexpected character ‘@’ found.”

 Summary

Lexical Analysis breaks code into tokens.
Blank spaces & comments are ignored.
 A symbol table stores variable names and types.
Lexical errors occur due to unknown characters.

Would you like a step-by-step breakdown of a real Lexical Analysis process?

Lexical Analysis in simple words – token generation, blank space, symbol table, lexical error

LEXICAL ANALYSIS Structure of compiler – Functions and …

Lexical Analysis – Compiler Construction

compiler design lecture notes

Chapter 3 Lexical Analysis

CS 335: Lexical Analysis



Leave a Reply

Your email address will not be published. Required fields are marked *

error: