#include <stdio.h>
#include <string.h>
#include <iostream.h>
#define BEGIN 1
#define IF 2
#define THEN 3
#define WHILE 4
#define DO 5
#define END 6
#define INT 11
#define LT 20
#define LE 21
#define EQ 24
#define NE 22
#define GT 12
#define GE 24
#define IS 18
#define PL 13
#define MI 14
#define MU 15
#define DI 16
#define SS 17
#define ID 10
#define AI 26
#define BI 27
#define HI 58
#define JI 0
char *keyword[8]={"begin","end","if","then","do","while"};
int i=0,j=0,k=0,t=0;
char ch,strtoken[20];
char * chr_form[100];
char * int_form[100];
char form[1000];
int q=0,temp;
void GetChar()
{
ch=form[k];
k++;
}
void getbc()
{
while(ch==' ')
{
GetChar();
}
}
void concat()
{
strtoken[i]=ch;
i++;
}
bool isletter()
{
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
return(1);
else
return(0);
}
bool isdigit()
{
if(((ch)<='9')&&((ch)>='0'))
return (1);
else
return (0);
}
int reserve()
{
for(int q=0;q<5;q++)
{
if(strcmp(keyword[q],strtoken)==0)
return q;
if(q==4)
return -1;
}
}
void retract()
{
k--;
ch=NULL;
}
char *insertld()
{
chr_form[j]=strtoken;
j++;
return chr_form[0];
}
char * insertconst()
{
int_form[t]=strtoken;
t++;
return int_form[0];
}
int code;
void output(int a,char *p1,char *p2)
{
cout<<"\t種别碼:"<<a<<"\t 單詞值:";
while(p1<=p2)
{
printf("%s",*p1);
p1++;
}
cout<<endl;
}
void analyze()
{
GetChar();
getbc();
if(isletter())
{
while (isletter()||isdigit())
{
concat();
GetChar();
}
retract();
code=reserve();
switch(code)
{
case 0:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<BEGIN<<endl;break;
case 1:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<END<<endl;break;
case 2:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<IF<<endl;break;
case 3:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<THEN<<endl;break;
case 4:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<DO<<endl;break;
case 5:cout<<"單詞:" <<strtoken<<" 種别碼為: "<<WHILE<<endl;break;
default:
cout<<"單詞:" <<strtoken<<" 種别碼為: "<<ID<<endl;break;
}
}
else
{
if( isdigit())
{
while(isdigit()||ch=='.')
{
concat();
GetChar();
}
retract();
cout<<"單詞:"<<strtoken<<" 種别碼為:"<<INT<<endl;
}
else
{
switch(ch)
{
case'+': cout<<"單詞:+ 種别碼為: "<<PL<<endl;break;
case'-': cout<<"單詞:- 種别碼為: "<<MI<<endl;break;
case'*': cout<<"單詞:* 種别碼為: "<<MU<<endl;break;
case'/': cout<<"單詞:/ 種别碼為: "<<DI<<endl;break;
case';': cout<<"單詞:; 種别碼為: "<<AI<<endl;break;
case'(': cout<<"單詞:( 種别碼為: "<<BI<<endl;break;
case')': cout<<"單詞:) 種别碼為: "<<HI<<endl;break;
case'#': cout<<"單詞:# 種别碼為: "<<JI<<endl;break;
case':':GetChar();
if(ch=='=')
{
cout<<"單詞::= 種别碼為: "<<IS<<endl;break;
}
else
{
retract();
cout<<"單詞:: 種别碼為: "<<SS<<endl;break;
}
case'=':cout<<"單詞:= 種别碼:"<<EQ<<endl; break;
case'>':GetChar();switch(ch)
{
case'=':cout<<"單詞:>= 種别碼:"<<GE<<endl;break;
default: retract;
cout<<"單詞:=> 種别碼:"<<GT<<endl;break;
}
case'<':GetChar();
switch(ch)
{
case'=':cout<<"單詞:<= 種别碼:"<<LE<<endl;break;
case'>':cout<<"單詞:<> 種别碼:"<<NE<<endl;break;
default: retract();
cout<<"單詞:< 種别碼:"<<LT<<endl;break;
}
}
}
}
while(k<q)
{
for(int p=0;p<50;p++)
strtoken[p]='\0';
i=0;
analyze();
}
}
void main ()
{
printf("輸入一段程式,以!結束:");
form[0]=cin.get();
for(q=1;form[q-1]!='!!';q++)
{
form[q]=cin.get();
if(form[q]=='!')
{
printf("你輸入的程式段為:\n");
cout.write(form,q);
break;
}
}
cout<<endl;
analyze();
}