Lexical analysis, also known as scanning, is the first phase of a compiler. In this phase, the compiler reads the source program character by character from left to right and groups them into meaningful units called tokens. These tokens serve as the input for the next phase of compilation, known as syntax analysis.

Token
A token is a sequence of characters that represents a basic unit of meaning in a programming language. Tokens are defined by the grammar of the language and are used by the parser to understand program structure.
Categories of Tokens
Keywords
Keywords are reserved words that have predefined meanings in a programming language.
- Examples in C:
if,else,for,while,int,void - They cannot be used as identifiers
- C language has 32 keywords
Identifiers
Identifiers are names given to variables, functions, arrays, or other user-defined elements.
Rules:
- Must start with a letter or underscore
_ - Can contain letters, digits, and underscores
- Case-sensitive
- Cannot be a keyword
Examples: count, _sum, totalValue
Constants
Constants are fixed values whose value does not change during program execution.
Examples:
- Integer:
10 - Floating-point:
3.14 - Character:
'a' - String:
"Hello"
Operators
Operators perform operations on operands.
Examples:
- Arithmetic:
+,-,*,/ - Relational:
<,>,== - Logical:
&&,||
Special Symbols
Special symbols are used to structure programs.
Examples:
;â statement terminator,â separator{ }â code blocks[ ]â arrays
Read more about Tokens.
Lexeme
A lexeme is an actual string of characters that matches with a pattern and generates a token.
Example
float â KEYWORD
abs_zero_Kelvin â IDENTIFIER
= â OPERATOR
273 â INTEGER
; â SEMICOLON
Lexemes and Tokens Representation
| Lexemes | Tokens | Lexemes Continued... | Tokens Continued... |
|---|---|---|---|
| Â while | Â WHILE | a | IDENTIEFIER |
| Â ( | LAPREN | = | ASSIGNMENT |
| Â Â a | IDENTIFIER | a | IDENTIFIER |
| Â >= | COMPARISON | - | ARITHMETIC |
| Â Â b | IDENTIFIER | 2 | INTEGER |
| Â ) | RPAREN | ; | SEMICOLON |
How Lexical Analyzer Works?
Tokens in a programming language can be described using regular expressions. A scanner, or lexical analyzer, uses a Deterministic Finite Automaton (DFA) to recognize these tokens, as DFAs are designed to identify regular languages. Each final state of the DFA corresponds to a specific token type, allowing the scanner to classify the input. The process of creating a DFA from regular expressions can be automated, making it easier to handle token recognition efficiently.
Read more about Working of Lexical Analyzer in Compiler.
The lexical analyzer identifies the error with the help of the automation machine and the grammar of the given language on which it is based like C, C++, and gives row number and column number of the error.
Suppose we pass a statement through lexical analyzer: a = b + c;
It will generate token sequence like this: id=id+id; Where each id refers to itâs variable in the symbol table referencing all details For example, consider the program
int main()
{
// 2 variables
int a, b;
a = 10;
return 0;
}
All the valid tokens are:
'int' 'main' '(' ')' '{' 'int' 'a' ',' 'b' ';'
'a' '=' '10' ';' 'return' '0' ';' '}'
Above are the valid tokens. You can observe that we have omitted comments. As another example, consider below printf statement.
There are 5 valid token in this printf statement.
Exercise 1: Count number of tokens:
int main()
{
int a = 10, b = 20;
printf("sum is:%d",a+b);
return 0;
}
Answer: Total number of token: 27.
Exercise 2: Count number of tokens:
int max(int i);
- Lexical analyzer first read int and finds it to be valid and accepts as token.
- max is read by it and found to be a valid function name after reading (
- int is also a token , then again I as another token and finally ;
Answer: Total number of tokens 7: int, max, ( ,int, i, ), ;

