編譯原理end

#include<bits/stdc++.h>
using namespace std;const int max_word = 505;
//關鍵字 
const char keyWord[13][20] = {"main","if","else","do","while","for","switch",
"case","int","double","float","long","void"};//單詞表 
struct Word{char value[20];//單詞值 int type;//單詞種類int line;//行號 
}w[max_word]; //四元式
struct si_Yuan{string op;//運算符string s1;string s2;string res;//結果 
}sy[105];
//文件流 
FILE *fin,*fout;
int line = 1;
char token[12];//存放單詞 
int flag = 0;//判斷是否獲取字符 
int flag1 = 0,flag2;//flag1判斷是否項已經產生miss錯誤; 
int e = 0;//錯誤個數 
int sy_num = 0;//四元式的個數
string op,s1,ss1,s2,res,res1,res2,op2,op1; 
int op_num = 0,op_num1 = 0;//運算符的個數
int sy_id;//四元式的序號 string var[300];//已經聲明的變量 
int var_num = 0;//聲明變量的個數 int sent_id;//判斷if,while語句 
string equ[300];//= 
int a,b,c; 
int equ_num = 1; //=個數 
int cnt = 0,token_num = 0;//當前字符 
int row = 1;//當前行數 
char ch;
char ch1; int word_num = 0;//單詞的總個數 
int word_cnt = 0;
string temp;
char str_int[20];
stack<string>word_stack;//保存 
stack<string>op_stack;
//程序 
void S();
void P();//(){分程序} 
void P1();//分程序 
void A();//變量說明部分
void B();//語句部分
void B1();
int C();//變量說明
void D();//標識符表
void D1();
void E();//標識符
void F();//字母 
void G();//數字 
void H();//語句 
void I();//賦值語句 
void J();//條件語句 
void K();//循環語句 
int L();//關系運算符 
void M();//表達式 
void M1();
void N();//項 
void N1();
int O();//加法運算符 
int Q();//乘法運算符 
void R();//常量 
void S();//無符號整數 
void T();//數字序列 
void W();//復合語句 
void X();//條件 
void Y();//語句1 
void Z();//因子//構造四元式 
void siYuan(string op,string s1,string s2,string res)
{	sy[sy_num].op =op;sy[sy_num].s1=s1;sy[sy_num].s2=s2;sy[sy_num].res=res;sy_num++;
}void printFour()//輸出四元式 
{ofstream out("sy.txt");int i;for(i=0;i<sy_num;i++){cout<<i<<":\t("<<sy[i].op<<",\t"<<sy[i].s1<<",\t"<<sy[i].s2<<",\t"<<sy[i].res<<")"<<endl;out<<i<<":\t("<<sy[i].op<<",\t"<<sy[i].s1<<",\t"<<sy[i].s2<<",\t"<<sy[i].res<<")"<<endl;}
}
void error(string err)//報錯,定位到錯誤行 
{cout<<"第"<<w[word_cnt].line<<"行出錯!\t";cout<<err<<"\n";e++;
}//檢查變量是否被聲明
void is_say(string a)
{int i;for(i=1;i<=var_num;i++){if(a.compare(var[i])==0){break;}}if(i>var_num)  {cout<<"第 "<<w[word_cnt].line<<" 行出錯!\t";cout<<w[word_cnt].value<<" 沒有被聲明 !"<<endl;e++;}
}void init_token(){int i;for(i = 0;i < 12;i++){token[i] = NULL;}
}int judge_token(){init_token();if(flag == 0){ch = getc(fin);}flag = 1;while(ch == ' ' || ch == '\t' || ch == '\n'){if(ch == '\n'){row++;}ch=getc(fin);}token_num = 0;if((ch>='a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z')){//可能為標識符或者變量名 while((ch>='a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z') || (ch >= '0' && ch <= '9')){token[token_num++] = ch;ch = getc(fin);}token[token_num++] = '\0';for(int i = 0;i <13;i++){if(strcmp(token,keyWord[i]) == 0){//3為關鍵詞 return 3;}}//2為標識符 return 2;}//是數字 else if(ch >= '0' && ch <= '9'){while((ch >= '0'&& ch <= '9') || ch == '.'){token[token_num++] = ch;ch = getc(fin);}return 1;}else{token[token_num++] = ch;switch(ch){case '(': ch = getc(fin); return 16;case ')': ch = getc(fin); return 17;case '{': ch = getc(fin); return 33;case '}': ch = getc(fin); return 34;case '+':ch = getc(fin);if(ch == '+'){token[token_num++] = ch;ch = getc(fin); return 29;}else{return 18;}case '-':ch = getc(fin);if(ch == '-'){token[token_num++] = ch;ch = getc(fin); return 30;}else{return 19;}case '*':ch = getc(fin);if(ch == '/'){token[token_num++] = ch;ch = getc(fin); return 32;}else{return 20;}case '/':ch = getc(fin);if(ch == '*'){token[token_num++] = ch;ch = getc(fin); return 31;}else{ return 21;}//這里要重新編碼 case '=':ch = getc(fin);if(ch == '='){token[token_num++] = ch;ch = getc(fin); return 23;}else{return 22;}case '>':ch = getc(fin);if(ch == '='){token[token_num++] = ch;ch = getc(fin); return 24;}else{return 23;}case '<':ch = getc(fin);if(ch == '='){token[token_num++] = ch;ch = getc(fin); return 26;}else{return 25;}case ';': ch = getc(fin); return 27;case '"': ch = getc(fin); return 28;case '!':ch = getc(fin);if(ch == '='){token[token_num++] = ch;ch = getc(fin); return 37;}else{return 36;}case '#': ch = getc(fin); return -2;case ',': ch = getc(fin); return 35;case EOF: return -1;default: ch = getc(fin); return -10;}}
}void getWord(){int temp;while(1){temp = judge_token();if(temp==-1){break;}switch(temp){case -10://cout<<"第 "<<row<<" 行出現錯誤."<<endl;error("word has a mistake");w[word_num].type = -1;strcat(w[word_num].value," ");word_num++;break;case -1:return;default:w[word_num].type  = temp;w[word_num].line = row;strcpy(w[word_num].value,token);word_num++;//cout<<"<"<<temp<<","<<token<<">"<<endl;break;}}
}
//程序 
void S(){if(strcmp("main",w[word_cnt].value)){error("miss main");word_cnt--;}word_cnt++;if(strcmp("(",w[word_cnt].value)){error("miss (");word_cnt--;}word_cnt++;if(strcmp(")",w[word_cnt].value)){error("miss )");word_cnt--;}word_cnt++;if(strcmp("{",w[word_cnt].value)){error("miss {");word_cnt--;}word_cnt++;P();if(strcmp("}",w[word_cnt].value)){error("miss }");word_cnt--;}op = "#";s1 = "";s2 = "";res = "";siYuan(op,s1,s2,res);cout<<"There are "<<e<<" mistakes in total."<<endl;
}
//分程序 
void P(){A();//變量說明部分if(strcmp(";",w[word_cnt].value)){error("miss ;");word_cnt--;}word_cnt++;B();//語句部分 
}
//變量說明部分 
void A(){if(C()){var[++var_num] = w[word_cnt].value;D();}else{D();}
}
//變量說明 
int C(){if(strcmp(w[word_cnt].value,"int")){error("miss int");return 0;}word_cnt++;return 1;
}
//標識符表
void D(){if(w[word_cnt].type != 2){error("denoter has a error");}word_cnt++;D1();
}
//子標識符表 
void D1(){if(strcmp(w[word_cnt].value,";") == 0){return;}if(strcmp(w[word_cnt].value,",")){error("miss ,");word_cnt--;}word_cnt++;var[++var_num] = w[word_cnt].value;if(w[word_cnt].type!=2){error("denoter has a error");}word_cnt++;D1();
}
//語句部分 
void B(){H();B1();
}
//子語句部分 
void B1(){if(strcmp(w[word_cnt].value,";") == 0){word_cnt++;H();B1();}else if(strcmp(w[word_cnt].value,"}") == 0){return;}else{error("miss ;");}
}
//語句 
void H(){cout<<"H()"<<endl;cout<<w[word_cnt].value<<endl;if(w[word_cnt].type == 2){op_num = 0;is_say(w[word_cnt].value);word_stack.push(w[word_cnt].value);word_cnt++;I();}else if(strcmp(w[word_cnt].value,"if") == 0){op_num = 0;J();}else if(strcmp(w[word_cnt].value,"while") == 0){op_num = 0;a = sy_num;K();op = "go";s1 = "";s2 = "";stringstream sss;//int 轉 string sss<<a;sss>>res;siYuan(op,s1,s2,res);//修改while跳轉的序號stringstream sss2;sss2<<sy_num;sss2>>sy[sent_id].res;}else{error("extra fuhao"); }
}//賦值語句
void I(){if(strcmp(w[word_cnt].value,"=") == 0){word_cnt++;//word_stack.push(w[word_cnt].value); M();if(e > 0){return;}if(op_stack.size() > 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop();s1 = word_stack.top();word_stack.pop();res = word_stack.top();word_stack.pop();siYuan(op,s1,s2,res);}else{s1 = word_stack.top();word_stack.pop();res = word_stack.top();word_stack.pop();siYuan("=",s1,"",res);}}else{error("miss = ");word_cnt--; }
} 
//if語句 
void J(){cout<<"J()"<<endl;if(strcmp(w[word_cnt].value,"if")){error("miss if");word_cnt--;		}word_cnt++;if(strcmp(w[word_cnt].value,"(")){error("miss (");word_cnt--;}word_cnt++; //word_stack.push(w[word_cnt].value);X();//條件 sent_id = sy_num;if(op_stack.size() > 0 && e == 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop();s1 = word_stack.top();word_stack.pop();siYuan(op,s1,s2,res);}if(strcmp(w[word_cnt].value,")")){error("miss )");word_cnt--;}word_cnt++;//四元式置為空op = "";s1 = "";s2 = "";res = "";Y();//語句1stringstream ss;ss<<sy_num+1;ss>>sy[sent_id].res;sent_id = sy_num;op = "go";s1 = "";s2 = "";res = "";siYuan(op,s1,s2,res);if(strcmp(w[word_cnt].value,"else")){error("miss else");word_cnt--;}word_cnt++;Y();stringstream ss2;ss2<<sy_num;ss2>>sy[sent_id].res;
}//while語句
void K(){cout<<"K()"<<endl;if(strcmp(w[word_cnt].value,"while")){error("miss while");word_cnt--;}word_cnt++;if(strcmp(w[word_cnt].value,"(")){error("miss (");word_cnt--;}word_cnt++;//word_stack.push(w[word_cnt].value);X();sent_id = sy_num;if(op_stack.size()>0 && e == 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop();s1 = word_stack.top();word_stack.pop();siYuan(op,s1,s2,res);}//cout<<w[word_cnt].value<<endl;if(strcmp(w[word_cnt].value,")")){error("miss )");word_cnt--;}word_cnt++;if(strcmp(w[word_cnt].value,"do")){error("miss do");word_cnt--;}word_cnt++;Y();	
}void X(){cout<<"X()"<<endl;M();if(op_stack.size() > 0 && e == 0){op = op_stack.top();op_stack.pop(); s2 = word_stack.top();word_stack.pop(); s1 = word_stack.top();word_stack.pop();stringstream ss;ss << op_num;res = "t"+ss.str();word_stack.push(res);siYuan(op,s1,s2,res);}if(strcmp(w[word_cnt].value,">") == 0){op = "<=";}else if(strcmp(w[word_cnt].value,">=") == 0){op = "<";}else if(strcmp(w[word_cnt].value,"<") == 0){op = ">=";}else if(strcmp(w[word_cnt].value,"<=") == 0){op = ">";}else if(strcmp(w[word_cnt].value,"==") == 0){op = "!=";}else if(strcmp(w[word_cnt].value,"!=") == 0){op = "==";}//cout<<w.value<<"  "<<w.type<<endl;if(!L()){error("miss relation operator");word_cnt--;}op_stack.push(op);word_cnt++;//word_stack.push(w[word_cnt].value);//sent_id = sy_num;M();
}
//表達式 
void M(){cout<<"M()"<<endl;N();M1();
}
//子表達式 
void M1(){cout<<"M1()"<<endl;cout<<w[word_cnt].value<<endl;if(strcmp(w[word_cnt].value,";") == 0 || strcmp(w[word_cnt].value,")") == 0 || L() || strcmp(w[word_cnt].value,"}") == 0 || strcmp(w[word_cnt].value,"else") == 0){return;}else if(!O()){error("miss + or -");//word_cnt--;}else{op_stack.push(w[word_cnt].value);word_cnt++;//word_stack.push(w[word_cnt].value);op_num++; //getWord();//strcpy(b[b_num++].bds,new_w.value);N();M1();if(op_stack.size() > 1 && e == 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop(); s1 = word_stack.top();word_stack.pop();stringstream ss;ss << op_num;res = "t"+ss.str();word_stack.push(res);siYuan(op,s1,s2,res);}}
}//項 ,消除左遞歸 
void N(){cout<<"N()"<<endl;Z();N1();
}
void N1(){cout<<"N1()"<<endl;cout<<w[word_cnt].value<<endl;//后根符號集,關系運算符  ;  ) if(strcmp(w[word_cnt].value,";") == 0 || strcmp(w[word_cnt].value,")") == 0 || strcmp(w[word_cnt].value,"}") == 0 || strcmp(w[word_cnt].value,"else") == 0 || L() || O()){return;}else if(!Q()){error("miss * or /");//分號出錯轉到語句 //word_cnt--;}//word_cnt++;else{op_num++;op_stack.push(w[word_cnt].value);word_cnt++;//word_stack.push(w[word_cnt].value);Z();N1();if(op_stack.size() > 1 && e == 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop(); s1 = word_stack.top();word_stack.pop();stringstream ss;ss << op_num;res = "t"+ss.str();word_stack.push(res);siYuan(op,s1,s2,res);}}
}
//因子
void Z(){//cout<<w.value<<"  "<<w.type<<endl;//非數字或標識符 cout<<"Z()"<<endl;if(w[word_cnt].type!=1 && w[word_cnt].type!=2 &&strcmp(w[word_cnt].value,"(")){error("expression has a error");return;}//標識符if(w[word_cnt].type == 1||w[word_cnt].type == 2){if(w[word_cnt].type == 2){is_say(w[word_cnt].value);}word_stack.push(w[word_cnt].value);word_cnt++;//equ[equ_num++] = w.value;return;}else if(strcmp(w[word_cnt].value,"(") == 0){word_cnt++;//word_stack.push(w[word_cnt].value);M();if(e == 0){op = op_stack.top();op_stack.pop();s2 = word_stack.top();word_stack.pop(); s1 = word_stack.top();word_stack.pop();stringstream ss;ss << op_num;res = "t"+ss.str();word_stack.push(res);siYuan(op,s1,s2,res);}if(strcmp(w[word_cnt].value,")") == 0){word_cnt++;return;}else{error("miss )");}}return;
}//語句1 
void Y(){cout<<"Y()"<<endl; if(strcmp(w[word_cnt].value,"{") == 0){word_cnt++;B();//cout<<w.value<<endl;if(strcmp(w[word_cnt].value,"}")){error("miss }");}word_cnt++;return; }else{H();//語句return; }
}//關系運算符
int L(){if(((w[word_cnt].type > 22 && w[word_cnt].type <= 26) || w[word_cnt].type == 37)){return 1;}return 0;
} //加法運算符 
int O(){if(w[word_cnt].type == 18 || w[word_cnt].type == 19){return 1;}return 0;
}
//乘法運算符 
int Q(){if(w[word_cnt].type == 20 || w[word_cnt].type == 21){return 1;}return 0;
}int main(){fin = fopen("compiler.txt","r");getWord();S();if(e==0){printFour();}else{cout<<"\nSorry,can not generate siYuan."<<endl;}return 0;
}
/**/

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/444601.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/444601.shtml
英文地址,請注明出處:http://en.pswp.cn/news/444601.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

做了nginx反向代理之后常見問題匯總

1.客戶端無緣無故的主動斷開和服務器的連接&#xff0c;如圖&#xff1a; 服務器端收到了FIN包&#xff0c;查看了nginx 的配置有個選項&#xff1a;proxy_timeout選項 設置為30s。 注意&#xff1a;“proxy_timeout”這個參數可以寫在stream節點下&#xff0c;所有server都生效…

在GoogPlay上發布的包Facebook登錄失敗提示簽名問題

在googplay提審的包發布后,發現Facebook登錄功能異常,提示如下: 意識到可能是hashkey出問題了,但是之前測試都是好的,原來是上傳包到googlePlay后有個二次簽名,會修改hashkey的,所以需要在Facebook后臺添加下重新簽名的hashkey。 基本簽名信息在Google Play 上都能查看…

JDK和Spring中的設計模式

JDK中的設計模式&#xff08;17&#xff09; 創建型 1&#xff09;工廠方法 Collection.iterator() 由具體的聚集類來確定使用哪一個Iterator 2&#xff09;單例模式 Runtime.getRuntime() 3&#xff09;建造者模式 StringBuilder 4&#xff09;原型模式 Java中的Clon…

解決蘋果發布正式環境后支付拉不起來或獲取商品列表為空問題

最近在海外蘋果商店發布新游戲,經歷了一個操蛋的兩天: 產品在提交testflight沙盒環境下是可以獲取到蘋果商品列表,并且測試支付可以拉起并到賬,等到我通過TF轉發布到正式環境后,游戲點擊游戲內商店獲取商品列表就為空,更別提拉起支付了。 最開始先檢查了蘋果開發者后臺的…

根據當前docker容器生成鏡像提交到遠端服務器

docker commit 4d6883e5fa21 gaoke/koa_ios docker push gaoke/koa_ios 然后在遠端可看到

2019我做成的事情

1、ccpc河北金 這個省賽可能是退役賽了&#xff0c;因為下半年寫項目&#xff0c;明年實習&#xff0c;沒機會參加省賽、區預賽了。 2019.5大二的時候參加的&#xff0c;記得敲了個區間dp&#xff0c;大模擬&#xff0c;隊友數學沒搞出來&#xff0c;有一個搜索也是膽子不夠大…

TCP: request_sock_TCP: Possible SYN flooding on port 80. Sending cookies. Check SNMP counters

最近老發現服務器丟包嚴重,想通過ssh登錄查看原因,但是仍然失敗,后來重啟云服務器后通過單用戶模式進入查看系統日志: TCP: request_sock_TCP: Possible SYN flooding on port 80. Sending cookies. Check SNMP counters 系統的內存,CPU資源是沒問題的,足夠當前的業務量…

記一次北美游戲服務器冬令時夏令時切換引發的時間問題

由于在運行的某SLG游戲在國內蘋果商店多次拿到推薦,我們打算把它做到海外,部署按照全球唯一服的架構來部署,運維同事將集群中的各個模塊選擇部署在美國芝加哥的機房。上線一段時間后客服反饋平時凌晨3點重置玩家每日數據的時間變成了4點,往后推遲了1小時,當時懷疑是不是出…

Redis你不得不探索的11個問題

1. 說說Redis基本數據類型有哪些吧 字符串&#xff1a;redis沒有直接使用C語言傳統的字符串表示&#xff0c;而是自己實現的叫做簡單動態字符串SDS的抽象類型。C語言的字符串不記錄自身的長度信息&#xff0c;而SDS則保存了長度信息&#xff0c;這樣將獲取字符串長度的時間由O(…

(一)深入淺出TCPIP之理解TCP報文格式和交互流程

目錄 1.引入TCP: 1.1 TCP用戶代碼 2. TCP數據報文格式 3 TCP棧及socket的初始化

leetcode85. 最大矩形

給定一個僅包含 0 和 1 的二維二進制矩陣&#xff0c;找出只包含 1 的最大矩形&#xff0c;并返回其面積。 示例: 輸入: [ ["1","0","1","0","0"], ["1","0","1","1","…

(二)深入淺出TCPIP之再識TCP,理解TCP三次握手(上)

目錄 1.三次握手 1.1 三次握手過程 1.2 TCP連接狀態 1.3 TCP狀態遷移路線分析 1.4 查看TCP狀態命令

(三)深入淺出TCPIP之再識TCP,理解TCP四次揮手(上)

目錄 1.TCP四次揮手過程 2.揮手連環發問 專欄其他文章: 理論篇: (一)深入淺出TCPIP之理解TCP報文格式和交互流程 (二)深入淺出TCPIP之再識TCP,理解TCP三次握手(上) (三)深入淺出TCPIP之再識TCP,理解TCP四次揮手(上) (四)深入淺出TCPIP之TCP三次握手和四次揮手…

(四)深入淺出TCPIP之TCP三次握手和四次揮手(下)的抓包分析

目錄 1. 通過netstat來分析服務器和客戶端的TCP狀態 2.通過tcpdump抓包分析服務器和客戶端的TCP狀態 2.1 語法

(六)深入淺出TCPIP之TCP擁塞控制

目錄 什么是網絡擁塞 如何避免擁塞 擁塞點 避免擁塞 慢啟動算法 算法思想

(五)深入淺出TCPIP之TCP流量控制

目錄 TCP流量控制 滑動窗口 固定窗口和滑動窗口 如何告知發送方窗口大小 滑動窗口細節

(八)深入淺出TCPIP之TCP長連接與短連接詳解

目錄 通信方式 連接方式 1.長連接 1.1服務器中的長連接 1.2 長連接的維護

(十)深入淺出TCPIP之網絡阻塞和非阻塞

專欄其他文章: 理論篇: (一)深入淺出TCPIP之理解TCP報文格式和交互流程 (二)深入淺出TCPIP之再識TCP,理解TCP三次握手(上) (三)深入淺出TCPIP之再識TCP,理解TCP四次揮手(上) (四)深入淺出TCPIP之TCP三次握手和四次揮手(下)的抓包分析 (五)深入淺出TCPIP之TCP流…

(九)深入淺出TCPIP之網絡同步異步

目錄 專欄其他文章: 同步和異步 同步與異步的例子 使用場景 代碼示例 專欄其他文章: </

(十二)深入淺出TCPIP之Nagle算法

未完待續 專欄其他文章: 理論篇: (一)深入淺出TCPIP之理解TCP報文格式和交互流程 (二)深入淺出TCPIP之再識TCP,理解TCP三次握手(上) (三)深入淺出TCPIP之再識TCP,理解TCP四次揮手(上) (四)深入淺出TCPIP之TCP三次握手和四次揮手(下)的抓包分析 (五)深入淺出T…