
A token is the smallest meaningful unit of a program that the compiler understands.
A token is the smallest meaningful unit of a program that the compiler understands.
int x = 10;
π Tokens:
int | x | = | 10 | ;
| Term | Meaning |
|---|---|
| Token | Category (e.g., identifier, keyword) |
| Lexeme | Actual text (e.g., x, 10) |
| Pattern | Rule used to define token |
π Token = <Token Name, Attribute Value>
Example:
(id, pointer to symbol table)
(num, 10)
Specification of tokens means defining tokens using rules or patterns so that the lexical analyzer can identify them.
Tokens are specified using:
π A regular expression is a pattern used to describe a set of strings.
π Rule:
letter (letter | digit)*
π Examples:
x, count, total1
π Rule:
digit+
π Examples:
10, 25, 100
π Predefined words:
int, if, while, return
+, -, *, /, =
;, (, ), {, }
| Token Type | Regular Expression | Example | |
|---|---|---|---|
| Identifier | letter (letter | digit)* | var1 |
| Number | digit+ | 123 | |
| Operator | + | - | * |
| Whitespace | space/tab/newline | β |
Recognition of tokens is the process of identifying tokens in the input string using the specified patterns.
Using:
Input String β Pattern Matching β Token Identified
Input:
sum = a + 5;
Recognition:
sum β identifier
= β operator
a β identifier
+ β operator
5 β number
; β delimiter
A machine used to recognize patterns in input strings.
Start β letter β (letter/digit)* β Accept
(q0) --letter--> (q1)
(q1) --letter/digit--> (q1)
π βRegular Expressions β Finite Automata β Token Recognitionβ
@1abcint total = 25;
int β keywordtotal β identifier25 β number| Lexeme | Token |
|---|---|
| int | keyword |
| total | identifier |
| = | operator |
| 25 | number |
| ; | delimiter |
π Frequently asked:
Regular Expression β DFA β Token Recognition
| Aspect | Specification of Tokens | Recognition of Tokens |
|---|---|---|
| Definition | Defining token patterns | Identifying tokens in input |
| Tool Used | Regular Expressions | Finite Automata (DFA/NFA) |
| Purpose | Describe tokens | Detect tokens |
| Stage | Before scanning | During scanning |
| Example | digit+ | Recognizing "123" as number |
| Output | Token rules | Actual tokens |
| Importance | High | Very High |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.