版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、編譯原理實(shí)驗(yàn)報(bào)告學(xué) 號(hào):姓 名:提交日期:2013.12.26成 績(jī):東北大學(xué)秦皇島分?!緦?shí)驗(yàn)編號(hào)】實(shí)驗(yàn)一【實(shí)驗(yàn)名稱(chēng)】詞法分析程序的設(shè)計(jì)與實(shí)現(xiàn)【實(shí)驗(yàn)?zāi)康暮鸵蟆吭O(shè)計(jì)、編制、調(diào)試一個(gè)具體的詞法分析程序,加深對(duì)詞法分析原理的理解?!緦?shí)驗(yàn)內(nèi)容】通過(guò)對(duì)PL/0詞法分析程序(GETSYM)的分析,并在此基礎(chǔ)上按照附錄A中給出的PL/0語(yǔ)言的語(yǔ)法描述,編寫(xiě)一個(gè)PL/0語(yǔ)言的詞法分析程序。此程序應(yīng)具有如下功能:輸入為字符串(待進(jìn)行詞法分析的源程序),輸出為單詞串,即由(單詞、類(lèi)別)所組成的二元組序列。有一定檢查錯(cuò)誤的能力,例如發(fā)現(xiàn)2A這類(lèi)不能作為單詞的字符串?!緦?shí)驗(yàn)環(huán)境】Windows PC機(jī),任何版本的C
2、語(yǔ)言?!緦?shí)驗(yàn)代碼】#include #include #include #include #include #include using namespace std;bool isLetter(char ch) if (ch=A & ch=a & ch=0 & ch=9) return true; else return false;bool isP(char ch) if(ch=+|ch=*|ch=-|ch=/) return true; /ch=:|ch=,|ch=|ch=;|ch=(|ch=) else return false;bool isJ(char ch) if(ch=,|ch=
3、;|ch=.|ch=(|ch=)|ch=|ch=|ch=|ch=:|ch=|ch=|ch=|ch=#) return true; / else return false;bool isBlank(char ch) if(ch= |ch=t) return true; else return false;int main() string src,ste,s; char ch0,ch,ch12; char ktt4820=and,begin,const,div,do,else,end,function,if,integer, not,or,procedure,program,read,real,
4、then,type,var,while,write,標(biāo)識(shí)符,無(wú)符號(hào)數(shù), ,;,:,.,(,),.,+,-,+,-,*,/,=,=,:=,#; int pos=0; FILE *fp; fp=fopen(pl0.txt,r); ch0=fgetc(fp); while(ch0!=EOF) /if(ch0!=t)src+=ch0; src+=ch0; ch0=fgetc(fp); src+=#; coutsrcendl; ch=srcpos+; ste= ; for(int j=0;j47;j+)coutj kttj ; if(j%002=1) coutendl; cout詞法分析:n; whil
5、e(ch!=#) char str20; if(ch!=n) if(isDigit(ch) /判斷常數(shù) int i=0; while(isDigit(ch)|ch=.) stri+=ch; /i+; ch=srcpos+; stri=0; ste=ste+|+22; coutstr; continue; else if(isLetter(ch) /判斷字符 int i=0,j; while(isLetter(ch)|isDigit(ch) stri+=ch; /i+; ch=srcpos+; stri=0; for(j=0;j21;j+) /判斷是否關(guān)鍵字 int t=strcmp(str,k
6、ttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; if(j=21)ste=ste+|+21; / cout ; coutstr; continue; else if(isP(ch) /判斷是否運(yùn)算符 int i=0,j; stri+=ch; stri=0; for(j=34;j38;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; coutstr; ch=srcpos+; continue; else if(isJ(
7、ch) /判斷是否界符 int i=0,j; while(isJ(ch) stri+=ch; ch=srcpos+; stri=0; for(j=23;j47;j+) int t=strcmp(str,kttj); if(t=0) stringstream ss; ste+=|; ssste;ssste; break; coutstr; continue; else if(isBlank(ch) coutch; ch=srcpos+; continue; else coutsteendl; ste= ; ch=srcpos+; return 0;【測(cè)試的源文件】 Pl0.txt CONST A
8、=10; VAR B,C; 2BPROCEDURE P; VAR D; 2E PROCEDURE Q; VAR X; BEGIN READ(X);D:=X;WHILE X0 DO CALL P; END; BEGIN WRITE(D); CALL Q; END; BEGIN CALL P; END. 實(shí)驗(yàn)結(jié)果【實(shí)驗(yàn)編號(hào)】實(shí)驗(yàn)二【實(shí)驗(yàn)名稱(chēng)】詞法分析程序自動(dòng)構(gòu)造器LEX的使用【實(shí)驗(yàn)內(nèi)容】(1)認(rèn)真閱讀由清華大學(xué)出版社出版的教材編譯原理附錄B中對(duì)LEX的介紹,掌握該工具的基本理論和LEX源程序的基本結(jié)構(gòu),了解編寫(xiě)LEX源程序的基本方法以及相關(guān)格式。(2)收集LEX的相關(guān)資料,并通過(guò)研究這些資料,掌握
9、LEX的正則表達(dá)式的詳細(xì)規(guī)則和寫(xiě)法。學(xué)會(huì)在LEX源程序中加入相應(yīng)規(guī)則匹配后可能的動(dòng)作。并在此基礎(chǔ)上完成一篇論文,詳細(xì)論述LEX的功能和用法用途。(3)掌握某種版本的LEX的用法,能夠用LEX語(yǔ)言編寫(xiě)簡(jiǎn)單的LEX程序并能用LEX生成相應(yīng)的輸出文件。(4)用LEX實(shí)現(xiàn)PL/0語(yǔ)言的部分或全部詞法分析的自動(dòng)構(gòu)造程序的生成,即編寫(xiě)識(shí)別PL/0語(yǔ)言(或附錄A中的語(yǔ)法圖所描述的語(yǔ)言)單詞的LEX源程序?!緦?shí)驗(yàn)環(huán)境】詞法分析器生成工具:flex;編程語(yǔ)言:C;調(diào)試環(huán)境:VC+【實(shí)驗(yàn)步驟】#include #include #include typedef struct Node char *s;int t;
10、struct Node *next;string;string * keyword,* ident,* integer,* boolean,* operator,* symbol;void deal(string * head);%digit 0-9number digit+letter a-zA-Zidentifier letter(digit|letter|_)*newline nwhitespace t+%if |then |while |do |read |write |call |begin |end |const |var |procedure |odd deal(keyword)
11、;true |false deal(boolean);identifier deal(ident);number deal(integer);+ |- |* |/ |= |# | | |= |:= |= deal(operator); | |( |) |, |; deal(symbol);. printf(%s,yytext);%void deal(string * head)string *p,*q;/p=(string *)malloc(sizeof(string);p=head-next;/if(p=NULL)exit(0);/p=head;if(p=NULL)q=(string *)m
12、alloc(sizeof(string);if(q=NULL)exit(0);q-s=(char *)malloc(strlen( yytext) + 1 );strcpy(q-s,yytext);q-t=1;q-next=NULL;head-next=q;else p = head;while(p-next!=NULL&strcmp(p-next-s,yytext) p = p-next;if(p-next=NULL)q=(string *)malloc(sizeof(string);if(q=NULL)exit(0);q-s=(char *)malloc(strlen( yytext) +
13、 1 );strcpy(q-s,yytext);q-t=1;q-next=NULL;p-next=q;else p-next-t+;void print(string *head,FILE *fout)string *p;/p=(string *)malloc(sizeof(string);/if(p=NULL)exit(0);p=head-next;while(p!=NULL)fprintf(fout,n%15s%5d time(s)n,p-s,p-t);p= p-next;int yywrap()return 1;main(int argc,char *argy)FILE *fout;+a
14、rgy;-argc;if(argc0)yyin=fopen(argy0,r);elseyyin=stdin;freopen(other.out,w,stdout);fout=fopen(lg.out,w);/* keyword,* ident,* integer,* boolean,* operator,* symbol;keyword=(string *)malloc(sizeof(string);if(keyword=NULL)exit(0);keyword-next=NULL;ident=(string *)malloc(sizeof(string);if(ident=NULL)exit
15、(0);ident-next=NULL;integer=(string *)malloc(sizeof(string);if(integer=NULL)exit(0);integer-next=NULL;boolean=(string *)malloc(sizeof(string);if(boolean=NULL)exit(0);boolean-next=NULL;operator=(string *)malloc(sizeof(string);if(operator=NULL)exit(0);operator-next=NULL;symbol=(string *)malloc(sizeof(
16、string);if(symbol=NULL)exit(0);symbol-next=NULL;yylex();fprintf(fout,keyword:n);print(keyword,fout);fprintf(fout,identifier:n);print(ident,fout);fprintf(fout,integer:n);print(integer,fout);fprintf(fout,boolean:n);print(boolean,fout);fprintf(fout,operator:n);print(operator,fout);fprintf(fout,keysymbo
17、l:n);print(symbol,fout);return 0;3. 程序說(shuō)明:1) deal(string *head)適用于對(duì)掃描的各種詞類(lèi)的相關(guān)操作:string 是定義的一個(gè)結(jié)構(gòu)體類(lèi)型,其成員有三:第一個(gè)是char *型字符型指針s,用來(lái)存儲(chǔ)各個(gè)詞;第二個(gè)是int型數(shù)據(jù)t,記錄相同詞出現(xiàn)的次數(shù)。head 是上面結(jié)構(gòu)體鏈表的頭指針,不存儲(chǔ)任何數(shù)據(jù)。deal(string *head)根據(jù)掃描到的詞種類(lèi)不同,對(duì)不同鏈表進(jìn)行操作,如果掃描到的是head 類(lèi)型的詞,其操作如下:如果head類(lèi)鏈表為空或者不存在與當(dāng)前掃描的詞匹配的結(jié)點(diǎn),則創(chuàng)建一個(gè)新的結(jié)點(diǎn),并把當(dāng)前掃描的詞賦值給新結(jié)點(diǎn)的char
18、 *變量s,同時(shí)把詞出現(xiàn)的次數(shù)(即t)置1,然后把它插入鏈表中;反之,把相應(yīng)結(jié)點(diǎn)的t加1。2)print(string *head,F(xiàn)ILE *fout):按照鏈表順序從頭到尾把head類(lèi)的詞和它們出現(xiàn)的次數(shù)輸出到fout所指的文件中。4執(zhí)行程序 執(zhí)行得到的C程序,輸入一段PL0源程序如下: 詞法分析后得到的結(jié)果如下: keyword:var 3 time(s) procedure 1 time(s) begin 3 time(s) end 3 time(s) call 1 time(s)identifier: a 1 time(s) b 1 time(s) test 2 time(s) t
19、2 time(s) s 2 time(s)integer: 1 1 time(s)boolean: true 1 time(s)operator: := 2 time(s)keysymbol: , 1 time(s) ; 9 time(s) 【實(shí)驗(yàn)編號(hào)】實(shí)驗(yàn)三【實(shí)驗(yàn)?zāi)康暮鸵蟆俊緦?shí)驗(yàn)名稱(chēng)】設(shè)計(jì)、編制、調(diào)試一個(gè)典型的語(yǔ)法分析程序,實(shí)現(xiàn)對(duì)如下文法的遞歸子程序語(yǔ)法分析,進(jìn)一步掌握常用的語(yǔ)法分析方法。遞歸子程序分析器的設(shè)計(jì)與實(shí)現(xiàn)【實(shí)驗(yàn)內(nèi)容】設(shè)計(jì)、編制、調(diào)試一個(gè)典型的語(yǔ)法分析程序,實(shí)現(xiàn)對(duì)如下文法的遞歸子程序語(yǔ)法分析,進(jìn)一步掌握常用的語(yǔ)法分析方法?!緦?shí)驗(yàn)步驟】(1)本分析程序所分析的文法如下:GE:EeB
20、aAAa|bAcBBdEd|aCCe|dC(2)針對(duì)上述文法編寫(xiě)一遞歸子程序分析程序,該程序的輸入是任意符號(hào)串,輸出是本次輸入的符號(hào)串是否是該文法的句子的結(jié)論。【實(shí)驗(yàn)環(huán)境】Windows PC機(jī),任何版本的C語(yǔ)言。實(shí)驗(yàn)代碼#include#include#include#includechar a10;int s=0;void E();void A();void B();void C(); void E() if(as=e) s+; B(); if(as=a) s+; A(); void A() if(as=b) s+; A(); if(as=c) s+; B(); else if(as=a)
21、 s+; void B() if(as=d) s+; E(); if(as=d) s+; else if(as=a) s+; C(); void C() if(as=d) s+; C(); else if (as=e) s+; int main() While(1)printf(請(qǐng)輸入算數(shù)表達(dá)式(以#鍵結(jié)束):); scanf(%s,a); E(); if(as=#) printf(句子結(jié)構(gòu)正確n); else printf(分析失敗n); return 0; 實(shí)驗(yàn)結(jié)果 實(shí)驗(yàn)編號(hào) 實(shí)驗(yàn)四實(shí)驗(yàn)名稱(chēng) 簡(jiǎn)單優(yōu)先分析程序的設(shè)計(jì)與實(shí)現(xiàn)實(shí)驗(yàn)內(nèi)容 (1)本分析程序所分析的文法如下:GS:SbAbA(B|aB
22、Aa)(2)針對(duì)上述文法編寫(xiě)一簡(jiǎn)單優(yōu)先分析程序,該程序的輸入是任意符號(hào)串,輸出是本次輸入的符號(hào)串是否是該文法的句子的結(jié)論。(3)有能力的同學(xué)可以根據(jù)構(gòu)造簡(jiǎn)單優(yōu)先關(guān)系表的算法用程序來(lái)實(shí)現(xiàn)對(duì)文法的簡(jiǎn)單優(yōu)先關(guān)系表的構(gòu)造。實(shí)驗(yàn)代碼import java.util.HashMap;import java.util.Stack;import java.util.Vector;public class test public static String testStr=b(aa)b#;public static char c= testStr.toCharArray();public static char
23、 token=S,b,A,(,B,a,),#;public static Stack s=new Stack();public static HashMap map= new HashMap();public static char relation = new chartoken.lengthtoken.length;static for(int i=0;itoken.length;i+)map.put(tokeni, i);for(int i=0;itoken.length;i+)for(int j=0;j;relation12=;relation13=;relation15=;relat
24、ion21=;relation25=;relation32=;relation33=;relation34=;relation35=;relation45=;relation51=;relation55=;relation56=;relation61=;relation65=;relation70=;relation71=;public test()/* * param args */public static void main(String args) /shiyan4 s4= new shiyan4();s.push(#);tryint i=0;while(!s.isEmpty()char pre = s.pop();if(compare(pre,ci)/移進(jìn)s.push(pre);s.push(ci);else/規(guī)約s.push(pre);i-;statute();i+;if(s.isEmpty()System.out.println(成功);elseSystem.out.println(失敗);catch(Exception e)System.out.printl
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026甘肅倚核人力資源有限公司招聘筆試參考題庫(kù)及答案解析
- 2026廣東省公共衛(wèi)生醫(yī)學(xué)中心泗安院區(qū)招聘編外臨床工作人員3人筆試備考題庫(kù)及答案解析
- 2026年四川職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)傾向性考試題庫(kù)附答案
- 2026陜西省面向北京航空航天大學(xué)招錄選調(diào)生考試參考題庫(kù)附答案
- 2026年徽商職業(yè)學(xué)院?jiǎn)握新殬I(yè)傾向性考試模擬測(cè)試卷附答案
- 2026福建福州經(jīng)濟(jì)技術(shù)開(kāi)發(fā)區(qū)糧食收儲(chǔ)有限公司招聘2人筆試備考題庫(kù)及答案解析
- 2026浙江寧波舜瑞產(chǎn)業(yè)控股集團(tuán)有限公司招聘1人補(bǔ)充筆試參考題庫(kù)及答案解析
- 江投國(guó)華信豐發(fā)電有限責(zé)任公司公開(kāi)招聘勞務(wù)派遣制工作人員筆試備考試題及答案解析
- 2025河南商丘工學(xué)院教師招聘?jìng)淇碱}庫(kù)附答案
- 2026青海西寧國(guó)有企業(yè)招聘4人筆試參考題庫(kù)及答案解析
- 【MOOC】通信原理-北京交通大學(xué) 中國(guó)大學(xué)慕課MOOC答案
- 正規(guī)裝卸合同范本
- 科研設(shè)計(jì)及研究生論文撰寫(xiě)智慧樹(shù)知到期末考試答案章節(jié)答案2024年浙江中醫(yī)藥大學(xué)
- 2024年江蘇省普通高中學(xué)業(yè)水平測(cè)試小高考生物、地理、歷史、政治試卷及答案(綜合版)
- 土力學(xué)與地基基礎(chǔ)(課件)
- 精神分裂癥等精神病性障礙臨床路徑表單
- 提撈采油安全操作規(guī)程
- 管道安全檢查表
- DB3211-T 1048-2022 嬰幼兒日間照料托育機(jī)構(gòu)服務(wù)規(guī)范
- 電纜井砌筑工序報(bào)驗(yàn)單檢驗(yàn)批
- SB/T 11137-2015代駕經(jīng)營(yíng)服務(wù)規(guī)范
評(píng)論
0/150
提交評(píng)論