Lexical Analysis

  • 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:

IdentifierTypeValueMemory LocationScope
xint1500x1001Global

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:

IdentifierTypeValueMemory LocationScope
PIfloat3.141592653590x1001Global
calculateAreafloat()Global
mainint()Global
radiusfloat0x1002main
areafloat0x1003main

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

ExampleDescription/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. 

Leave a comment