# include <iostream.h>
# include <string.h>
# include <stdio.h>
# include <conio.h>
# include <ctype.h>
typedef enum { false=0,true=1 } bool;
/*************************************************************************//*************************************************************************///----------------------- Function Prototypes -------------------------///*************************************************************************//*************************************************************************/constbool isPositiveNumber(constchar*);
constbool isNegativeNumber(constchar*);
constbool isPositiveDigit(constchar*);
constbool isNegativeDigit(constchar*);
constbool isIdentifier(constchar*);
constbool isKeyword(constchar*);
/*************************************************************************//*************************************************************************///------------------------------ main( ) ------------------------------///*************************************************************************//*************************************************************************/void main( )
{
clrscr( );
char Token[25]={NULL};
cout<<"Enter a token : ";
gets(Token);
if(isKeyword(Token)==true)
cout<<"The given Token is a Keyword.";
elseif(isIdentifier(Token)==true)
cout<<"The given Token is an Identifier.";
elseif(isPositiveDigit(Token)==true)
cout<<"The given Token is a Positive Digit.";
elseif(isNegativeDigit(Token)==true)
cout<<"The given Token is a Negative Digit.";
elseif(isPositiveNumber(Token)==true)
cout<<"The given Token is a Positive Number.";
elseif(isNegativeNumber(Token)==true)
cout<<"The given Token is a Negative Number.";
else
cout<<"Error : The given Token is invalid.";
getch( );
}
/*************************************************************************//*************************************************************************///---------------------- Function Definitions -------------------------///*************************************************************************//*************************************************************************//*************************************************************************///----------------------- isPositiveNumber( ) -------------------------///*************************************************************************/constbool isPositiveNumber(constchar* Token)
{
if((!isdigit(Token[0]) && Token[0]!='+') || Token[0]=='0')
returnfalse;
if(Token[0]=='+' && (Token[1]=='0' || !isdigit(Token[1])))
returnfalse;
int length=strlen(Token);
for(int count=1;count<length;count++)
{
if(!isdigit(Token[count]))
returnfalse;
}
returntrue;
}
/*************************************************************************///----------------------- isNegativeNumber( ) -------------------------///*************************************************************************/constbool isNegativeNumber(constchar* Token)
{
if(Token[0]!='-' || !isdigit(Token[1]) || Token[1]=='0')
returnfalse;
int length=strlen(Token);
for(int count=2;count<length;count++)
{
if(!isdigit(Token[count]))
returnfalse;
}
returntrue;
}
/*************************************************************************///------------------------ isPositiveDigit( ) -------------------------///*************************************************************************/constbool isPositiveDigit(constchar* Token)
{
if(strlen(Token)>2 || strlen(Token)==0)
returnfalse;
elseif(strlen(Token)==2 && (Token[0]!='+' ||
Token[1]=='0' || !isdigit(Token[1])))
returnfalse;
elseif(strlen(Token)==1 && (Token[0]=='0' || !isdigit(Token[0])))
returnfalse;
returntrue;
}
/*************************************************************************///------------------------ isNegativeDigit( ) -------------------------///*************************************************************************/constbool isNegativeDigit(constchar* Token)
{
if(strlen(Token)!=2 || Token[0]!='-' ||
Token[1]=='0' || !isdigit(Token[1]))
returnfalse;
returntrue;
}
/*************************************************************************///------------------------- isIdentifier( ) ---------------------------///*************************************************************************/constbool isIdentifier(constchar* Token)
{
if(!isalpha(Token[0]) && Token[0]!='_')
returnfalse;
int length=strlen(Token);
for(int count=1;count<length;count++)
{
if(!isalpha(Token[count]) && !isdigit(Token[count]) &&
Token[count]!='_')
returnfalse;
}
returntrue;
}
/*************************************************************************///--------------------------- isKeyword( ) ----------------------------///*************************************************************************/constbool isKeyword(constchar* Token)
{
if(strlen(Token)>16 || strlen(Token)==0)
returnfalse;
constchar Keywords[64][20]={
"asm","auto","bool","break","case","catch",
"char","class","const","const_cast",
"continue","default","delete","do","double",
"dynamic_cast","else","enum","explicit",
"export","extern","false","float","for",
"friend","goto","if","inline","int","long",
"main","mutable","namespace","new",
"operator","private","protected","public",
"register","reinterpret_cast","return",
"short","signed","sizeof","static",
"static_cast","struct","switch","template",
"this","throw","true","try","typedef",
"typeid","typename","union","unsigned",
"using","virtual","void","volatile",
"wchar_t","while"
};
for(int count=0;count<64;count++)
{
if(strcmpi(Keywords[count],Token)==0)
returntrue;
}
returnfalse;
}
/*************************************************************************//*************************************************************************///----------------------------- THE END -------------------------------///*************************************************************************//*************************************************************************/