版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年成都紡織高等??茖W(xué)校單招綜合素質(zhì)筆試模擬試題帶答案解析
- 2026年廣州體育職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測試備考試題帶答案解析
- 2026年保定理工學(xué)院單招職業(yè)技能考試備考題庫帶答案解析
- 2026年阜陽幼兒師范高等??茖W(xué)校高職單招職業(yè)適應(yīng)性測試模擬試題有答案解析
- 土地承包協(xié)議2025年生態(tài)保護(hù)條款
- 2026年河北工業(yè)職業(yè)技術(shù)大學(xué)單招綜合素質(zhì)考試模擬試題帶答案解析
- 2026年撫順職業(yè)技術(shù)學(xué)院單招綜合素質(zhì)筆試參考題庫帶答案解析
- 投資協(xié)議(2025年投資回報)
- 投資合作協(xié)議(2025年退出機(jī)制)
- 2026年鄂爾多斯職業(yè)學(xué)院高職單招職業(yè)適應(yīng)性測試模擬試題有答案解析
- 現(xiàn)場應(yīng)急處置方案
- 2025年1月新疆普通高中學(xué)業(yè)水平考試物理試卷
- 二手房定金協(xié)議合同
- 2025-2026學(xué)年北師大版二年級數(shù)學(xué)上冊期末測試題(含答案)
- 2026年上半年新疆中小學(xué)教師資格考試(筆試)備考題庫(真題匯編)
- 2025-2026學(xué)年度第一學(xué)期期末測試三年級語文試卷
- 爐渣資源化處理技術(shù)方案
- 江蘇省2025年普通高中學(xué)業(yè)水平合格性考試數(shù)學(xué)試卷(含答案)
- 廣東省廣州市越秀區(qū)2025年七年級上學(xué)期期末考試數(shù)學(xué)試卷附答案
- 大學(xué)計(jì)算機(jī)教程-計(jì)算與人工智能導(dǎo)論(第4版)課件 第5章 數(shù)據(jù)庫與信息系統(tǒng)
- 九上歷史“資本主義”期末考點(diǎn)押題
評論
0/150
提交評論