#include <string.h>
#include <conio.h>
#include <process.h>
#include <iostream>
using namespace std;
char prog[80];//存储输入串
char token[8];
char ch; //从键盘上获得单个输入,存储在ch中
int syn; //判断
string operater;
int p;
int m;
int n;
int sum; //储存输出数字
char *rwtab[6]={"begin","if","then","while","do","end"};
void scaner();
void getInput();
int main()
{
p=0;
getInput();
p=0;
do{
scaner();
switch(syn)
{
case 11:
cout <<"("<< sum <<" "<< operater.c_str() <<")" <<endl ;
break;
case -1:
cout << "你输入了错误的语句"<<endl;
getch();
exit(0);
case 0:
cout <<""<<endl;
break;
default:
cout << "("<<token << " " << operater.c_str()<<")"<< endl;
break;
}
}while(syn!=0);
getch();
return 0;
}
void scaner()
{
sum=0;
for(m=0;m<8;m++)
token[m++]=NULL;
ch=prog[p++];
m=0;
while((ch==' ')||(ch=='\n'))
ch=prog[p++];
if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
{
while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')||((ch>='0')&&(ch<='9'))))
{
token[m++]=ch;
ch=prog[p++];
operater = "字母";
}
p--;
syn=10;
for(n=0;n<6;n++)
if(strcmp(token,rwtab[n])==0)//判断关键字,确定syn
{
syn=n+1;
operater = "关键字";
break;
}
}
else if((ch>='0')&&(ch<='9'))//判断数字
{
while((ch>='0')&&(ch<='9'))
{
sum=sum*10+ch-'0';
ch=prog[p++];
operater = "数字";
}
p--;
syn=11;
}
else switch(ch)
{
case '<':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=22;
token[m++]=ch;
operater = "符号";
}
else
{
syn=20;
p--;
operater = "符号";
}
break;
case '>':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=24;
token[m++]=ch;
operater = "符号";
}
else
{
syn=23;
p--;
operater = "符号";
}
break;
case '+':
token[m++]=ch;
ch=prog[p++];
if(ch=='+')
{
syn=17;
token[m++]=ch;
operater = "符号";
}
else
{
syn=13;
p--;
operater = "符号";
}
break;
case '-':
token[m++]=ch;
ch=prog[p++];
if(ch=='-')
{
syn=29;
token[m++]=ch;
operater = "符号";
}
else
{
syn=14;
p--;
operater = "符号";
}
break;
case '!':
ch=prog[p++];
if(ch=='=')
{
syn=21;
token[m++]=ch;
operater = "符号";
}
else
{
syn=31;
p--;
operater = "符号";
}
break;
case '=':
token[m++]=ch;
ch=prog[p++];
if(ch=='=')
{
syn=25;
token[m++]=ch;
operater = "符号";
}
else
{
syn=18;
p--;
operater = "符号";
}
break;
case '*':
syn=15;
token[m++]=ch;
operater = "符号";
break;
case '/':
syn=16;
token[m++]=ch;
break;
case '(':
syn=27;
token[m++]=ch;
operater = "符号";
break;
case ')':
syn=28;
token[m++]=ch;
operater = "符号";
break;
case '{':
syn=5;
token[m++]=ch;
operater = "符号";
break;
case '}':
syn=6;
token[m++]=ch;
operater = "符号";
break;
case ';':
syn=26;
token[m++]=ch;
operater = "符号";
break;
case '\"':
syn=30;
token[m++]=ch;
operater = "符号";
break;
case '#':
syn=0;
token[m++]=ch;
break;
case ':':
syn=17;
token[m++]=ch;
operater = "符号";
break;
default:
syn=-1;
break;
}
token[m++]='\0';
}
void getInput()
{
cout <<"please input a string(end with '#'):"<<endl;
do{
ch = cin.get();
prog[p++]=ch;
}while(ch!='#');
}