無符號(hào)數(shù)的識(shí)別_第1頁
無符號(hào)數(shù)的識(shí)別_第2頁
無符號(hào)數(shù)的識(shí)別_第3頁
無符號(hào)數(shù)的識(shí)別_第4頁
無符號(hào)數(shù)的識(shí)別_第5頁
已閱讀5頁,還剩5頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

1、無符號(hào)數(shù)的詞法分析程序一、實(shí)驗(yàn)?zāi)康暮鸵螅?)初步掌握編譯原理的實(shí)驗(yàn)的技能;(2)驗(yàn)證所學(xué)理論、鞏固所學(xué)知識(shí)并加深理解。二、實(shí)驗(yàn)內(nèi)容和原理內(nèi)容:掌握詞法分析的基本思想,并用高級(jí)語言編寫無符號(hào)數(shù)(包括整數(shù)和實(shí)數(shù))的詞法分析程序。要求:從鍵盤中輸入一字符串(包括字母、數(shù)字等),編寫程序識(shí)別出其中的無符號(hào)數(shù)。無符號(hào)數(shù)的文法規(guī)則課定義如下: <無符號(hào)數(shù)> <無符號(hào)實(shí)數(shù)>|<無符號(hào)整數(shù)> <無符號(hào)實(shí)數(shù)> <無符號(hào)整數(shù)>.<數(shù)字串>E<比例因子> <比例因子> <有符號(hào)整數(shù)> <有符號(hào)整數(shù)&g

2、t; +|-<無符號(hào)整數(shù)> <無符號(hào)整數(shù)> <數(shù)字串> <數(shù)字串> <數(shù)字><數(shù)字> <數(shù)字> 0 1 2 3 4 5 6 7 8 9 本實(shí)驗(yàn)中我利用了狀態(tài)轉(zhuǎn)化圖的思想,下面是試驗(yàn)中用到構(gòu)造的狀態(tài)轉(zhuǎn)化圖:描述狀態(tài)機(jī)的代碼格式如下:int state = S0 ;while(1)Switch(state)case S0 : if(T0轉(zhuǎn)移條件滿足) 狀態(tài)轉(zhuǎn)移到滿足T0的次態(tài);操作;if(T1轉(zhuǎn)移條件滿足) 狀態(tài)轉(zhuǎn)移到滿足T1的次態(tài);操作;if(T2轉(zhuǎn)移條件滿足) 狀態(tài)轉(zhuǎn)移到滿足T2的次態(tài);操作;Break ; c

3、ase S1 : /插入S1狀態(tài)下的操作; break ; 實(shí)驗(yàn)代碼:/本程序主要實(shí)現(xiàn)實(shí)數(shù)的識(shí)別import java.io.BufferedReader ;import java.io.IOException ;import java.io.InputStreamReader ;public class RealNumberIdentified /*-成員變量的定義-*/private final int S_0 = 0 ; private final int S_1 = 1 ; private final int S_2 = 2 ; private final int S_3 = 3 ;

4、private final int S_4 = 4 ; private final int S_5 = 5 ; private final int S_6 = 6 ; private final int S_7 = 7 ; private char originNumber ; /用來記錄原始的數(shù)字串 private String resultNumber = new String( ) ; private int realnumberAmount = 0 ; /用來記錄識(shí)別出的實(shí)數(shù)的個(gè)數(shù) private int realnumberFlag = 0 ; /用于標(biāo)識(shí)該實(shí)數(shù)是正數(shù)還是負(fù)數(shù) pri

5、vate int eFlag = 0 ; /用來標(biāo)識(shí)該實(shí)數(shù)是不是指數(shù)實(shí)數(shù) private int pointFlag = 0 ; /用來標(biāo)識(shí)該實(shí)數(shù)是否是小數(shù) private int basePlusMinus = 1 ; /用來標(biāo)識(shí)底數(shù)的符號(hào) private int count = 0 ; /用來記錄已經(jīng)識(shí)別數(shù)字串的位數(shù) private int currentState = S_0 ; /用來記錄當(dāng)前所處于的狀態(tài) /*- -*/ /*-該方法用來初始化一些參數(shù)-*/ private void init( ) resultNumber = new String( ) ; basePlusMinu

6、s = 1 ; eFlag = 0 ; pointFlag = 0 ; /*-*/ /*-init( )方法用于去除實(shí)數(shù)串開始和結(jié)尾的無效的空格-*/ private void initOriginNumber( String str ) str = str.trim( ) ; str += '#' ; originNumber = str.toCharArray( ) ; /*-*/ /*-printResult()方法用來輸出識(shí)別的結(jié)果-*/ private void printResult( ) if( realnumberFlag = 0 ) System.out.pr

