編譯原理實驗 編譯器 綜合報告(附源代碼)_第1頁
編譯原理實驗 編譯器 綜合報告(附源代碼)_第2頁
編譯原理實驗 編譯器 綜合報告(附源代碼)_第3頁
編譯原理實驗 編譯器 綜合報告(附源代碼)_第4頁
編譯原理實驗 編譯器 綜合報告(附源代碼)_第5頁
已閱讀5頁,還剩14頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、編譯原理 編譯器綜合實驗-工程精品 神刀公子一 實驗背景編譯器就是將“一種語言(通常為高級語言)”翻譯為“另一種語言(通常為低級語言)”的程序。一個現(xiàn)代編譯器的主要工作流程:源代碼 (source code)  預處理器 (preprocessor)  編譯器 (compiler)  目標代碼 (object code)  鏈接器(Linker) 可執(zhí)行程序 (executables)高級計算機語言便于人編寫,閱讀交流,維護。機器語言是計算機能直接解讀、運行的。編譯器將匯編或高級計算機語言源程序(Sour

2、ce program)作為輸入,翻譯成目標語言(Target language)機器代碼的等價程序。源代碼一般為高級語言 (High-level language), 如Pascal、C、C+、Java、漢語編程等或匯編語言,而目標則是機器語言的目標代碼(Object code),有時也稱作機器代碼(Machine code)。對于C#、VB等高級語言而言,此時編譯器完成的功能是把源碼(SourceCode)編譯成通用中間語言(MSIL/CIL)的字節(jié)碼(ByteCode)。最后運行的時候通過通用語言運行庫的轉換,編程最終可以被CPU直接計算的機器碼(NativeCode)。二 算法設計典型的

3、編譯器輸出是由包含入口點的名字和地址, 以及外部調用(到不在這個目標文件中的函數(shù)調用)的機器代碼所組成的目標文件。一組目標文件,不必是同一編譯器產生,但使用的編譯器必需采用同樣的輸出格式,可以鏈接在一起并生成可以由用戶直接執(zhí)行的EXE,詞法分析程序 à 語法分析程序 à 語義分析程序 à編譯器。不斷完善,不斷改進。漸變的過程。函數(shù)。void scanner(); /掃描void lrparser(); void staBlock(int *nChain); /語句塊void staString(int *nChain); /語句串void sta(int *nCh

4、ain); /語句void fuzhi(); /賦值語句void tiaojian(int *nChain); /條件語句void xunhuan(); /循環(huán)語句char* E(); /Expresiion表達式char* T(); /Term項char* F(); /Factor因子char *newTemp(); /自動生成臨時變量void backpatch(int p,int t); /回填int merge(int p1,int p2); /合并p1和p2void emit(char *res,char *num1,char *op,char *num2); /生成四元式截圖說明:

