?? tjyj.cpp
字號:
#include<iostream.h>
#include<fstream.h>
#include<string.h>
#define $ASSIGN 249
#define $IF 250
#define $THEN 251
#define $ELSE 252
#define $GREAT 253
#define $ID 254
//有效字符串: IF,ELSE,THEN, a,b,x,>,=
/*文法:
S->id=id | if E then S else S
E->id < id
*/
typedef struct Token
{
int type;
char ch;
}Token;
typedef enum{JUMP,JG,ASSIGN,END}OpKind;
typedef struct
{
int label; //標號
OpKind op;
char par1,par2;
union
{
char result;
int address;
};
}Quad; //四元式
#define MAX_TOKEN 256 //Token表大小
#define MAX_QUAD 256 //四元式數組大小
Token tokentable[MAX_TOKEN];
Quad quad[MAX_QUAD];
int token_index; //token表索引
int total_len; //token表有效長度
int quad_len; //四元式表有效長度
int quad_index; //四元式索引
int label;
ifstream ins;
bool init(char filename[255]);
bool cifa();
void ERROR();
void S(int,int);
void E(int,int,int);
bool nexttoken();
int newlabel();
void yuyi();
void printQuad();
void main()
{
cout<<"請輸入文件名:";
char fname[200];
cin>>fname;
if(!init(fname))
return;
if(!cifa())
return;
char ch;
while(1)
{
if(ins.eof())
break;
ins>>ch;
}
cout<<"詞法分析結果:"<<endl;
nexttoken();
cout<<endl;
cout<<"使用的產生式:"<<endl;
yuyi();
cout<<endl;
cout<<"產生的四元式:"<<endl;
printQuad();
}
int newlabel()
{
return label++;
}
bool init(char filename[255])
{
token_index=0;
total_len=0;
quad_len=0;
quad_index=0;
label=0;
ins.open(filename,ios::nocreate | ios::in);
if(ins.fail())
{
cout<<"打開文件出錯!"<<endl;
return false;
}
return true;
}
bool cifa()
{
char buf[16];
char ch;
while(1)
{
ins>>ch;
if(ins.fail())
break;
if(ch=='I')
{
ins>>buf;
if(strcmp(buf,"F")==0)
tokentable[total_len++].type=$IF;
}
else if(ch=='T')
{
ins>>buf;
if(strcmp(buf,"HEN")==0)
tokentable[total_len++].type=$THEN;
}
else if(ch=='E')
{
ins>>buf;
if(strcmp(buf,"LSE")==0)
tokentable[total_len++].type=$ELSE;
}
else if(ch=='>')
{
tokentable[total_len++].type=$GREAT;
}
else if(ch=='=')
{
tokentable[total_len++].type=$ASSIGN;
}
else if(ch=='x' || (ch>='a' && ch<='z'))
{
tokentable[total_len].type=$ID;
tokentable[total_len++].ch=ch;
}
}
return true;
}
#define AD_RESULT(nlabel,nop,npar1,npar2,nresult) {quad[quad_len].label=nlabel; quad[quad_len].op=nop; quad[quad_len].par1=npar1; quad[quad_len].par2=npar2; quad[quad_len].result=nresult; quad_len++; }
#define AD_ADDRESS(nlabel,nop,npar1,npar2,naddress) { quad[quad_len].label=nlabel; quad[quad_len].op=nop; quad[quad_len].par1=npar1; quad[quad_len].par2=npar2; quad[quad_len].address=naddress; quad_len++; }
Token cur;
bool nexttoken()
{
if(token_index>=total_len)
return false;
cur.type=tokentable[token_index].type;
cur.ch=tokentable[token_index].ch;
token_index++;
return true;
}
void ERROR(char str[20])
{
cout<<"error! "<<str<<endl;
}
void S(int begin,int next)
{
if(cur.type==$ID)
{
char a,b;
cout<<"S->"<<cur.ch;
a=cur.ch;
if(!nexttoken())
ERROR("S");
if(cur.type!=$ASSIGN)
ERROR("S");
cout<<"=";
if(!nexttoken())
ERROR("S");
if(cur.type!=$ID)
ERROR("S");
cout<<cur.ch<<endl;
b=cur.ch;
AD_RESULT(begin,ASSIGN,b,0,a);
AD_ADDRESS(-1,JUMP,0,0,next);
nexttoken();
}
else if(cur.type==$IF)
{
if(!nexttoken())
ERROR("S");
{
cout<<"S->if E then S else S"<<endl;
int etrue=newlabel();
int efalse=newlabel();
E(begin,etrue,efalse);
if(cur.type==$THEN)
{
if(!nexttoken())
ERROR("S");
S(etrue,next);
if(cur.type==$ELSE)
{
if(!nexttoken())
ERROR("S");
S(efalse,next);
}
else
{
ERROR("S");
}
}
else
ERROR("S");
}
}
}
void E(int begin,int etrue,int efalse)
{
if(cur.type==$ID)
{
char a,b;
a=cur.ch;
cout<<"E->"<<cur.ch<<">";
if(!nexttoken())
ERROR("E()");
if(cur.type!=$GREAT)
ERROR("E()");
if(!nexttoken())
ERROR("E()");
if(cur.type!=$ID)
ERROR("E()");
cout<<cur.ch<<endl;
b=cur.ch;
AD_ADDRESS(begin,JG,a,b,etrue);
AD_ADDRESS(-1,JUMP,0,0,efalse);
if(!nexttoken())
ERROR("E()");
}
else
ERROR("E()");
}
void yuyi()
{
S(-1,1000);
AD_RESULT(1000,END,0,0,'-');
}
void printQuad()
{
for(int i=0;i<quad_len;i++)
{
if(quad[i].label>-1)
cout<<"L"<<quad[i].label<<":";
else cout<<" ";
if(quad[i].op==JG)
{
cout<<"J>,"<<quad[i].par1<<","<<quad[i].par2<<",L"<<quad[i].address<<endl;
}
else if(quad[i].op==JUMP)
{
cout<<"J ,"<<"-,-,L"<<quad[i].address<<endl;
}
else if(quad[i].op==ASSIGN)
{
cout<<"= ,"<<quad[i].par1<<",-,"<<quad[i].result<<endl;
}
else if(quad[i].op==END)
{
cout<<"-,-,-,-"<<endl;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -