編譯原理實驗----詞法分析_第1頁
編譯原理實驗----詞法分析_第2頁
編譯原理實驗----詞法分析_第3頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、實驗一詞法分析器設計【實驗目的】1. 熟悉詞法分析的基本原理,詞法分析的過程以及詞法分析中要注意的問題。2. 復習髙級語言,進一步加強用高級語言來解決實際問題的能力。3通過完成詞法分析程序,了解詞法分析的過程?!緦嶒瀮热荨坑肅語言編寫一個PL/O詞法分析器,為語法語義分析提供單詞,使之能把輸入的字符串形 式的源程序分割成一個個單詞符號傳遞給語法語義分析,并把分析結果(基本字,運算符,標識 符,常數(shù)以及界符)輸出。【實驗步驟和要求】1. 要求繪出詞法分析過程的流程圖。2. 根據(jù)詞法分析的目的以及內容.確左完成分析過程所需模塊。3. 寫岀每個模塊的源代碼。/源程序如下:# i n cl u d e

2、 <io s t r e a m>#incl u de <cty p e .h># include < f s t ream>#incl u de <st r in g h>#includ e <ma 1 loc.h># d efine NU L L Mabc°us i ng n a mespa c e std;ifst r eam f p(nd: cifa. c pp,ios:i n );c h ar c c h ;char *key12=,rif Vel swhile M , 11 do H return M , Mbr

3、 e akM;rco n t inue,Hint M;,voi d M,Mmain M , Hc onstH :/ / 基本字cha r * border 10= M , 11 :11 V . T, T , T,八'"" ; 界符char *ari t hmetic 6=,+M , y * , M /M+”,;/算術運算符ch a r *rela t i on7 = VM =n, n >M , M> = M , H = = v! =M);/關系運算符ch a r * lab 1 e c o n s t 80 ;/標識符i nt c on s tnum=

4、4 0;int lablecons t n u m= 0 ;int se a rch(char sea r ch c har 5 i n t wordty p e)i n t i=0, t = 0 ;s w i tch (w o r dty p e)c ase 1:for (i= 0 ; i <=ll:i+)(if (str emp (ke y i,sc a rc h c har)= 0 )ret urn(i+l);r e t u rn(0);cas e 2:for (i=0; i<=9;i+)if (s t r c mp(bor der 訂,sear chch a r)=0)re

5、turn (i+1):ret u r n(0);cas e 3:for (i=0;i<=5;i+)i f (strcmp( a rithm e t i c i ,searchch a r )= 0 ) re tum(i+l);re t u m(0):c ase 4:for ( i = 0 ;iv=6:i+)i f (str c mp( r ela t i o ni,sea r chchar)=O) r eturn(i+l);retur n (0);ca s e 5:fo r (t=40; t <=constnum; t + + )i f ( s trcmp(searchc h a

6、r, 1 a b i e c o n s tt)=O) re t u rn(t+l);lableconstt-l= (ch a r *)mall o c(sizeof(s e archchar);strcpy ( I ableconst t-1,searchchar):c o nstnum+;ret u r n(t);case 6:for ( i = 0 : i<= 1 abl e con s tnum;i+)if (st r cmp(s e arc h c h ar, lablecon s t i )=0)rcturn(i+l);1 a b 1 ec o nst i-1 =( c ha

7、r *)malloc (s i ze o f(sea r c h c ha r );str c py ( I a b leco n s t i -lt s e a rchch a r);1 a bl e constnum+;return (i);def a ul t :coutv V 錯誤!";cha r alph a proces s (charch)int atype;i n t i=-l:char alpha t p 2 0:while ( ( i sa 1 ph a (ch) II (isd i g i t(ch)alp h atp+i=ch;f p g et( c h);a

8、lph a tpi+l=z 0:if (atype= s care h (al p h a(p 1 )c ou t«a 1 p ha t p«Ht t tH<<(atype- 1 )«endl:elseat y pe=s e ar c h(alp h a t p , 6 );co u t<< a 1 p hat p «H t tt°<< (a t ype-l)<<end 1;r e tu r n ( c h );char d i gitpr o ce s s(c h arch)inti =-1 :

9、c h ar digittp 20;int d ty p e :while (isd i g i t(ch )d i gi t tp+i=ch:fp g e t ( c h);digittpi+l=;dtype = s earch (digi 11 p ,5);cou t «digitt p «Ht t t M «(dty p e -40) << e ndl: retu r n(ch);ch a r o t h erpro c es s (c h ar c h )int i= 1;char othertp 20;int ot y pc.oty p et

10、p:othertp0=ch:o t h e rtpfif (o t y p e=search(o t h e r t p.3)fp. g et(ch);ot h e rtpl=ch;othertp 2=0 ;if ( o t y p etp =sea r ch(ot h crtp,3)co u t« o t h ert p <<u tt t " «(o t y p etp-l)<<en d 1: f p g e t ( c h): goto out;e Iseot h e rtp 1 =0:c o u t « o t hertp&

11、#171;u t ttH«( o ty p e-l)<<end 1 ; goto o ut:if (o t ype=se a rch(othert p ,4)f p g e t (ch);o t h e rtpl=ch;other t p 2=0:if (o t ype t p =se a rc h (othertp, 4)co u t <<o t hertp« " tt t H<<(otypetp-1 )«end 1 ; fp. get(ch); g oto out;elseothertpl=0*;c out

12、1; o th e r t p<< ” tttH«(oty p 1 )vv e ndl;got o ou t :i f (ch=,!1)f p g e t(ch);i f(ch = = ' 7 )co u t« * ! =(2,2)1V;fp.ge t (ch);go t o out;elsei f (o t y p e =sea r ch( o thertp.2)c o ut«oth e r t p «Ht t t M<<(o t y p e -1 )«e n d 1; fp.get( c h ):got o

13、out;)i f ( ( ch!n')&& (ch ! = '*)c o ut<<H錯誤!,字符非法,y v" tt tH«ch«endl;f p .get( c h);o ut: r e tu r n (ch);vo i d main()i nt i ;for (i= 0 : i v=50;i+)lablec o nst i=NU L L;if(! f p )cout«"文件打開錯誤! '«e ndl;els efp. g e t (c c h);while (! f p.e o

14、f()i f ( i salp h a (cch)c ch=a 1 pha p roc e ss( c c h);e Is e if (is d igit( c ch)cch=d i gitpr o cess (cc h );e I se c c h =othcrp r oc e s s (cch);cout« 成功1V;g e t c ha r (); c i f a cpp#i n elude<s t dio.h>voi d ma i n ()cou t «He 1 lo Wo rid";Z "E:Sf建文Debugl.exe*B5BI餌誤一字符祚法ttinclude0<0sCdio1錯誤!,字符非法h2>3void9main10<4>5<2錯誤!與符非法cout3<0<U鑄誤!.字符非袪Hello4World5錯誤| 字符非法9t>3咸功【實驗小結】通過這次實驗,我對編譯原理這門專業(yè)必修課有了進一步的深層次了解,把理論知識應用于 實驗中,也讓我重新熟悉C語言的相關內容,加深了對C語言知識的深化和用途的理解。實驗二LL (1)語法分析程序設計【實驗目

溫馨提示

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

評論

0/150

提交評論