/* Program on lexical analysis */
#include<stdio.h>
#include<conio.h>
#define MAX 30
void main()
{
char str[MAX];
int state=0;
int i=0, j, startid=0, endid, startcon, endcon;
clrscr();
for(j=0; j<MAX; j++)
str[j]=NULL; //Initialise NULL
printf("*** Program on Lexical Analysis ***");
printf("
Enter the string: ");
gets(str); //Accept input string
str[strlen(str)]=' ';
printf("
Analysis:
");
while(str!=NULL)
{
while(str==' ') //To eliminate spaces
i++;
switch(state)
{
case 0: if(str=='i') state=1; //if
else if(str=='w') state=3; //while
else if(str=='d') state=8; //do
else if(str=='e') state=10; //else
else if(str=='f') state=14; //for
else if(isalpha(str) || str=='_')
{
state=17;
startid=i;
} //identifiers
else if(str=='<') state=19;
//relational '<' or '<='
else if(str=='>') state=21;
//relational '>' or '>='
else if(str=='=') state=23;
//relational '==' or assignment '='
else if(isdigit(str))
{
state=25; startcon=i;
}
//constant
else if(str=='(') state=26;
//special characters '('
else if(str==')') state=27;
//special characters ')'
else if(str==';') state=28;
//special characters ';'
else if(str=='+') state=29;
//operator '+'
else if(str=='-') state=30;
//operator '-'
break;
//States for 'if'
case 1: if(str=='f') state=2;
else { state=17; startid=i-1; i--; }
break;
case 2: if(str=='(' || str==NULL)
{
printf("
if : Keyword");
state=0;
i--;
}
else { state=17; startid=i-2; i--; }
break;
//States for 'while'
case 3: if(str=='h') state=4;
else { state=17; startid=i-1; i--; }
break;
case 4: if(str=='i') state=5;
else { state=17; startid=i-2; i--; }
break;
case 5: if(str=='l') state=6;
else { state=17; startid=i-3; i--; }
break;
case 6: if(str=='e') state=7;
else { state=17; startid=i-4; i--; }
break;
case 7: if(str=='(' || str==NULL)
{
printf("
while : Keyword");
state=0;
i--;
}
else { state=17; startid=i-5; i--; }
break;
//States for 'do'
case 8: if(str=='o') state=9;
else { state=17; startid=i-1; i--; }
break;
case 9: if(str=='{' || str==' ' || str==NULL || str=='(')
{
printf("
do : Keyword");
state=0;
i--;
}
break;
//States for 'else'
case 10: if(str=='l') state=11;
else { state=17; startid=i-1; i--; }
break;
case 11: if(str=='s') state=12;
else { state=17; startid=i-2; i--; }
break;
case 12: if(str=='e') state=13;
else { state=17; startid=i-3; i--; }
break;
case 13: if(str=='{' || str==NULL)
{
printf("
else : Keyword");
state=0;
i--;
}
else { state=17; startid=i-4; i--; }
break;
//States for 'for'
case 14: if(str=='o') state=15;
else { state=17; startid=i-1; i--; }
break;
case 15: if(str=='r') state=16;
else { state=17; startid=i-2; i--; }
break;
case 16: if(str=='(' || str==NULL)
{
printf("
for : Keyword");
state=0;
i--;
}
else { state=17; startid=i-3; i--; }
break;
//States for identifiers
case 17:
if(isalnum(str) || str=='_')
{
state=18; i++;
}
else if(str==NULL||str=='<'||str=='>'||str=='('||str==')'||str==';'||str=='='||str=='+'||str=='-') state=18;
i--;
break;
case 18:
if(str==NULL || str=='<' || str=='>' || str=='(' || str==')' || str==';' || str=='=' || str=='+' ||str=='-')
{
endid=i-1;
printf("
");
for(j=startid; j<=endid; j++)
printf("%c", str[j]);
printf(" : Identifier");
state=0;
i--;
}
break;
//States for relational operator '<' & '<='
case 19: if(str=='=') state=20;
else if(isalnum(str) || str=='_')
{
printf("
< : Relational operator");
i--;
state=0;
}
break;
case 20: if(isalnum(str) || str=='_')
{
printf("
<= : Relational operator");
i--;
state=0;
}
break;
//States for relational operator '>' & '>='
case 21: if(str=='=') state=22;
else if(isalnum(str) || str=='_')
{
printf("
> : Relational operator");
i--;
state=0;
}
break;
case 22: if(isalnum(str) || str=='_')
{
printf("
>= : Relational operator");
i--;
state=0;
}
break;
//States for relational operator '==' & assignment operator '='
case 23: if(str=='=') state=24;
else
{
printf("
= : Assignment operator");
i--;
state=0;
}
break;
case 24: if(isalnum(str))
{
printf("
== : Relational operator");
state=0;
i--;
}
break;
//States for constants
case 25: if(isalpha(str))
{
printf("
*** ERROR ***
");
puts(str);
for(j=0; j<i; j++)
printf(" ");
printf("^");
printf("
Error at position %d
Alphabet cannot follow digit", i);
state=99;
}
else if(str=='(' || str==')' || str=='<' || str=='>' || str==NULL || str==';' || str=='=')
{
endcon=i-1;
printf("
");
for(j=startcon; j<=endcon; j++)
printf("%c", str[j]);
printf(" : Constant");
state=0;
i--;
}
break;
//State for special character '('
case 26: printf("
( : Special character");
startid=i;
state=0;
i--;
break;
//State for special character ')'
case 27: printf("
) : Special character");
state=0;
i--;
break;
//State for special character ';'
case 28: printf("
; : Special character");
state=0;
i--;
break;
//State for operator '+'
case 29: printf("
+ : Operator");
state=0;
i--;
break;
//State for operator '-'
case 30: printf("
+ : Operator");
state=0;
i--;
break;
//Error State
case 99: goto END;
}
i++;
}
printf("
End of program");
END:
getch();
}
/* Output
Correct input
-------------
*** Program on Lexical Analysis ***
Enter the string: for(x1=0; x1<=10; x1++);
Analysis:
for : Keyword
( : Special character
x1 : Identifier
= : Assignment operator
0 : Constant
; : Special character
x1 : Identifier
<= : Relational operator
10 : Constant
; : Special character
x1 : Identifier
+ : Operator
+ : Operator
) : Special character
; : Special character
End of program
Wrong input
-----------
*** Program on Lexical Analysis ***
Enter the string: for(x1=0; x1<=19x; x++);
Analysis:
for : Keyword
( : Special character
x1 : Identifier
= : Assignment operator
0 : Constant
; : Special character
x1 : Identifier
<= : Relational operator
Token cannot be generated
*/