7、intln( "n識(shí)別結(jié)束,您所輸入的字符串中不包含任何實(shí)數(shù)!" ) ; else realnumberAmount + ; if( realnumberAmount = 1 ) System.out.println( "n識(shí)別結(jié)束,您所輸入的字符串中所包含的實(shí)數(shù)如下:" ) ; if( resultNumber.length( ) != 0 ) if( eFlag = 1 ) if( basePlusMinus = 1 ) System.out.println( "正指數(shù)實(shí)數(shù):" + Double.parseDouble( resu

8、ltNumber ) ) ; else System.out.println( "負(fù)指數(shù)實(shí)數(shù):" + Double.parseDouble( resultNumber ) ) ; else if( pointFlag = 1 ) System.out.println( " 小 數(shù) :" + Double.parseDouble( resultNumber ) ) ; else if( basePlusMinus = 1 ) System.out.println( " 正 整 數(shù):" + Long.parseLong( resultNu

9、mber ) ) ; else System.out.println( " 負(fù) 整 數(shù):" + Long.parseLong( resultNumber ) ) ; /*-*/ /*-識(shí)別該實(shí)數(shù)串的過程-*/ private void identifiedProcess( String str ) initOriginNumber( str ) ; while( count < originNumber.length ) switch( currentState ) case S_0 : if( originNumber count = '+' ) r

10、esultNumber = resultNumber + originNumber count ; currentState = S_1 ; else if( originNumber count = '-' ) resultNumber = resultNumber + originNumber count ;basePlusMinus = 0 ;currentState = S_1 ;else if( originNumber count >=48 && originNumber count <=57 )resultNumber = result

11、Number + originNumber count ; realnumberFlag = 1 ;currentState = S_2 ;else currentState = S_0 ; count + ; break ; case S_1 : if( originNumber count >=48 && originNumber count <=57 ) resultNumber = resultNumber + originNumber count ; realnumberFlag = 1 ; currentState = S_2 ; else result

12、Number = resultNumber.substring( 0 , resultNumber.length( ) - 1 ) ;currentState = S_0 ; count + ; break ; case S_2 : if( originNumber count = '.' ) resultNumber = resultNumber + originNumber count ;currentState = S_3 ; else if( originNumber count >=48 && originNumber count <=57

13、 ) resultNumber = resultNumber + originNumber count ;currentState = S_2 ; else if( originNumber count = 'E' | originNumber count = 'e' ) resultNumber = resultNumber + originNumber count ;currentState = S_5 ; else if( originNumber count = '+' | originNumber count = '-'

14、 )count - ;printResult( ) ;init( ) ;currentState = S_0 ; count + ; break ; case S_3 : if( originNumber count >=48 && originNumber count <=57 ) resultNumber = resultNumber + originNumber count ; pointFlag = 1 ;currentState = S_4 ; else resultNumber = resultNumber.substring( 0 , resultNu

15、mber.length( ) - 1 ) ; printResult( ) ; init( ) ; currentState = S_0 ; count + ; break ; case S_4 : if( originNumber count >=48 && originNumber count <=57 ) resultNumber = resultNumber + originNumber count ;currentState = S_4 ; else if( originNumber count = 'E' | originNumber c

16、ount = 'e' )resultNumber = resultNumber + originNumber count ;currentState = S_5 ;else if( originNumber count = '+' | originNumber count = '-' )count - ;printResult( ) ;init( ) ;currentState = S_0 ; count + ; break ; case S_5 : if( originNumber count = '+' ) resultNum

17、ber = resultNumber + originNumber count ; currentState = S_6 ; else if( originNumber count = '-' ) resultNumber = resultNumber + originNumber count ;currentState = S_6 ;elseif( originNumber count >= 49 && originNumber count <= 57 )resultNumber = resultNumber + originNumber coun

18、t ;eFlag = 1 ;currentState = S_7 ;elseresultNumber = resultNumber.substring( 0 , resultNumber.length( ) - 1 ) ; printResult( ) ; init( ) ; currentState = S_0 ; count - ; count + ; break ; case S_6 : if( originNumber count >= 49 && originNumber count <= 57 ) resultNumber = resultNumber

19、+ originNumber count ; eFlag = 1 ;currentState = S_7 ; else resultNumber = resultNumber.substring( 0 , resultNumber.length( ) - 2 ) ; printResult( ) ; init( ) ; currentState = S_0 ; count -= 2 ; count + ; break ; case S_7 : if( originNumber count >= 48 && originNumber count <= 57 ) resultNumber = resultNumber + originNumber count ;currentState = S_7 ; else if( originNumber count = '+' | o

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論