編譯原理課程設(shè)計報告模板_第1頁
編譯原理課程設(shè)計報告模板_第2頁
編譯原理課程設(shè)計報告模板_第3頁
編譯原理課程設(shè)計報告模板_第4頁
編譯原理課程設(shè)計報告模板_第5頁
已閱讀5頁,還剩22頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

《編譯原理課程設(shè)計》報告MiniC編譯器MFC實現(xiàn)分組序號:5設(shè)計地點:微機301、401電子郵件:1260815873@指導(dǎo)教師:李村合技術(shù)支持:CSDN下載:百度文庫下載:迷若煙雨版權(quán)所有迷若煙雨版權(quán)所有 僅用于學(xué)習(xí)、交流,侵權(quán)必究僅用于學(xué)習(xí)、交流,侵權(quán)必究 2011年12月24日

目錄1 前言 功能需求1、用C++語言實現(xiàn)了類C語言,稱為擴展的MiniC語言,即MiniC語言的編譯器。 2、文法是LL(1)文法,采用遞歸子程序法實現(xiàn)語法分析,并用C語言實現(xiàn)了詞法分析器、語法分析器、代碼生成器,能直接生成80x96運行的可執(zhí)行文件。 3、在聲明中實現(xiàn)了對靜態(tài)常量、變量、數(shù)組和過程的聲明支持;在賦值語句中實現(xiàn)了+=、++、-=、--、*=、/=、%=、:=(賦值);數(shù)學(xué)運算支持+、-、*、/(/除法編譯可以通過,但匯編時會出現(xiàn)溢出錯誤!正在努力修正。 4、使用函數(shù)實現(xiàn)了對過程的調(diào)用。 5、使用{}實現(xiàn)了復(fù)合語句。 6、使用read()來同時讀入一個到多個數(shù)據(jù),使用write()來同時輸出一個到多個數(shù)據(jù)。printf()來輸出一個字符串。 7、在循環(huán)分支語句中實現(xiàn)了if語句,if……else……語句,for(…,……,……)…語句,while……語句。 8、條件語句包括奇偶判斷、=(等于)、#(不等于)、<、<=、>、>=。主要用到的關(guān)鍵詞:關(guān)鍵字:enumTokenType //reservedKeyword _AUTO,_DOUBLE,_INT,_STRUCT, _BREAK,_ELSE,_LONG,_SWITCH, _CASE,_ENUM,_REGISTER,_TYPEDEF, _CHAR,_EXTERN,_RETURN,_UNION, _CONST,_FLOAT,_SHORT,_UNSIGNED, _CONTINUE,_FOR,_SIGNED,_VOID, _DEFAULT,_GOTO,_SIZEOF,_VOLATILE, _DO,_IF,_STATIC,_WHILE, _READ,_WRITE,_PRINTF, //operations ASSIGN,PLUS,MINUS,TIMES,DIV,MOD, BITWISE_AND,BITWISE_OR,BITWISE_NOT,LOGICAL_NOT,LT,GT, //interpunctions LPARAN,RPARAN,LBRACE,RBRACE,LSQUARE,RSQUARE,COMMA,DOT,SEMI,COLON, //complexoperations EQ/*==*/,NEQ/*!=*/,PLUS_PLUS/*++*/,MINUS_MINUS/*--*/, PLUS_ASSIGN/*+=*/,MINUS_ASSIGN/*-=*/,TIMES_ASSIGN/**=*/,DIV_ASSIGN/*/=*/, NGT/*<=*/,NLT/*>=*/,LOGICAL_AND/*&&*/,LOGICAL_OR/*||*/, //others _EOF,_ID,_NUM,_STRING,_CHARACTER,_LABEL,_ERROR,_NONE匯編成可運行文件采用80x86匯編語言,匯編成可執(zhí)行文件,可在相應(yīng)機型的機器上運行。MiniC語言的BNF文法

//Grammar://1.program->declaration_list//2.declaration_list->declaration_listdeclaration|declaration//3.declaration->var_declaration|fun_declaration//m_tokenisasupportedtype-identifiertoken//4.var_declaration->type_specifierID(,...)`;`|type_specifierID`[`NUM`]`(,...)`;`//5.type_specifier->`int`|`void`|`char`,actuallythisstepisindeclaration_list()//m_token.str==";"","or"["http://6.fun_declaration->type_specifierID`(`params`)`compound_stmt//m_token.str=="(",TypeTokencontainstype_specifier,IDTokencontainsID//7.params->param_list|`void`|empty,`void`isthoughtasempty//8.param_list->param_list`,`param|param//9.param->type_specifierID|type_specifierID`[``]`//m_token.str=="("http://10.compound_stmt->`{`loal_declarationsstatement_list`}`|expression_stmt//thenexttokenshouldbe'{'//11.local_declarations->local_declarationsvar_declaration|var_declaration//m_tokenisasupportedtype-specifiertoken//12.`read``(`var`)``;`//m_token.str=="read"http://13.`write``(`expression`)``;`//m_token.str=="write"http://14.`printf``(``"`STRING`"``)``;`//m_token.str=="printf"http://15.expression_stmt->expression`;`|`;`//m_tokenis'!','(',ID,NUM,CHARACTERor';'//16.expression->var`=`expression|logic1_expression//FIRST(expression)={`!`,`(`,ID,NUM,CHARACTER}//m_tokencontainsthefirsttokenofexpression//17.logic1_expression->logic1_expression`||`logic2_expression|logic2_expression//m_tokencontainsthefirsttokenoflogic1_expression//18.logic2_expression->logic2_expression`&&`simple_expression|simple_expression//m_tokencontainsthefirsttokenoflogic2_expression//19.simple_expression->additive_expressionrelopadditive_expression|additive_expression//20.relop->`<=`|`<`|`>`|`>=`|`==`|`!=`//m_tokencontainsthefirsttokenofsimple_expression//21.additive_expression->additive_expressionaddopterm|term//22.addop->`+`|`-`//m_tokencontainsthefirsttokenofadd_expression//23.term->termmuloplogic3_expression|logic3_expression//24.mulop->`*`|`/`|`%`//m_tokencontainsthefirsttokenofterm//25.logic3_expression->`!`logic3_expression|factor//m_tokencontainsthefirsttokenoflogic3_expression//26.factor->`(`expression`)`|var|call|NUM//m_tokencontainsthefirsttokenoffactor//27.var->ID|ID`[`expression`]`//IDTokencontainsID//28.call->ID`(`args`)`//m_token.str=="(",IDTokencontainsID//29.args->args_list|empty//30.args_list->args_list`,`expression|expression//m_token.str=="("http://31:sub_compoundstmt->ID`:`|call`;`|expression_stmt//m_tokencontainsthefirsttokenofsub_compoundstmt//32:if_stmt->`if``(`expression`)`compound_stmt//|`if``(`expression`)`compound_stmt`else`compound_stmt//m_token.str=="if"http://33.while_stmt->`while``(`expression`)`compound_stmt//m_token.str=="while"http://34.for_stmt->`for``(`var`=`expression`;`expression`;`var`=`expression`)`compound_stmt//m_token.str=="for"http://35.goto_stmt->`goto`ID`;`//m_token.str=="goto"http://36.break_stmt->`break``;`//m_token.str=="break"http://37.continue_stmt->`continue``;`//m_token.str="continue"http://38.return_stmt->`return``;`|`return`expression`;`//m_token.str="return"系統(tǒng)設(shè)計與實現(xiàn)BY5編譯程序主要模塊功能

表1BY5編譯程序的過程或函數(shù)的功能表過程或函數(shù)名簡要功能說明CTokenizer::NextToken()略去空格、回車、注釋取得輸入串的下一個有效字符CScaner::NextToken()取得下一個有效字符的相關(guān)屬性CParser::BuildSyntaxTree()構(gòu)造輸入串的語法樹CAnalyzer::BuildSymbolTable()構(gòu)造符號表CAnalyzer::traverse遍歷語法樹,并設(shè)置相應(yīng)的屬性CAnalyzer::TraceTypeCheck()語義分析處理CAsmCodeGenerator::GenerateAsmCode生成匯編語言代碼CBY5Doc::OnCompileBuildAsm()調(diào)用語義類,生成匯編代碼CBY5Doc::OnCompileBuildExe()調(diào)用匯編程序,生成可執(zhí)行文件OnCompileRun()運行生成的可執(zhí)行文件詞法分析子程序詞法分析類為CTokenizer,功能是從源程序中讀出一個單詞符號(token),把它的信息放入類中中,語法分析器需要單詞時,直接從這個類中獲得。詞法分析器的分析過程:構(gòu)造CTokenizer時,它通過構(gòu)造函數(shù)從源程序中獲得一個字符串。如果這個字符是字母,則繼續(xù)獲取字符或數(shù)字,最終可以拼成一個單詞,查保留字表,如果查到為保留字,則把m_tType變量賦成相應(yīng)的保留字類型值;如果沒有查到,則這個單詞應(yīng)是一個用戶自定義的標(biāo)識符(可能是變量名、常量名或是過程的名字),把m_tType置為相應(yīng)的關(guān)鍵值,把這個單詞存入m_tType變量。查保留字表時使用了二分法查找以提高效率。如果m_tType獲得的字符是數(shù)字,則繼續(xù)用NextToken()獲取數(shù)字,并把它們拼成一個整數(shù),然后把m_tType置為number,并把拼成的數(shù)值放入m_dVal變量。如果識別出其它合法的符號(比如:賦值號、大于號、小于等于號等),則把m_tType則成相應(yīng)的類型。對應(yīng)的CTokenizer類:

//givesthenextToken,returnsthetokentypeintCTokenizer::NextToken() if(m_bPushedBack){ m_bPushedBack=FALSE; returnm_tType; intc=m_peekc; m_sVal=_T(""); if(c==EOF)returnm_tType=TT_EOF; //isthisaspace while(::isspace(c)){ if(c=='\r'){ m_iLineNo++; c=GetChar(); if(c=='\n')c=GetChar(); if(m_bEolIsSignificant){ m_peekc=c; returnm_tType=TT_EOL; }else{ if(c=='\n'){ m_iLineNo++; if(m_bEolIsSignificant){ m_peekc=''; returnm_tType=TT_EOL; c=GetChar(); if(c==EOF)returnm_tType=TT_EOF; //isthisanumber if(::isdigit(c)||c=='.'||c=='-'){ intipre=m_iChar-2; if(ipre<0) ipre=0; charchpre=m_sString[ipre]; BOOLneg=FALSE; if(c=='-') { c=GetChar(); if(c!='.'&&!::isdigit(c)||!isop(chpre)){ m_peekc=c; returnm_tType='-'; neg=TRUE; doublev=0; intdecexp=0; intseendot=0; while(true){ if(c=='.'&&seendot==0) seendot=1; elseif(::isdigit(c)){ v=v*10+(c-'0'); decexp+=seendot; }else break; c=GetChar(); m_peekc=c; if(decexp!=0){ doubledenom=10; decexp--; while(decexp>0){ denom*=10; decexp--; v=v/denom; }elseif(seendot==1){ m_iChar--; m_peekc='.'; seendot=0; m_dVal=neg-v:v; if(seendot==0) returnm_tType=TT_INTEGER; else returnm_tType=TT_REAL; //isthisaword if(::isalpha(c)||c=='_'){ inti=0; m_sVal=_T(""); do{ m_sVal=m_sVal+(TCHAR)c; c=GetChar(); }while(::isalnum(c)||c=='_'); m_peekc=c; if(m_bForceLower) m_sVal.MakeLower(); returnm_tType=TT_WORD; //nowthechar&string if(c=='\''||c=='"'){ m_sVal=_T(""); m_tType=c; m_peekc=''; inti=0,c2; while((c=GetChar())!=EOF&&c!=m_tType&&c!='\n'&&c!='\r'){ if(c=='\\')//escape switch(c=GetChar()){ case'a':c=0x7;break; case'b':c='\b';break; case'f':c=0xC;break; case'n':c='\n';break; case'r':c='\r';break; case't':c='\t';break; case'v':c=0xb;break; case'0': case'1': case'2': case'3': case'4': case'5': case'6': case'7': c=c-'0'; c2=GetChar(); if(c2==m_tType){ m_sVal+=(TCHAR)c; returnm_tType; if('0'<=c2&&c2<='7'){//octal c=(c<<3)+(c2-'0'); c2=GetChar(); if(c2==m_tType){ m_sVal+=(TCHAR)c; returnm_tType; if('0'<=c2&&c2<='7') c=(c<<3)+(c2-'0'); else{ m_sVal+=(TCHAR)c; c=c2; }else{ m_sVal+=(TCHAR)c; c=c2; break; default: //warning:'c':unrecognizedcharacterescapesequence OutputErrMsg("warninginline%d:'%c':unrecognizedcharacterescapesequence", m_iLineNo,c); m_sVal+=(TCHAR)c; if(c==EOF){ //errormsg:syntaxerrorinline%d:missing'"' OutputErrMsg("errorinline%d:syntaxerror,missing'\"'",m_iLineNo); }elseif(c=='\r'||c=='\n'){ //errormsg:syntaxerrorinline%d:newlineinconstant OutputErrMsg("errorinline%d:syntaxerror,newlineinconstant",m_iLineNo); returnm_tType; //andnowthecomment //"http://"or"/*...*/" if(c=='/'&&(m_bSlSlComments||m_bSlStComments)){ c=GetChar(); if(c=='*'&&m_bSlStComments){ intprevc=0; while((c=GetChar())!='/'||prevc!='*'){ if(c=='\n') m_iLineNo++; if(c==EOF)returnm_tType=TT_EOF; prevc=c; m_peekc=''; returnNextToken(); }else{ if(c=='/'&&m_bSlSlComments){ while((c=GetChar())!='\n'&&c!='\r'); m_peekc=c; returnNextToken(); }else{ m_peekc=c; returnm_tType='/'; m_peekc=''; returnm_tType=c;語法語義分析子程序語法分析子程序采用了自頂向下的遞歸子程序法,語法分析同時也根據(jù)程序的語義生成相應(yīng)的代碼,并提供了出錯處理的機制。語法分析主要由分程序分析過程(fun_declaration)、變量定義分析過程(var_declaration)、語句分析過程(statement)、表達(dá)式處理過程(expression)、項處理過程(term)、因子處理過程(factor)和條件處理過程(if_stmt)、循環(huán)處理過程(for_stmt)、調(diào)用子過程(call)構(gòu)成。這些過程在結(jié)構(gòu)上構(gòu)成一個嵌套的層次結(jié)構(gòu)。除此之外,還有ParamListRec、FunDecListRec、CFunArgsCheck作過語法分析的輔助過程。分程序處理過程CTreeNode*CParser::fun_declaration() CTreeNode*temp=newNode(kFunDec,TypeToken.type,IDToken.str); //updatefunctionscope m_szScope=IDToken.str; //params CTreeNode*p=temp->child[0]=params(); if(p)p->father=temp; while(p&&p->sibling){ p=p->sibling; p->father=temp; if(!match(RPARAN)){ OutputErrMsg("errorinline%d:missing')'infunction\"%s\"(...)declaration", m_pScaner->LineNo(),(LPCTSTR)m_token.str); m_pScaner->PushBack(); //compoundstatements p=temp->child[1]=compound_stmt(); if(p)p->father=temp; while(p&&p->sibling){ p=p->sibling;p->father=temp; returntemp;變量定義過程CTreeNode*CParser::var() CTreeNode*temp=newExpNode(kID,IDToken.type,IDToken.str); m_token=m_pScaner->NextToken();//shouldbe`[`orjustpushback if(m_token.type==LSQUARE){ temp->bArray=TRUE; m_token=m_pScaner->NextToken(); temp->child[0]=expression(); if(!match(RSQUARE)){ OutputErrMsg("errorinline%d:missing']'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery }else m_pScaner->PushBack(); returntemp;語句處理過程CTreeNode*CParser::compound_stmt() CTreeNode*first=NULL,*last=NULL,*temp=NULL; BOOLbHasNoBraces=FALSE; if(!match(LBRACE)){//match'{' //OutputErrMsg("errorinline%d:missing'{'",m_pScaner->LineNo()); bHasNoBraces=TRUE; m_pScaner->PushBack();//errorrecovery //local_declarations while(1){ TypeToken=m_token=m_pScaner->NextToken(); if(m_token.type==_CHAR||m_token.type==_INT|| m_token.type==_VOID||m_token.type==_FLOAT) temp=local_declarations(); else{m_pScaner->PushBack();break;} if(bHasNoBraces)returntemp;//hasnobraces,returnwhenreachthefirst';' if(temp) //linkalllocal_declarationstogether if(!first){first=temp;last=temp->LastSibling();} else{last->sibling=temp;last=temp->LastSibling();} //statement_list //m_tokencontainsthefirsttokenofstatement_list m_token=m_pScaner->NextToken(); while(1){ temp=NULL; if(m_token.type==RBRACE){ if(bHasNoBraces)OutputErrMsg("errorinline%d:unpaired'}'",m_pScaner->LineNo()); break;//'}' if(m_token.type==_EOF){ OutputErrMsg("errorinline%d:missing'}'",m_pScaner->LineNo()); m_pScaner->PushBack(); break; switch(m_token.type){ case_READ: temp=read_stmt(); break; case_WRITE: temp=write_stmt(); break; case_PRINTF: temp=printf_stmt(); break; caseSEMI://';' case_NUM: case_CHARACTER: caseLOGICAL_NOT: caseLPARAN: temp=expression_stmt(); break; case_ID: temp=subcompound_stmt(); break; case_IF: temp=if_stmt(); break; case_WHILE: temp=while_stmt(); break; case_FOR: temp=for_stmt(); break; case_GOTO: temp=goto_stmt(); break; case_BREAK: temp=break_stmt(); break; case_CONTINUE: temp=continue_stmt(); break; case_RETURN: temp=return_stmt(); break; case_ELSE: OutputErrMsg("errorinline%d:unpaired'else'statement",m_pScaner->LineNo()); ConsumeUntil(SEMI,RBRACE); break; default: OutputErrMsg("errorinline%d:undefinedsymbol\"%s\"", m_pScaner->LineNo(),(LPCTSTR)m_token.str); ConsumeUntil(SEMI,RBRACE); if(bHasNoBraces)returntemp;//hasnobraces,returnwhenreachthefirst';' if(temp) //linkallstatementstogether if(!first){first=temp;last=temp->LastSibling();} else{last->sibling=temp;last=temp->LastSibling();} m_token=m_pScaner->NextToken(); returnfirst;賦值語句的處理CTreeNode*CParser::factor() CTreeNode*temp=NULL; switch(m_token.type){ case_NUM: case_CHARACTER: temp=newExpNode(kConst,m_token.type,m_token.str); break; case_ID: IDToken=m_token; m_token=m_pScaner->NextToken(); if(m_token.type==LPARAN)temp=call(); else{ m_pScaner->PushBack();//pushthenexttokenback temp=var(); break; caseLPARAN: m_token=m_pScaner->NextToken();//m_tokencontainthefirsttokenofexpression temp=expression(); if(!match(RPARAN)){//match')' OutputErrMsg("errorinline%d:missing')'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery break; default: OutputErrMsg("errorinline%d:'%s'expressionsyntaxerror", m_pScaner->LineNo(),(LPCTSTR)m_token.str); ConsumeUntil(SEMI,RBRACE);//errorrecovery returntemp;read語句的處理CTreeNode*CParser::read_stmt() CTreeNode*temp=NULL; if(!match(LPARAN)){//'(' OutputErrMsg("errorinline%d:syntaxerror,missing'('",m_pScaner->LineNo()); ConsumeUntil(SEMI,RBRACE);//errorrecovery returnNULL; IDToken=m_token=m_pScaner->NextToken(); if(m_token.type!=_ID){ OutputErrMsg("errorinline%d:\"%s\"badarguments",m_pScaner->LineNo(),(LPCTSTR)m_token.str); ConsumeUntil(SEMI,RBRACE);//errorrecovery returnNULL; temp=newStmtNode(kRead,CString("read")); if((temp->child[0]=var())!=NULL)temp->child[0]->father=temp; if(!match(RPARAN)){//')' OutputErrMsg("errorinline%d:syntaxerror,missing')'",m_pScaner->LineNo()); ConsumeUntil(SEMI,RBRACE);//errorrecovery returntemp; if(!match(SEMI)){//';' OutputErrMsg("errorinline%d:syntaxerror,missing';'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery returntemp;write語句的處理CTreeNode*CParser::write_stmt() CTreeNode*temp=NULL; if(!match(LPARAN)){//'(' OutputErrMsg("errorinline%d:syntaxerror,missing'('",m_pScaner->LineNo()); ConsumeUntil(SEMI,RBRACE);//errorrecovery returnNULL; temp=newStmtNode(kWrite,CString("write")); m_token=m_pScaner->NextToken(); //m_tokencontainsthefirsttokenofexpression if((temp->child[0]=expression())!=NULL)temp->child[0]->father=temp; if(!match(RPARAN)){//')' OutputErrMsg("errorinline%d:syntaxerror,missing')'",m_pScaner->LineNo()); ConsumeUntil(SEMI,RBRACE);//errorrecovery returntemp; if(!match(SEMI)){//';' OutputErrMsg("errorinline%d:syntaxerror,missing';'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery returntemp;call語句的處理CTreeNode*CParser::call() CTreeNode*p=newStmtNode(kCall,IDToken.str),*temp=NULL; p->szScope=_T("global");// CTreeNode*temp=newExpNode(kID,IDToken.type,IDToken.str);// p->child[0]=temp;// p->child[0]->father=p; if((p->child[0]=args()))p->child[0]->father=p; temp=p->child[0]; while(temp&&temp->sibling){ temp=temp->sibling; temp->father=p; if(!match(RPARAN)){//match')' OutputErrMsg("errorinline%d:missing')'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery returnp;if語句的處理CTreeNode*CParser::if_stmt() CTreeNode*temp=newStmtNode(kIf,m_token.str),*p=NULL; if(!match(LPARAN))//match'(' OutputErrMsg("errorinline%d:missing'('in'if'statement",m_pScaner->LineNo()); elsem_token=m_pScaner->NextToken(); //m_tokenshouldbethefirsttokenofexpression temp->child[0]=expression(); if(temp->child[0])temp->child[0]->father=temp; if(!match(RPARAN)){//match')' OutputErrMsg("errorinline%d:missing')'in'if'statement",m_pScaner->LineNo()); m_pScaner->PushBack(); p=temp->child[1]=compound_stmt(); if(p)p->father=temp; while(p&&p->sibling){p=p->sibling;p->father=temp;} m_token=m_pScaner->NextToken(); if(m_token.type==_ELSE){ p=temp->child[2]=compound_stmt(); if(p)p->father=temp; while(p&&p->sibling){p=p->sibling;p->father=temp;} }else m_pScaner->PushBack();//pushthenexttokenback returntemp;while語句的處理CTreeNode*CParser::while_stmt() CTreeNode*temp=newStmtNode(kWhile,m_token.str),*p=NULL; if(!match(LPARAN))//match'(' OutputErrMsg("errorinline%d:missing'('in'while'statement",m_pScaner->LineNo()); elsem_token=m_pScaner->NextToken(); //m_tokenshouldbethefirsttokenofexpression temp->child[0]=expression(); if(temp->child[0])temp->child[0]->father=temp; if(!match(RPARAN)){//match')' OutputErrMsg("errorinline%d:missing')'in'while'statement",m_pScaner->LineNo()); m_pScaner->PushBack(); //thenexttokenshouldbe'{' p=temp->child[1]=compound_stmt(); if(p)p->father=temp; while(p&&p->sibling){p=p->sibling;p->father=temp;} returntemp;表達(dá)式、項、因子處理CTreeNode*CParser::factor() CTreeNode*temp=NULL; switch(m_token.type){ case_NUM: case_CHARACTER: temp=newExpNode(kConst,m_token.type,m_token.str); break; case_ID: IDToken=m_token; m_token=m_pScaner->NextToken(); if(m_token.type==LPARAN)temp=call(); else{ m_pScaner->PushBack();//pushthenexttokenback temp=var(); break; caseLPARAN: m_token=m_pScaner->NextToken();//m_tokencontainthefirsttokenofexpression temp=expression(); if(!match(RPARAN)){//match')' OutputErrMsg("errorinline%d:missing')'",m_pScaner->LineNo()); m_pScaner->PushBack();//errorrecovery break; default: OutputErrMsg("errorinline%d:'%s'expressionsyntaxerror", m_pScaner->LineNo(),(LPCTSTR)m_token.str); ConsumeUntil(SEMI,RBRACE);//errorrecovery returntemp;邏輯表達(dá)式的處理CTreeNode*CParser::local_declarations() CTreeNode*temp=NULL; IDToken=m_token=m_pScaner->NextToken();//IDorerror if(IDToken.type!=_ID){ OutputErrMsg("errorinline%d:\"%s\"isareservedtoken", m_pScaner->LineNo(),(LPCTSTR)IDToken.str); ConsumeUntil(SEMI);//errorrecovery returnNULL; m_token=m_pScaner->NextToken();//';','[',','orerror if(m_token.type==SEMI||m_token.type==LSQUARE||m_token.type==COMMA) temp=var_declaration(); else{ OutputErrMsg("errorinline%d:missing';'afteridentifier\"%s\"", m_pScaner->LineNo(),(LPCTSTR)IDToken.str); m_pScaner->PushBack();//errorrecovery returntemp;CTreeNode*CParser::logic1_expression() CTreeNode*p=logic2_expression(); m_token=m_pScaner->NextToken(); while(m_token.type==LOGICAL_OR){ CTreeNode*temp=newExpNode(kOp,m_token.type,m_token.str); temp->child[0]=p; p=temp; if(p->child[0])p->child[0]->father=p; m_token=m_pScaner->NextToken(); if((p->child[1]=logic2_expression()))p->child[1]->father=p; m_token=m_pScaner->NextToken(); m_pScaner->PushBack();//putthenexttokenback returnp;CTreeNode*CParser::simple_expression() CTreeNode*p=additive_expression(); m_token=m_pScaner->NextToken(); if(m_token.type==NGT||m_token.type==LT||m_token.type==GT|| m_token.type==NLT||m_token.type==EQ||m_token.type==NEQ){ CTreeNode*temp=newExpNode(kOp,m_token.type,m_token.str); temp->child[0]=p; p=temp; if(p->child[0])p->child[0]->father=p; m_token=m_pScaner->NextToken(); if((p->child[1]=additive_expression()))p->child[1]->father=p; }else m_pScaner->PushBack(); returnp;CTreeNode*CParser::additive_expression() CTreeNode*p=term(); m_token=m_pScaner->NextToken(); while(m_token.type==PLUS||m_token.type==MINUS){ CTreeNode*temp=newExpNode(kOp,m_token.type,m_token.str); temp->child[0]=p; p=temp; if(p->child[0])p->child[0]->father=p; m_token=m_pScaner->NextToken(); if((p->child[1]=term()))p->child[1]->father=p; m_token=m_pScaner->NextToken(); m_pScaner->PushBack();//putthenexttokenback returnp;CTreeNode*CParser::term() CTreeNode*p=logic3_expression(); m_token=m_pScaner->NextToken(); while(m_token.type==TIMES||m_token.type==DIV||m_token.type==MOD){ CTreeNode*temp=newExpNode(kOp,m_token.type,m_token.str); temp->child[0]=p; p=temp; if(p->child[0])p->child[0]->father=p; m_token=m_pScaner->NextToken(); if((p->child[1]=logic3_expression()))p->child[1]->father=p; m_token=m_pScaner->NextToken(); m_pScaner->PushBack();//putthenexttokenback returnp;CTreeNode*CParser::logic3_expression() CTreeNode*p=NULL,*temp=NULL; if(m_token.type==LOGICAL_NOT){ p=newExpNode(kOp,m_token.type,m_token.str); m_token=m_pScaner->NextToken(); if((temp=factor())){ p->child[0]=temp; p->child[0]->father=p; }else p=factor(); returnp;判斷單詞合法性與出錯恢復(fù)過程分析://forerrorrecoveryvoidCParser::ConsumeUntil(enumTokenTypetype) while(m_token.type!=type&&m_token.type!=_EOF) m_token=m_pScaner->NextToken();voidCParser::ConsumeUntil(enumTokenTypetype1,enumTokenTypetype2) while(m_token.type!=type1&&m_token.type!=type2&&m_token.type!=_EOF) m_token=m_pScaner->NextToken();系統(tǒng)測試與運行結(jié)果分析測試程序:/*StudentInformationManagementSystem*/intmainmenu(intstu)//mainmenuforuseinterface intret; printf("**************StudentInformationManagementSystem**************\n"); printf("\t1input\n"); printf("\t2sort\n"); printf("\t0eixt\n"); printf("\tPleasechooseachoice:\n"); printf("*****************************************************************\n"); read(ret); returnret; intaverage(inta[],intn) intsum; inti; sum=0; i=0; while(i<n) sum=sum+a[i]; i=i+1; su

溫馨提示

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

評論

0/150

提交評論