編譯原理實(shí)驗(yàn)二語法分析器LL實(shí)現(xiàn)_第1頁
編譯原理實(shí)驗(yàn)二語法分析器LL實(shí)現(xiàn)_第2頁
編譯原理實(shí)驗(yàn)二語法分析器LL實(shí)現(xiàn)_第3頁
編譯原理實(shí)驗(yàn)二語法分析器LL實(shí)現(xiàn)_第4頁
編譯原理實(shí)驗(yàn)二語法分析器LL實(shí)現(xiàn)_第5頁
已閱讀5頁,還剩11頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、編譯原理程序設(shè)計(jì)實(shí)驗(yàn)報告表達(dá)式語法分析器的設(shè)計(jì)班級:計(jì)算機(jī)1306班姓名:張濤學(xué)號:實(shí)驗(yàn)?zāi)繕?biāo):用LL(1)分析法設(shè)計(jì)實(shí)現(xiàn)表達(dá)式語法分析器實(shí)驗(yàn)內(nèi)容:概要設(shè)計(jì):通過對實(shí)驗(yàn)一的此法分析器的程序稍加改造,使其能夠輸出正確的表達(dá)式的token序列。然后利用LL(1)分析法實(shí)現(xiàn)語法分析。數(shù)據(jù)結(jié)構(gòu):int op=0; /當(dāng)前判斷進(jìn)度char ch; /當(dāng)前字符char nowword10=; /當(dāng)前單詞char operate4=+,-,*,/; /運(yùn)算符char bound2=(,); /界符struct Token int code; char ch10; /Token定義struct Token to

2、kenlist50; /Token數(shù)組struct Token tokentemp; /臨時Token變量struct Stack /分析棧定義 char *base; char *top; int stacksize;分析表及流程圖關(guān)鍵函數(shù):int IsLetter(char ch) /判斷ch是否為字母int IsDigit(char ch) /判斷ch是否為數(shù)字int Iskey(char *string) /判斷是否為關(guān)鍵字int Isbound(char ch) /判斷是否為界符int Isboundnum(char ch) /給出界符所在token值int init(STack *

3、s) /棧初始化int pop(STack *s,char *ch) /彈棧操作int push(STack *s,char ch) /壓棧操作void LL1(); /分析函數(shù)源程序代碼:(加入注釋)#include#include#include#include#include int op=0; /當(dāng)前判斷進(jìn)度char ch; /當(dāng)前字符char nowword10=; /當(dāng)前單詞char operate4=+,-,*,/; /運(yùn)算符char bound2=(,); /界符struct Token int code; char ch10; /Token定義struct Token tok

4、enlist50; /Token數(shù)組struct Token tokentemp; /臨時Token變量struct Stack /分析棧定義 char *base; char *top; int stacksize;typedef struct Stack STack;int init(STack *s) /棧初始化 (*s).base=(char*)malloc(100*sizeof(char); if(!(*s).base) exit(0); (*s).top=(*s).base; (*s).stacksize=100; printf(初始化棧n); return 0;int pop(S

5、Tack *s,char *ch) /彈棧操作 if(*s).top=(*s).base) printf(彈棧失敗n); return 0; (*s).top-; *ch=*(*s).top); printf(%c,*ch); return 1;int push(STack *s,char ch) /壓棧操作 if(*s).top-(*s).base=(*s).stacksize) (*s).base=(char*)realloc(*s).base,(*s).stacksize+10)*sizeof(char); if(!(*s).base) exit(0); (*s).top=(*s).ba

