Lexical Analysis Examples
Lexical Analysis Examples
Type Examples
Comment // This will compare 2 numbers
Pre-processor directive #include <stdio.h>
Pre-processor directive #define NUMS 8,9
Macro NUMS
Whitespace /n /b /t
C Tokens
• Keywords (eg: int, while),
• Identifiers (eg: main, total),
• Constants (eg: 10, 20),
• Strings (eg: “total”, “hello”),
• Special symbols (eg: (), {}),
• Operators (eg: +, /,-,*)
C TOKENS EXAMPLE PROGRAM:
int main()
{
int x, y, total;
x = 10, y = 20;
total = x + y;
printf ("Total = %d \n", total);
}
Tokens
where,
• main – identifier
• {,}, (,) – delimiter
• int – keyword
• x, y, total – identifier
• main, {, }, (, ), int, x, y, total,10,20 – tokens
Examples
int main()
{ // 2 variables
int a, b;
a = 10;
return 0;
}
'int' 'main' '(' ')' '{' 'int' 'a' ',' 'b' ';' 'a' '=' '10'
';' 'return' '0' ';' '}'
Examples Continued- Count the no of tokens
and what are the tokens
int main()
{
int a = 10, b = 20;
printf("sum is :%d",a+b);
return 0;
}