Documente Academic
Documente Profesional
Documente Cultură
#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();
printf("Analysis:");
while(str[i]!=NULL)
{
while(str[i]==' ') //To eliminate spaces
i++;
switch(state)
{
case 0: if(str[i]=='i') state=1; //if
else if(str[i]=='w') state=3; //while
else if(str[i]=='d') state=8; //do
else if(str[i]=='e') state=10; //else
else if(str[i]=='f') state=14; //for
else if(isalpha(str[i]) || str[i]=='_')
{
state=17;
startid=i;
} //identifiers
else if(isdigit(str[i]))
{
state=25; startcon=i;
}
//constant
break;
if(isalnum(str[i]) || str[i]=='_')
{
state=18; i++;
}
else if(str[i]==NULL||str[i]=='<'||str[i]=='>'||str[i]=='('||str[i]==')'||str[i]==';'||str[i]=='='||
str[i]=='+'||str[i]=='-') state=18;
i--;
break;
case 18:
//Error State
case 99: goto END;
}
i++;
}
printf("\nEnd of program");
END:
getch();
}
/* Output
Correct input
-------------
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
-----------
Analysis:
for : Keyword
( : Special character
x1 : Identifier
= : Assignment operator
0 : Constant
; : Special character
x1 : Identifier
<= : Relational operator