6、se+(*s).stacksize; (*s).stacksize+=10; *(*s).top=ch; *(*s).top+; return 1;void LL1();int IsLetter(char ch) /判斷ch是否為字母 int i;for(i=0;i=a&ch=A&ch=Z)return 1; return 0; int IsDigit(char ch) /判斷ch是否為數(shù)字 int i; for(i=0;i=0&ch=9) return 1; return 0; int Isbound(char ch) /判斷是否為界符 int i; for(i=0;i2;i+) if(ch

7、=boundi) return i+1; return 0;int Isoperate(char ch) /判斷是否為運(yùn)算符 int i; for(i=0;i4;i+) if(ch=operatei) return i+3; return 0;int main()FILE *fp;int q=0,m=0;char sour200= ;printf(請將源文件置于以下位置并按以下方式命名:F:2.txtn);if(fp=fopen(F:2.txt,r)=NULL) printf(文件未找到!n); else while(!feof(fp) if(isspace(ch=fgetc(fp); els

8、e sourq=ch; q+; int p=0; printf(輸入句子為:n); for(p;p=q;p+) printf(%c,sourp); printf(n); int state=0,nowlen=0; BOOLEAN OK=TRUE,ERR=FALSE; int i,flagpoint=0; for(i=0;iq;i+) if(souri=#) tokenlistm.code=#; switch(state) case 0: ch=souri; if(Isbound(ch) if(ERR) printf(無法識別n); ERR=FALSE; OK=TRUE; else if(!OK

9、) printf(標(biāo)識符n,nowword); tokentemp.code=10; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; state=4; else if(IsDigit(ch) if(OK) memset(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; state=3; OK=FALSE; break; else nowwordnowlen=ch; nowlen+; else if(IsLetter(ch) if(OK) mem

10、set(nowword,0,strlen(nowword); nowlen=0; nowwordnowlen=ch; nowlen+; OK=FALSE; else nowwordnowlen=ch; nowlen+; else if(Isoperate(ch) if(!OK) printf(標(biāo)識符n,nowword); tokentemp.code=10; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; OK=TRUE; printf(運(yùn)算符n,Isoperate(ch),ch); tokentemp.code=Isoperate(ch

11、); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; break; case 3: if(IsLetter(ch) printf(錯誤n); nowwordnowlen=ch; nowlen+; ERR=FALSE; state=0; break; if(IsDigit(ch=souri) nowwordnowlen=ch; nowlen+; else if(souri=.&flagpoint=0) flagpoint=1; nowwordnowlen=ch; nowlen+; else printf(數(shù)字n,nowword); i-; state=0

12、; OK=TRUE; tokentemp.code=20; tokentemp.ch10=nowword10; tokenlistm=tokentemp; m+; break; case 4: i-; printf(界符n,Isbound(ch),ch); tokentemp.code=Isbound(ch); tokentemp.ch10=ch; tokenlistm=tokentemp; m+; state=0; OK=TRUE; break; printf(tokenlist值為%dn,m); int t=0; tokenlistm+1.code=r; m+; for(t;tm;t+)

13、printf(tokenlist%d值為%dn,t,tokenlistt.code); LL1(); printf(tokenlist值為%dn,m); if(op+1=m) printf(OK!); else printf(WRONG!); return 0;void LL1() STack s; init(&s); push(&s,#); push(&s,E); char ch; int flag=1; do pop(&s,&ch); printf(輸出棧頂為%cn,ch); printf(輸出棧頂為%dn,ch); printf(當(dāng)前p值為%dn,op); if(ch=()|(ch=)|

14、(ch=+)|(ch=-)|(ch=*)|(ch=/)|(ch=10)|(ch=20) if(tokenlistop.code=1|tokenlistop.code=20|tokenlistop.code=10|tokenlistop.code=2|tokenlistop.code=3|tokenlistop.code=4|tokenlistop.code=5|tokenlistop.code=6) op+; else printf(WRONG!); exit(0); else if(ch=#) if(tokenlistop.code=0) flag=0; else printf(WRONG!

15、); exit(0); else if(ch=E) printf(進(jìn)入En); if(tokenlistop.code=10|tokenlistop.code=20|tokenlistop.code=1) push(&s,R); printf(將R壓入棧n); push(&s,T); else if(ch=R) printf(進(jìn)入Rn); if(tokenlistop.code=3|tokenlistop.code=4) push(&s,R); push(&s,T); printf(將T壓入棧n); push(&s,+); if(tokenlistop.code=2|tokenlistop.c

16、ode=0) else if(ch=T) printf(進(jìn)入Tn); if(tokenlistop.code=10|tokenlistop.code=20|tokenlistop.code=1) push(&s,Y); push(&s,F); else if(ch=Y) printf(進(jìn)入Yn); if(tokenlistop.code=5|tokenlistop.code=6) push(&s,Y); push(&s,F); push(&s,*); else if(tokenlistop.code=3|tokenlistop.code=2|tokenlistop.code=0|tokenlistop.code=4) else if(ch=F) printf(進(jìn)入Fn); if(tokenlistop.code=1

溫馨提示

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

最新文檔

評論

0/150

提交評論