5、綜合輸入:(賦值,循環(huán),條件。結合,自己定義即可)源代碼:/*編譯器*/*Erin*/*軟件工程0801班*/*HUST*/*#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>char prog80; /存放所有輸入字符 char token8; /存放詞組 char ch; /單個字符 int syn,p,m,n,i; /syn:種別編碼 double sum; int count; int isSignal; /是否帶正負號(0不帶,1負號,2正號)int

6、 isError;int isDecimal; /是否是小數(shù) double decimal; /小數(shù) int isExp; /是否是指數(shù) int index; /指數(shù)冪 int isNegative; /是否帶負號 double temp; int temp2;int repeat; /是否連續(xù)出現(xiàn)+,-int nextq;int kk; /臨時變量的標號int ntc,nfc,nnc,nnb,nna;char *rwtab9="main","int","float","double","char&quo

7、t;,"if","else","do","while" structchar result10; /字符串(字符數(shù)組)char arg110;char opera10;char arg210;fourCom20; /結構體數(shù)組void scanner(); /掃描void lrparser(); void staBlock(int *nChain); /語句塊void staString(int *nChain); /語句串void sta(int *nChain); /語句void fuzhi(); /賦值語句v

8、oid tiaojian(int *nChain); /條件語句void xunhuan(); /循環(huán)語句char* E(); /Expresiion表達式char* T(); /Term項char* F(); /Factor因子char *newTemp(); /自動生成臨時變量void backpatch(int p,int t); /回填int merge(int p1,int p2); /合并p1和p2void emit(char *res,char *num1,char *op,char *num2); /生成四元式void main()p=0;count=0;isDecimal=0

9、;index=0;repeat=0;kk=0;printf("nPlease input your source string:n");doch=getchar();progp+=ch;while(ch!='#');p=0;isError=0;scanner();lrparser();for(i=1;i<nextq;i+) /循環(huán)輸出四元式printf("n%dt",i);printf("(%5s %5s %5s t%5s )n",fourComi.arg1,fourComi.opera,fourComi.arg

10、2,fourComi.result);void lrparser()int nChain;nfc=ntc=1;nextq=1;if(syn=1) /mainscanner();if(syn=26) /(scanner();if(syn=27) /)scanner();staBlock(&nChain);elseprintf("缺少右括號n");else printf("缺少左括號n");elseprintf("缺少mainn");/<語句塊> := ''<語句串>''vo

11、id staBlock(int *nChain) /語句塊if(syn=28) /scanner();staString(nChain);/backpatch(*nChain,nextq);if(syn=29) /scanner(); /讀下一個elseprintf("缺少號n");elseprintf("缺少號n");/<語句串>:=<語句><語句>void staString(int *nChain) /語句串sta(nChain);backpatch(*nChain,nextq);while(syn=31) /;

12、scanner();sta(nChain);/backpatch(*nChain,nextq-1);void sta(int *nChain) /語句if(syn=10)fuzhi();/*nChain=0;else if(syn=6) /iftiaojian(nChain);else if(syn=8) /doxunhuan();/<條件語句>->if(<條件>)<語句塊>void tiaojian(int *nChain)char res10,num110,num210,op10;int nChainTemp;/<條件>-><

13、;表達式><關系運算符><表達式>if(syn=6) /ifscanner();/strcpy(num1,E();if(syn=26) /(scanner();strcpy(num1,E();if(syn<=37)&&(syn>=32) switch(syn)case 32:strcpy(op,">");break;case 33:strcpy(op,">=");break;case 34:strcpy(op,"<");break;case 35:strcpy

14、(op,"<=");break;case 36:strcpy(op,"=");break;case 37:strcpy(op,"!=");break;default:printf("error");scanner();strcpy(num2,E();strcat(num1,op);strcat(num1,num2);/nfc=nextq+1;ntc=nextq; /記住if語句位置emit("0","if",num1,"goto"); nfc=nex

15、tq; /if中表達式為假emit("0","","","goto");/第一個0已回填backpatch(ntc,nextq); /ntc鏈接的所有四元式都回填nextqif(syn=27) /)scanner();staBlock(&nChainTemp); /語句塊*nChain=merge(nChainTemp,nfc);/<循環(huán)語句>:=do <語句塊>while <條件>void xunhuan()char res10,num110,num210,op10;i

16、nt nChainTemp;if(syn=8) /donnc=nextq; /記住if語句位置,emit之后nextq就變了/emit("0","if",num1,"goto"); scanner();staBlock(&nChainTemp); /語句塊if(syn=9) /whilescanner();if(syn=26) /(scanner();strcpy(num1,E();if(syn<=37)&&(syn>=32) switch(syn)case 32:strcpy(op,"&

17、gt;");break;case 33:strcpy(op,">=");break;case 34:strcpy(op,"<");break;case 35:strcpy(op,"<=");break;case 36:strcpy(op,"=");break;case 37:strcpy(op,"!=");break;default:printf("error");scanner();strcpy(num2,E();strcat(num1,op);

18、strcat(num1,num2);nnb=nextq;emit("0","if",num1,"goto"); backpatch(nnb,nnc);nna=nextq;emit("0","","","goto");backpatch(nna,nextq);if(syn=27) /)scanner();void fuzhi() /賦值語句只有1個操作數(shù)char res10,num10; /num操作數(shù)if(syn=10) /字符串strcpy(res,to

19、ken); /結果scanner();if(syn=21) /=scanner();strcpy(num,E();emit(res,num,"=","");elseprintf("缺少=號n");char* E() /Expression表達式char *res,*num1,*op,*num2;res=(char *)malloc(10);num1=(char *)malloc(10);op=(char *)malloc(10);num2=(char *)malloc(10);strcpy(num1,T();while(syn=22)

20、|(syn=23) /+ -if(syn=22) /+strcpy(op,"+");elsestrcpy(op,"-");scanner();strcpy(num2,T();strcpy(res,newTemp();emit(res,num1,op,num2);strcpy(num1,res);return num1;char* T() /Term項char *res,*num1,*op,*num2;res=(char *)malloc(10);num1=(char *)malloc(10);op=(char *)malloc(10);num2=(cha

21、r *)malloc(10);strcpy(num1,F();while(syn=24)|(syn=25) /* /if(syn=24) strcpy(op,"*");elsestrcpy(op,"/");scanner();strcpy(num2,F();strcpy(res,newTemp();emit(res,num1,op,num2);strcpy(num1,res);return num1;char* F() /Factor因子char *res;res=(char *)malloc(10);if(syn=10) /字符串strcpy(res,

22、token);scanner();else if(syn=20) /二進制數(shù)itoa(int)sum,res,10); /整數(shù)轉換為字符串scanner();else if(syn=26) /(scanner();res=E();if(syn=27) /)scanner();else isError=1;elseisError=1;return res;char *newTemp()char *p;char varTemp10;p=(char *)malloc(10);kk+;itoa(kk,varTemp,10);strcpy(p+1,varTemp);p0='T'retur

23、n p;/將p所鏈接的每個四元式的第四個分量都回填tvoid backpatch(int p,int t) int w,circle=p;while(circle) /circle不為0的時候w=atoi(fourComcircle.result); /四元式circle第四分量內容/strcpy(fourComcircle.result,t); /把t填進四元式circle的第四分量sprintf(fourComcircle.result,"%d",t);circle=w; /w記錄的是鏈條上下一個四元式,移動!return;int merge(int p1,int p2

24、) /合并p1和p2char circle,nResult;if(p2=0)nResult=p1;elsenResult=circle=p2;while(atoi(fourComcircle.result) /四元式第四個分量不為0circle=atoi(fourComcircle.result); /strcpy(fourComcircle.result,p1);sprintf(fourComcircle.result,"%s",p1);/目的是用p1的值覆蓋0return nResult; /p2是頭,p1覆蓋0,接在p2后邊void emit(char *res,ch

25、ar *num1,char *op,char *num2)strcpy(fourComnextq.result,res);strcpy(fourComnextq.arg1,num1);strcpy(fourComnextq.opera,op);strcpy(fourComnextq.arg2,num2);nextq+;void scanner() sum=0; decimal=0; m=0; for(n=0;n<8;n+) tokenn=NULL; ch=progp+; /從prog中讀出一個字符到ch中 while(ch=' '|ch='n') /跳過空

26、字符(無效輸入) ch=progp+; if(ch>='a')&&(ch<='z')|(ch>='A')&&(ch<='Z') /ch是字母字符 while(ch>='a')&&(ch<='z')|(ch>='A')&&(ch<='Z')|(ch>='0')&&(ch<='9') tokenm+=c

27、h; /ch=>token ch=progp+; /讀下一個字符 tokenm+='0' p-; /回退一格 syn=10; /標識符 /如果是"begin","if","then","while","do","end"標識符中的一個 for(n=0;n<9;n+) if(strcmp(token,rwtabn)=0) syn=n+1; break; else if(ch>='0')&&(ch<='

28、;9') IsNum: if(isSignal=1) /tokenm+='-' while(ch>='0')&&(ch<='9') sum=sum*10+ch-'0' /ch中數(shù)字本身是當做字符存放的 ch=progp+; if(ch='.') isDecimal=1; ch=progp+; count=0; /之前忘了清零,123.123+123.123#兩個浮點數(shù)就無法識別 while(ch>='0')&&(ch<='9&#

29、39;) /pow(x,y)計算x的y次冪 temp=(ch-'0')*pow(0.1,+count); decimal=decimal+temp; /AddToDec(); ch=progp+; sum=sum+decimal; if(ch='e'|ch='E') isExp=1; ch=progp+; if(ch='-') isNegative=1; ch=progp+; while(ch>='0')&&(ch<='9') /指數(shù) index=index*10+ch-

30、'0' ch=progp+; /10的冪 /123e3代表123*10(3) /sum=sum*pow(10,index);是錯誤的 if(isNegative) sum=sum*pow(0.1,index); else sum=sum*pow(10,index); if(isSignal=1)sum=-sum;isSignal=0; p-; syn=20; else switch(ch) case '<': m=0; tokenm+=ch; ch=progp+; if(ch='=') syn=35; tokenm+=ch; else sy

31、n=34; p-; break; case '>': m=0; tokenm+=ch; ch=progp+; if(ch='=') syn=33; tokenm+=ch; else syn=32; p-; break; case '=': m=0; tokenm+=ch; ch=progp+; if(ch='=') syn=36; tokenm+=ch; else syn=21; p-; break; case '+':temp2=progp;tokenm+=ch;if(temp2>='0')&&(temp2<='9')&&(repeat=1)isSignal=2;ch=progp+;repeat=0;goto IsN

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論