- This converts the source code into tokens. The individual characters of the source file must be grouped into tokens.
e.g. x = 150; will be broken into 4 tokens:
| x | = | 150 | ; |
Note that tokens are not just the characters that make up the line, but the parts that make up the entire line. In the above example , 150 is recognized as a single token and not 3 tokens of 1, 5 and 0.
- Next, a symbol (identifier) table is generated which stores the attributes of each identifier including:
- name,
- datatype ,
- Scope: where in the source code it was declared (Globa , within functions)
For our line of code above, the following table entry may be generated:
| Identifier | Type | Value | Memory Location | Scope |
| x | int | 150 | 0x1001 | Global |
Note that in practice, symbol tables may store more metadata – we have simplified the concept for easy understanding.
Symbol Table Example With A Full Program
Code
| #include <stdio.h> float PI = 3.14159265359; // Define PI as a variable float calculateArea(float radius) { return PI * radius * radius; } int main() { float radius; printf(“Enter the radius of the circle: “); scanf(“%f”, &radius); float area = calculateArea(radius); printf(“The area of the circle with radius %.2f is %.2f\n”, radius, area); return 0; } |
Symbol Table
The following simplified symbol table is generated:
| Identifier | Type | Value | Memory Location | Scope |
| PI | float | 3.14159265359 | 0x1001 | Global |
| calculateArea | float() | Global | ||
| main | int() | Global | ||
| radius | float | 0x1002 | main | |
| area | float | 0x1003 | main |
Error Detection (Lexical Error Handling)
Errors in the tokens are detected.
Common errors include detecting :
- invalid characters not specified in the language , e.g
int café = 42; // The ‘é’ character is not valid in C. - invalid identifier declaration, e.g,
int $var = 10; // Invalid character ‘$’ - The misspelling of keywords eg
innt x; //should be int:
Examples of lexical Errors In C
| Example | Description/Explanation |
| int n%mber = 5; | Illegal character % in identifier. |
| floar x = 5.0; | Misspelling of keyword float as floar. |
| int 1stNumber = 1; | Identifiers cannot start with a digit. |
| #incldue <stdio.h> | Misspelling of directive #include. |
| double price = 45.6L; | Incorrect use of float literal suffix (L is for long int). |
| char* str = ‘Hello’; | Single quotes used instead of double quotes for string literal. |
© 2024 Vedesh Kungebeharry. All rights reserved.