詞法分析器文檔說明.doc_第1頁
詞法分析器文檔說明.doc_第2頁
詞法分析器文檔說明.doc_第3頁
詞法分析器文檔說明.doc_第4頁
詞法分析器文檔說明.doc_第5頁
已閱讀5頁,還剩12頁未讀 繼續免費閱讀

下載本文檔

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

文檔簡介

詞法分析器說明文檔指數E|e都可,考慮過/的注釋 詞法規則 關鍵字: 本程序識別的關鍵字為int real if then else while,僅當單獨出現 以上標識符時識別為關鍵字,對于inta,ifb等識別為標識符。 標識符: 一個標識符必須以字母開頭,后面接上字母和數字,否則產生報錯信息,程 序停止詞法分析,輸出相關錯誤信息 如: 正確:abc, abc123, 等 錯誤:123abc 操作符: 本程序識別的操作符為: + - / * = = = != 當 !后跟其它字符時產生出錯信息。 分隔符: 本程序識別的分隔符為: ( ) ; 數字: 識別的數字遵循以下文法規則: digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 intnumber digit+ exponent (E|e) ( + | - | e ) digit+ fraction . digit+ realnumber digit+ exponent | digit+ fraction ( exponent | e ) 例如:正確: 123, 123E2, 123.3123.3E4 錯誤: 123A,123E, 123.A . 123.3A, 123.3E 注釋符: 注釋部分以 / 本程序對其不進行識別. 引入文件 為 #include形式,其他做出錯處理設計思路本程序采用字符流形式讀入文件,識別其中的關鍵字,標識符,分隔符,注釋符,引入文件聲明等.設計了一個靈活的讀取字符的自動機為如下圖所顯示讀入文件的第一個字符回車空格操作符+ - * 直接識別為操作符. 操作符號 ! = 進行跳轉看是否是=; !=; = = ;操作符/ 進行跳轉看是否是/ ( ); 直接識別為分隔符根據isletter()函數判斷是否讀到字符,若是,進行跳轉,以此識別出標識符串和關鍵字根據isdigit()函數判斷是否讀到數字,若是,進行跳轉,以此識別出整個數字狀態0 判斷讀入字符類型,進行狀態跳轉,并對行號和列號做相應調整轉入相應狀態,進行相關操作根據install()函數按格式將符號表輸出。根據nextChar()函數讀取下一個將要讀取的字符。根據iskey()函數判斷是否讀到規定的關鍵字。據analyzer()函數判斷各個字符各屬各家。 詞法分析部分的Lexical類如下:public class Lexical private int rnum;/行號private int rpos;/列號 private static ArrayList symbolTable;private StreamReader br;private char c;private char bC;private int state;private String str;public Lexical(StreamReader br)symbolTable = new ArrayList();this.br=br;c= ;bC= ;state = 0;rnum=1;rpos=0;str=;public ArrayList getSymbolTable()c = nextChar();state = 0;analyzer();return symbolTable;private void analyzer()bool isRunning=true;while(isRunning)switch (state)case 0:if(c= ) /空格 else if(c=t) /tabrpos+=3;else if(int)c=13) /空格 else if(int)c=10) /回車rpos = 0;rnum+;else if(c=) /結束符return;else if(c=+) /操作符+install(操作符,+,rpos,rnum); else if(c=-) /操作符-install(操作符,-,rpos,rnum);else if(c=/) /操作符/state = 1;else if(c=*) /操作符*install(操作符,*,rpos,rnum);else if(c=) /操作符= state = 2;else if(c=) /操作符) /操作符state = 4;else if(c=!) /操作符!state = 5;else if(c=) /分隔符install(分隔符,rpos,rnum);else if(c=) /分隔符install(分隔符,rpos,rnum);else if(c=() /分隔符(install(分隔符,(,rpos,rnum);else if(c=) /分隔符)install(分隔符,),rpos,rnum);else if(c=;) /分隔符;install(分隔符,;,rpos,rnum);else if(int)c=65535)isRunning=false;else if(isLetter(c) /讀到了字符bC = c;state = 6;else if(isDigit(c) /讀到了數字符bC = c;str=;state = 7;else if(c=#) state=13;else state=0;isRunning=false;fail(0); c = nextChar();break;case 1:if(c=/) /表示注釋后面的東西,不讀它while(int)c!=10)c = nextChar();rpos=0; /從下一行開始rnum+;c = nextChar();state = 0;else state = 0; / 下一個不是/, 后退一步到state=0install(操作符,/,rpos-1,rnum);break;case 2: if(c=) /表示操作符=state = 0;install(操作符,=,rpos-1,rnum);c = nextChar();else state = 0;install(操作符,=,rpos-1,rnum);break;case 3:if(c=) /表示操作符=state = 0;install(操作符,=,rpos-1,rnum);c = nextChar();else state = 0;install(操作符,=state = 0;install(操作符,=,rpos-1,rnum);c = nextChar();else state = 0;install(操作符,rpos-1,rnum);break;case 5:if(c=) / 表示操作符!=state = 0;install(操作符,!=,rpos-1,rnum);c = nextChar();else state = 0;isRunning=false;fail(2);break; /*/讀入標識符,后面是 數字符和字母才是對的/*case 6: String id = +bC;while(isLetter(c)|isDigit(c)id+=c;c = nextChar();if(isKey(id)install(關鍵字,id,rpos-id.Length,rnum);elseinstall(標識符,id,rpos-id.Length,rnum);state = 0;break;/*/讀入數字符,后面是 數字符(循環); .(轉狀態);E(轉狀態)/*case 7: str+=bC; while(isDigit(c) str+=c;c=nextChar();if(c=.) state=8;c=nextChar(); else if(c=E) state=10;c=nextChar();/需要判斷是否是 11w等else if(isLetter(c)state = 0;isRunning=false;fail(1); else state = 0;install(數字符,str,rpos-str.Length,rnum);break;/*/ . 后面跟的字符,除了數字符其它都出錯/*case 8: str+=.;if(isDigit(c)state = 9;str+=c;c=nextChar();elsestate = 0;isRunning=false;fail(1);break;/*/ .數字符 后面字符,可以是數字符或者是E(需要加錯誤提示,當不是空格而是其他字母時候)/*case 9: while(isDigit(c)str+=c;c=nextChar();if(c=E)state = 10;c=nextChar();/需要判斷是否是 11w 等else if(isLetter(c)state = 0;isRunning=false;fail(1); elsestate = 0;install(數字符,str,rpos-str.Length,rnum);break; /*/ E 后面字符, +;-;數字符 都是對的,其他都是錯的/*case 10:str+=E;if(c=+|c=-)state=11;str+=c;c=nextChar();else if(isDigit(c)state=12;str+=c;c=nextChar();elsestate=0;isRunning=false;fail(1);break;/*/ + - 后面字符, 數字符是對的.其他都是錯的/*case 11:if(isDigit(c)state=12;str+=c;c=nextChar();elsestate = 0;isRunning=false;fail(1);break;/*/ E數字符 后面字符,只有數字符是對的/*case 12:while(isDigit(c)str+=c;c=nextChar(); if(isLetter(c)state = 0;isRunning=false;fail(1); install(數字符,str,rpos-str.Length,rnum);state=0;break;case 13: /聲明部分 id =#;while(isLetter(c)id+=c;c = nextChar();if(id=#include&c=)id+=c; install(文件引入,id,rpos-id.Length,rnum);c = nextChar();else fail(3); elsefail(0);state = 0;break; private void fail(int i) string typeerror=;switch(i)case 0:typeerror=輸入非法字符錯誤;break;case 1:typeerror=操作符錯誤;break;case 2:typeerror=數字符錯誤;break;case 3:typeerror=聲明錯誤;break;Console.WriteLine(typeerror+,位置);private char nextChar()char t;rpos+;if(br.Peek() = -1)br.Close();return ;t= (char) br.Read();return t; private void install(String tType,String aValue,int rpos,int rnum)ArrayList tokenArray = new ArrayList();tokenArray.Add(tType);tokenArray.Add(aValue);tokenArray.Add();symbolTable.Add(tokenArray);str=; private bool isLetter(char checkChar)int checkInt = (int)checkChar;if(checkInt=97)|(checkInt=65&checkInt=97)return true;return false; private bool isDigit(char checkChar)int checkInt = (int)checkChar;if(checkInt=48)return true;return false; private bool isKey(String checkKey)if(checkKey.Equals(if)|checkKey.Equals(then)|checkKey.Equals(else)|checkKey.Equals(int)|checkKey.Equals(real)|checkKey.Equals(while)return true;return false;程序輸入輸出舉例:輸出格式為 : 記號 字符 1: #include #includemain() a=1;if(a=1)then a=2.3;else

溫馨提示

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

評論

0/150

提交評論