-
大小: 940KB文件類型: .rar金幣: 1下載: 0 次發(fā)布日期: 2021-07-27
- 語言: 其他
- 標簽:
資源簡介
編譯原理的第二個實驗,賦值語句的翻譯程序設計,包括詞法分析,文件輸出存盤,含有.cpp .dsw .txt輸出文件等等
代碼片段和文件信息
#include
#include
#include
#include
using?namespace?std;
typedef?struct?SqStack{//定義棧的數(shù)據(jù)結(jié)構(gòu)??????
?char???*base;??????
????char???*top;??????
?}SqStack;????????????????????????????????
??struct?Query{//數(shù)據(jù)結(jié)點
char?thename[45];
};
struct?QueNode{//鏈表
Query?query;
QueNode?*pnext;
};
QueNode*Addshu(QueNode*phchar*str){
???QueNode*p1;
????p1=new?QueNode();
strcpy(p1->query.thenamestr);
????????p1->pnext=ph;
ph=p1;
return?ph;
}
void?Push(SqStack?&Schar?e){?//?將元素e插入到棧S中,成為新的棧頂元素???????
??*S.top++=e;
}????
??
char?Pop(SqStack?&Schar?&ch){?//字符型取棧頂元素并刪除之?????
????if(S.top==S.base){??????
????????ch=NULL;?????
????}??????
????else?ch=*--S.top;??????
?????return?ch;?
}??
?char?Gettop(SqStack?&Schar?&e){//字符型取棧頂元素
if(S.base==S.top)e=NULL;
?else?e=*(S.top-1);
return?e;
}
int?in(char?c){//判斷是否為運算符
??char?su[10]={‘+‘‘-‘‘*‘‘/‘‘(‘‘)‘‘;‘‘=‘};
?for(int?i=0;i<8;i++)
?if(c==su[i])return?1;
????return?0;
}
char?precede(char?x1char?x2){//優(yōu)先級判斷
char?c1c2c;
c1=x1;c2=x2;
if(c1==‘(‘){
if(c2==‘)‘)
c=‘=‘;
else
c=‘<‘;
}
else?if(c1==‘)‘)
c=‘>‘;
else?if(c1==‘;‘){
?????????if(c2==‘;‘)
c=‘=‘;
else
c=‘<‘;
}
else{
int?ab;
????switch(c1){
???case?‘+‘:
???????case?‘-‘:a=2;break;
???????case?‘*‘:
???????case?‘/‘:a=3;break;
???????case?‘(‘:a=4;break;
???????????case?‘)‘:a=1;break;
???????case?‘;‘:a=-2;break;
???case?‘=‘:a=-1;break;
}
????switch(c2){?
???case?‘=‘:a=-1;break;
???????case?‘+‘:
???????case?‘-‘:b=2;break;
???????case?‘*‘:
???????case?‘/‘:b=3;break;
???????case?‘(‘:b=4;break;
???????case?‘)‘:b=1;break;
???????case?‘;‘:b=-2;break;
}
if(a-b>=0)c=‘>‘;
if(a-b<0)c=‘<‘;
}
return?c;
}
void?main(){
cout<<“賦值語句翻譯成四元式!!“< string?inaona;
cerr<<“請輸入源文件名:“< cin>>ina;
ifstream?fin;
fin.open(ina.c_str()ios::in);
if(!fin){cerr<<“open?error!“< cerr<<“請輸入要生成的文件名:“< cin>>ona;
ofstream?quedata;
quedata.open(ona.c_str()ios::out);
if(!quedata){cerr<<“open?error!“< char?cethetax;
char?a[20]=““b[20]=““;
int?s=0i=1;
char?chs[20]=““che[2]=““kon[20]=““;
QueNode*ph=NULL;
char?str[50];
????SqStack?stack;
????stack.base=stack.top=&str[0];?
Push(stack‘;‘);
????fin.get(c);
while(c!=‘;‘||Gettop(stacke)!=‘;‘){??
?if(!in(c)){?
??????che[0]=c;
????????if((c>=‘A‘&&c<=‘Z‘)||(c>=‘a(chǎn)‘&&c<=‘z‘)||c==‘_‘)i=0;
?strcat(chsche);fin.get(c);
if(in(c)){
??if(i==0)quedata<<“?<“<“< ??else?quedata<<““<“< ph=Addshu(phchs);strcpy(chskon);
????????????if(c!=‘;‘)quedata<<““<“< else?quedata<<““<“< }
}
????else{
???char?a2=s+‘0‘;
char?a1[]={‘(‘‘t‘a(chǎn)2‘)‘‘\0‘};i=1;
??????? switch(precede(Gettop(stacke)c)){
case?‘<‘:
if(c==‘(‘||c==‘)‘)quedata<<““<“< Push(stackc);fin.get(c);break;
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????74752??2010-11-21?13:11??Debug\vc60.idb
?????文件?????110592??2010-11-21?13:11??Debug\vc60.pdb
?????文件?????565333??2010-11-21?13:11??Debug\賦值語句的翻譯程序設計.exe
?????文件?????814816??2010-11-21?13:11??Debug\賦值語句的翻譯程序設計.ilk
?????文件?????303197??2010-11-21?13:11??Debug\賦值語句的翻譯程序設計.obj
?????文件????2186100??2010-11-16?18:08??Debug\賦值語句的翻譯程序設計.pch
?????文件????1131520??2010-11-21?13:11??Debug\賦值語句的翻譯程序設計.pdb
?????文件??????49152??2010-11-16?23:07??~VC134.tmp
?????文件??????????0??2010-11-16?23:07??~VC135.tmp
?????文件???????1153??2010-11-16?20:27??answer.txt
?????文件?????????32??2010-11-21?12:48??test.txt
?????文件???????3697??2010-11-18?13:21??themain.txt
?????文件??????50176??2010-11-21?13:27??賦值語句的翻譯程序設計.ncb
?????文件???????1269??2010-11-21?13:11??賦值語句的翻譯程序設計.plg
?????文件???????3544??2010-11-21?13:11??賦值語句的翻譯程序設計.cpp
?????文件???????3595??2009-12-01?11:27??賦值語句的翻譯程序設計.dsp
?????文件????????552??2009-12-01?11:27??賦值語句的翻譯程序設計.dsw
?????文件??????48640??2010-11-21?13:27??賦值語句的翻譯程序設計.opt
?????文件????????284??2010-11-21?13:11??test_1.txt
?????目錄??????????0??2010-11-21?13:11??Debug
-----------?---------??----------?-----??----
??????????????5348404????????????????????20
評論
共有 條評論