C詞法分析器設計報告同濟大學2010級計算機4班_第1頁
C詞法分析器設計報告同濟大學2010級計算機4班_第2頁
C詞法分析器設計報告同濟大學2010級計算機4班_第3頁
C詞法分析器設計報告同濟大學2010級計算機4班_第4頁
C詞法分析器設計報告同濟大學2010級計算機4班_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用C#詞法解析器設計報告同濟大學級計算機班范成程序界面南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用開發(fā)環(huán)境:MicrosoftVisualStudio2005南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用程序運行環(huán)境:任何安裝了〔或以上版本〕的計算機。下載地址:開發(fā)語言:MicrosoftC#.NET設計思想:根據(jù)教材給出的掃描程序框架,自行編寫掃描程序的各個方法〔Method〕,并設計程序界面,為各按鈕編寫Click事件響應方法,由“SCAN〞按鈕的Click事件響應方法調(diào)用掃描程序,依次對源程序〔來自文件或用戶鍵入〕的每個字符進行掃描并鑒別。掃描程序的返回值有以下幾各種類:表記符<@ID,該表記符在表記符表中的地址>保存字<@RESERVED_WORD_保存字編號,->整型常數(shù)<@INT,該數(shù)在整型常數(shù)表中的地址>浮點型常數(shù)<@FLOAT,該數(shù)在浮點型常數(shù)表中的地址>字符串<@STRTING,該字符串在字符串表中的地址>(nullstring)<@STRING_NULL,該字符串在字符串表中的地址>字符<@CHAR,該字符在字符表中的地址>符號<@符號助記符,->源程序掃描流程圖〔按鈕按下后〕:開始將找尋指示器指向源程序第一個字符YES源程序掃描達成?NO掃描當前字符,并將搜索指示器向后移動結束南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用狀態(tài)變換圖:空白字母或數(shù)字或下劃線字母或下劃線1非字母與數(shù)字與下劃線*02/非=*2324數(shù)字=25*/數(shù)字非數(shù)字2634非*~**/-2728295非-*3031`-632=@+337非+*3435#+836$=379=非=*3839%10^11(12)13[14]15{16}17,18.19?20:21;22

=40!非=*4142=43&非&*4445&46*非=*4748=49>非>*5051>52=53<非<*5455<56=57非〞“58〞59,非??606162南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用源程序:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.IO;namespaceScanner{publicpartialclassScanner:Form{publicScanner(){InitializeComponent();rtbInput.SelectAll();}privatestring[]token;//表記符表privateinttokenIndex=0;//表記符表指針privatelong[]constant;//整型常數(shù)表privateintconstantIndex=0;//整型常數(shù)表指針privatedouble[]dblConstant;//浮點常數(shù)表privateintdblConstantIndex=0;//浮點常數(shù)表指針privatestring[]strConst;//字符串表privateintstrConstIndex;//字符串表指針privatechar[]charConst;//字符表privateintcharConstIndex=0;//字符表指針privatestringinput;//存放待識其他源程序字符串privateintinputIndex=0;//input字符串指針privatecharch;//存放最新讀進的源程序字符privatestringstrToken;//存放組成單詞符號的字符串privateboolscanned=false;//是否掃描過最少一次privatestaticstring[]tokenReserve={"abstract","do","in","protected","true","as","double","int","public","try","base","else","interface","readonly","typeof","bool","enum","internal","ref","uint","break","event","is","return","ulong","byte","explicit","lock","sbyte",南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用"unchecked","case","extern","long","sealed","unsafe","catch","false","namespace","short","ushort","char","finally","new","sizeof","using","checked","fixed","null","stackalloc","virtual","class","float","object","static","volatile","const","for","operator","string","void","continue","foreach","out","struct","while","decimal","goto","override","switch","default","if","params","this","delegate","implicit","private","throw"};//C#保存重點字表(77個)將下一輸入字符讀到ch中,找尋指示器前移一字符地址privatevoidgetChar(){ch=input[inputIndex];inputIndex++;}將ch中的字符連接到strToken之后privatevoidconcat(){strToken+=ch;}判斷ch中的字符是否為字母privateboolisLetter(){if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))returntrue;elsereturnfalse;}判斷ch中的字符是否為數(shù)字privateboolisDigit(){if(ch>='0'&&ch<='9')returntrue;elsereturnfalse;}南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用//對strToken中的字符串查找保存字表,假設它是一個保存字那么返回其編碼,否那么返回0privateintreserve(){inti;boolnotFound=true;strings=strToken;for(i=0;notFound&&i<tokenReserve.Length;i++){if(tokenReserve[i]==s)notFound=false;}if(notFound)return0;elsereturni;}將找尋指示器回調(diào)一個字符地址,將ch置為空白字符privatevoidretract(){inputIndex--;}將strToken中的表記符插入符號表〔表中不存在該元素時才插入〕,返回符號表指針privateintinsertId(){先在符號表里找該元素inti;boolnotFound=true;strings=strToken;for(i=0;notFound&&i<token.Length;i++){if(token[i]==s)notFound=false;}沒找到if(notFound){token[tokenIndex]=strToken;tokenIndex++;return(tokenIndex-1);}找到了elsereturni-1;}將strToken中的整型常數(shù)插入整型常數(shù)表〔表中不存在該元素時才插入〕,返回整型常數(shù)表指針南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用privateintinsertConst(){inti;boolnotFound=true;longs=int.Parse(strToken);for(i=0;notFound&&i<constant.Length;i++){if(constant[i]==s)notFound=false;}沒找到if(notFound){constant[constantIndex]=int.Parse(strToken);constantIndex++;return(constantIndex-1);}找到了elsereturni-1;}將strToken中的浮點型常數(shù)插入浮點常數(shù)表〔表中不存在該元素時才插入〕,返回浮點常數(shù)表指針privateintinsertDblConst(){inti;boolnotFound=true;doubles=double.Parse(strToken);for(i=0;notFound&&i<dblConstant.Length;i++){if(dblConstant[i]==s)notFound=false;}沒找到if(notFound){dblConstant[dblConstantIndex]=double.Parse(strToken);dblConstantIndex++;return(dblConstantIndex-1);}找到了elsereturni-1;}將strToken中的字符串插入字符串表〔表中不存在該元素時才插入〕,返回字符串表指針privateintinsertString()南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用{inti;boolnotFound=true;strings=strToken;for(i=0;notFound&&i<strConst.Length;i++){if(strConst[i]==s)notFound=false;}沒找到if(notFound){strConst[strConstIndex]=strToken;strConstIndex++;return(strConstIndex-1);}找到了elsereturni-1;}將strToken中的字符插入字符表〔表中不存在該元素時才插入〕,返回字符表指針privateintinsertChar(){inti;boolnotFound=true;charc=ch;for(i=0;notFound&&i<charConst.Length;i++){if(charConst[i]==c)notFound=false;}沒找到if(notFound){charConst[charConstIndex]=ch;charConstIndex++;return(charConstIndex-1);}找到了elsereturni-1;}//Scan方法privatestringscan(){strToken="";南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用intcode,value;getChar();表記符及保存字if(isLetter()||ch=='_'){if(input.Length>1){while((isLetter()||isDigit()||(ch=='_'))&&inputIndex<input.Length){concat();getChar();}if(inputIndex<input.Length)retract();elseif(inputIndex==input.Length){if(isLetter()||isDigit()||(ch=='_'))concat();elseretract();}code=reserve();if(code==0){value=insertId();return(strToken+"<@ID"+","+value.ToString()+">");}elsereturn(strToken+"<@RESERVED_WORD+"code.ToString()+",->");}else{return(ch+"<@ID"+",0>");}}整型常數(shù)和浮點常數(shù)elseif(isDigit()){if(input.Length>1){while(isDigit()&&inputIndex<input.Length){concat();getChar();}if(ch=='.'&&inputIndex<input.Length){concat();南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用getChar();while(isDigit()&&inputIndex<input.Length){concat();getChar();}if(inputIndex<input.Length)retract();elseif(inputIndex==input.Length){if(isDigit())concat();elseretract();}value=insertDblConst();return(strToken+"<@FLOAT"+","+value.ToString()+">");}if(inputIndex<input.Length)retract();elseif(inputIndex==input.Length){if(isDigit())concat();elseretract();}value=insertConst();return(strToken+"<@INT"+","+value.ToString()+">");}else{return(ch+"<@INT,0>");}}elseif(ch=='='){if(inputIndex<input.Length){getChar();if(ch=='=')return("==<@EQUAL,->");retract();return("=<@ASSIGN,->");}elsereturn("=<@ASSIGN,->");}elseif(ch=='!'){if(inputIndex<input.Length){南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用getChar();if(ch=='=')return("!=<@NOT_EQUAL,->");retract();return("!<@NOT,->");}elsereturn("!<@NOT,->");}elseif(ch=='+'){if(inputIndex<input.Length){getChar();if(ch=='+')return("++<@SELF_INCREASE,->");elseif(ch=='=')return("+=<@SELF_INCREASE_BY_SIZE,->");retract();return("+<@PLUS,->");}elsereturn("+<@PLUS,->");}elseif(ch=='-'){if(inputIndex<input.Length){getChar();if(ch=='-')return("--<@SELF_DECREASE,->");elseif(ch=='=')return("-=<@SELF_DECREASE_BY_SIZE,->");elseif(ch=='>')return("-><@MEMBER_VISIT,->");retract();return("-<@MINUS,->");}elsereturn("-<@MINUS,->");}elseif(ch=='|'){if(inputIndex<input.Length){getChar();if(ch=='|')return("||<@OR2,->");elseif(ch=='=')return("|=<@OR_EQUAL,->");retract();return("|<@OR1,->");}elsereturn("|<@OR1,->");}南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用elseif(ch=='&'){if(inputIndex<input.Length){getChar();if(ch=='&')return("&&<@AND2,->");elseif(ch=='=')return("&=<@AND_EQUAL,->");retract();return("&<@AND1,->");}elsereturn("&<@AND1,->");}elseif(ch=='>'){if(inputIndex<input.Length){getChar();if(ch=='=')return(">=<@GREATER_OR_EQUAL,->");elseif(ch=='>')return(">><@BIT_MOV_R,->");retract();return("><@GREATER,->");}elsereturn("><@GREATER,->");}elseif(ch=='<'){if(inputIndex<input.Length){getChar();if(ch=='=')return("<=<@LESS_OR_EQUAL,->");elseif(ch=='<')return("<<<@BIT_MOV_L,->");retract();return("<<@LESS,->");}elsereturn("<<@LESS,->");}elseif(ch=='\''){if(inputIndex<input.Length-2){getChar();if(ch=='\'')return"INVALIDUSEOF'";elseif(ch=='\\'){南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用getChar();if(ch=='n'||ch=='r'||ch=='\\'||ch=='t'||ch=='b'||ch=='\''||ch=='\"')//各種換行符{getChar();if(ch=='\'')return"\\"+input[inputIndex-2]+"<@ESC,->";}else{retract();return"INVALIDUSEOF'";}}getChar();if(ch=='\''){ch=input[inputIndex-2];value=insertChar();return(input[inputIndex-2]+"<@CHAR,"+value.ToString()+">");}elseif(inputIndex==input.Length-1){retract();return"INVALIDUSEOF'";}else{retract();retract();return"INVALIDUSEOF'";}}elseif(inputIndex<input.Length-1){getChar();if(ch=='\''||ch=='\\')return"INVALIDUSEOF'";getChar();if(ch=='\''){ch=input[inputIndex-2];value=insertChar();return(input[inputIndex-2]+"<@CHAR,"+value.ToString()+">");}elseif(inputIndex==input.Length-1)南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用{retract();return"INVALIDUSEOF'";}else{retract();retract();return"INVALIDUSEOF'";}}elsereturn"INVALIDUSEOF'";}elseif(ch=='\"'){boolnotMatch=true;if(inputIndex==input.Length)return("INVALIDUSEOF\"");elseif(input[inputIndex]=='\"'){strToken="";value=insertString();if(inputIndex<input.Length)getChar();return("(nullstring)<@STRING_NULL,"+value.ToString()+">");}else{getChar();while(notMatch&&inputIndex<=input.Length){concat();if(inputIndex<input.Length){getChar();if(ch=='\"')notMatch=false;}if(ch=='\"')notMatch=false;}if(notMatch)return("INVALIDUSEOF\"");else{value=insertString();return(strToken+"<@STRING,"+value.ToString()+">");}}南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用}elseif(ch=='*'){if(inputIndex<input.Length){getChar();if(ch=='=')return("*=<@SELF_MULTIPLY_BY_SIZE,->");retract();return("*<@MULTIPLY,->");}elsereturn("*<@MULTIPLY,->");}elseif(ch=='/'){if(inputIndex<input.Length){getChar();//說明行if(ch=='*'){boolstillSearch=true;if(inputIndex>input.Length-2)return("INVALIDUSEOF/*");getChar();while(ch!='*'&&input[inputIndex]!='/'&&stillSearch){concat();getChar();if(inputIndex==input.Length)stillSearch=false;}if(inputIndex==input.Length){concat();}getChar();return("");}elseif(ch=='/'){if(inputIndex<input.Length){getChar();while(ch!='\n'&&inputIndex<input.Length){concat();南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用getChar();}if(inputIndex==input.Length){concat();}return("");}elsereturn("");}elseif(ch=='='){return("/=<@SELF_DIVIDE_BY_SIZE,->");}else{retract();return("/<@DIVIDE,->");}}elsereturn("/<@DIVIDE,->");}elseif(ch=='#')return("#<@SHARP,->");elseif(ch==':'){if(inputIndex<input.Length){getChar();if(ch==':')return("::<@FIELD,->");retract();return(":<@COLON,->");}elsereturn(":<@COLON,->");}elseif(ch==';')return(";<@SEMICOLON,->");elseif(ch=='~')return("~<@COMPLEMENT,->");elseif(ch=='`')return("`<@UPDOT,->");elseif(ch=='@')return("@<@AT,->");elseif(ch=='$')return("$<@DOLLAR,->");elseif(ch=='%'){if(inputIndex<input.Length){getChar();南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用if(ch=='=')return("%=<@MOD_EQUAL,->");retract();return("%<@MOD,->");}elsereturn("%<@MOD,->");}elseif(ch=='^'){if(inputIndex<input.Length){getChar();if(ch=='=')return("^=<@XOR_EQUAL,->");retract();return("^<@XOR,->");}elsereturn("^<@XOR,->");}elseif(ch=='_')return("_<@UNDERLINE,->");elseif(ch=='?'){if(inputIndex<input.Length){getChar();if(ch=='?')return("??<@RETURN_NOT_NULL,->");retract();return("?<@INTERROGATION,->");}elsereturn("?<@INTERROGATION,->");}elseif(ch=='[')return("[<@LSQUARE,->");elseif(ch==']')return("]<@RSQUARE,->");elseif(ch=='{')return("{<@LBRACKET,->");elseif(ch=='}')return("}<@RBRACKET,->");elseif(ch=='(')return("(<@LPAR,->");elseif(ch==')')return(")<@RPAR,->");elseif(ch==',')return(",<@COMMA,->");elseif(ch=='.')return(".<@DOT,->");elseif(ch=='')return("");elseif(ch=='\n')return("");elseif(ch=='\t')return("");elseif(ch=='\b')return("");elseif(ch=='\r')return("");elseif(ch=='\\')return("");elsereturn(ch+",UNDEFINED,-");南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用}privatevoidcmdReset_Click(objectsender,EventArgse){if(rtbInput.Text!=""||rtbOutput.Text!=""){if(MessageBox.Show("CLEARTEXT?","TEXTCLEARCONFIRM",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK){rtbInput.Clear();rtbOutput.Clear();}}}privatevoidcmdStart_Click(objectsender,EventArgse){rtbOutput.Clear();token=newstring[10000];constant=newlong[10000];dblConstant=newdouble[10000];charConst=newchar[10000];strConst=newstring[10000];tokenIndex=0;constantIndex=0;dblConstantIndex=0;strConstIndex=0;charConstIndex=0;inputIndex=0;input=rtbInput.Text.ToString();strings;stringresult="";if(rtbInput.Text.Length>100000){rtbOutput.Text="Filetoolarge.";return;}try{while(inputIndex<input.Length){s=scan();if(s!="")result+=s+"\n";//掃描字符串}南通效勞器租用,效勞器托管,域名注冊,合租空間,游戲效勞器,企業(yè)郵局南通效勞器租用|效勞器托管|空間合租|游戲效勞器租用if(result!=""){rtbOutput.Text=result;scanned=true;}}catch(IndexOutOfRangeExceptionindexOutOfRange){rtbOutput.Text+="Invalidinput.";}catch(Exceptionexception){rtbOutput.Text=exception.Message;}}privatevoidcmdSave_Click(objectsender,EventArgse){if(scanned&&rtbOutput.Text!=""){DialogResultbuttonClicked=saveFileDialog.ShowDialog();if(buttonClicked.Equals(DialogResult.OK)){rtbOutput.SaveFile(saveFileDialog.FileName);MessageBox.Show("SAVED.","SCANNER");}}}privatevoidcmdAbout_Click(objectsender,EventArgse){MessageBox.Show("范成(KENSHIN)\nTongjiUniversity\nDepartmentofComputerScience\n2007","ABOUT",MessageBoxButtons.OK,MessageBoxIcon.Information);}privatevoidcmdOpen_Click(objectsender,EventArgse){DialogResultbuttonClicked=openFileDialog.ShowDialog();if(buttonClicked.Equals(DialogResult.OK)){FileStreamfs=newFileStream(openFileDialog.FileName,FileMode.Open,FileAccess.Read);StreamReaderm_streamReader=newStream

溫馨提示

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

評論

0/150

提交評論