📄 songwei.cpp
字号:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#define $XIAOYU 0 //<
#define $DAYU 1 //>
#define $DENG 4 //=
#define $IF 6 //IF
#define $if 7 //if
#define $THEN 8 //THEN
#define $then 9 //then
#define $ID 10 //标志符
#define $NUM 11 //数字
typedef struct Token
{
int type; //类型
char ch; //值
}Token;
Token cur;
#define MAX_TOKEN 256 //Token表大小
Token tokentable[MAX_TOKEN];
int token_index; //token表索引
int total_len; //token表有效长度
struct
{
char str[10];
}stack[80],buffer[80];
int pom=0; //栈指针
int inc=0;
ifstream shuru;
bool init(char filename[255]);
bool cifa();
void S();
void E();
bool nexttoken();
void print();
void push(char arg[8]);
void pop();
int main()
{
cout<<"Please input the file which wants to be compiled: ";
char fname[200];
cin>>fname;
if(!init(fname))
return 1;
if(!cifa())
return 1;
char ch;
while(1)
{
if(shuru.eof()) //如果文件结束
break;
shuru>>ch;
}
cout<<"使用的文法为:"<<endl;
cout<<"S->E|if E then S"<<endl;
cout<<"E->T<T|T>T|T=T"<<endl;
cout<<"T->id|num"<<endl;
nexttoken();
cout<<"所使用的产生式:"<<endl;
S();
cout<<"逆波兰式:"<<endl;
print();
return 0;
}
bool init(char filename[200]) //打开文件
{
token_index=0;
total_len=0;
shuru.open(filename,ios::nocreate|ios::in);
if(!shuru)
{
cout<<"文件打开出错!"<<endl;
return false;
}
else
return true;
}
bool cifa()
{
char buf[16];
char ch;
while(1)
{
shuru>>ch;
if(!shuru)
break;
else if(ch=='<')
{
tokentable[total_len].type=$XIAOYU;
tokentable[total_len++].ch=ch;
}
else if(ch=='>')
{
tokentable[total_len].type=$DAYU;
tokentable[total_len++].ch=ch;
}
else if(ch=='=')
{
tokentable[total_len].type=$DENG;
tokentable[total_len++].ch=ch;
}
else if(ch=='I')
{
shuru>>buf;
if(strcmp(buf,"F")==0)
tokentable[total_len].type=$IF;
tokentable[total_len++].ch=0;
}
else if(ch=='i')
{
shuru>>buf;
if(strcmp(buf,"f")==0)
tokentable[total_len].type=$if;
tokentable[total_len++].ch=0;
}
else if(ch=='T')
{
shuru>>buf;
if(strcmp(buf,"HEN")==0)
tokentable[total_len].type=$THEN;
tokentable[total_len++].ch='$';
}
else if(ch=='t')
{
shuru>>buf;
if(strcmp(buf,"hen")==0)
tokentable[total_len].type=$then;
tokentable[total_len++].ch='$';
}
else if(ch>='a' && ch<='z')
{
tokentable[total_len].type=$ID;
tokentable[total_len++].ch=ch;
}
else if(ch>='0' && ch<='9')
{
tokentable[total_len].type=$NUM;
tokentable[total_len++].ch=ch;
}
}
token_index=0;
return true;
}
void S()
{
if((cur.type==$ID)||(cur.type==$NUM))
{
char a[8],b[8],c[8];
cout<<"S->"<<cur.ch;
*a=cur.ch;
*(a+1)='\0';
if(!nexttoken())
cout<<"There is only one operand"<<endl;
if((cur.type!=$DENG)&&(cur.type!=$DAYU)&&(cur.type!=$XIAOYU))
cout<<"Lack of operator"<<endl;
cout<<cur.ch;
*c=cur.ch;
*(c+1)='\0';
if(!nexttoken())
cout<<"Lack of operand"<<endl;
if((cur.type!=$ID)&&(cur.type!=$NUM))
cout<<"Lack of operand"<<endl;
cout<<cur.ch<<endl;
*b=cur.ch;
*(b+1)='\0';
push(c);
push(b);
push(a);
pop();
pop();
pop();
nexttoken();
}
else if((cur.type==$IF)||(cur.type==$if))
{
if(!nexttoken())
cout<<"There is nothing behind the keyword if"<<endl;
{
cout<<"S->if E then S "<<endl;
E();
if((cur.type==$then)||(cur.type==$THEN))
{ char d[8];
*d=cur.ch;
*(d+1)='\0';
push(d);
pop();
if(!nexttoken())
cout<<"There is nothing behind the keyword then"<<endl;
S();
}
else
cout<<"Lack of then"<<endl;
}
}
}
void E()
{
if((cur.type==$ID)||(cur.type==$NUM))
{
char a[8],b[8],c[8];
*a=cur.ch;
*(a+1)='\0';
cout<<"E->"<<cur.ch;
if(!nexttoken())
cout<<"There is nothing before E"<<endl;
if((cur.type!=$DENG)&&(cur.type!=$DAYU)&&(cur.type!=$XIAOYU))
cout<<"Lack of operator"<<endl;
cout<<cur.ch;
*c=cur.ch;
*(c+1)='\0';
if(!nexttoken())
cout<<"Lack of operand"<<endl;
if((cur.type!=$ID)&&(cur.type!=$NUM))
cout<<"Lack of operand"<<endl;
cout<<cur.ch<<endl;
*b=cur.ch;
*(b+1)='\0';
push(c); //让操作符进栈
push(b); //让第二个操作数进栈
push(a); //让第一个操作数进栈
pop();
pop();
pop();
if(!nexttoken())
cout<<"Lack of then!"<<endl;
}
else
cout<<"E(),error!"<<endl;
}
void push(char arg[8])
{
strcpy(stack[pom].str,arg);
pom++;
}
void pop()
{
strcpy(buffer[inc].str,stack[pom-1].str);
inc++;
pom--;
}
bool nexttoken()
{
if(token_index>=total_len)
return false;
else
{
cur.type=tokentable[token_index].type;
cur.ch=tokentable[token_index].ch;
token_index++;
return true;
}
}
void print()
{
for(inc=0;inc<80;inc++)
cout<<buffer[inc].str;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -