编译原理课程设计报告模板_第1页
编译原理课程设计报告模板_第2页
编译原理课程设计报告模板_第3页
编译原理课程设计报告模板_第4页
编译原理课程设计报告模板_第5页
已阅读5页,还剩22页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

《编译原理课程设计》报告MiniC编译器MFC实现分组序号:5设计地点:微机301、401电子邮件:1260815873@指导教师:李村合技术支持:CSDN下载:百度文库下载:迷若烟雨版权所有迷若烟雨版权所有 仅用于学习、交流,侵权必究仅用于学习、交流,侵权必究 2011年12月24日

目录1 前言 功能需求1、用C++语言实现了类C语言,称为扩展的MiniC语言,即MiniC语言的编译器。 2、文法是LL(1)文法,采用递归子程序法实现语法分析,并用C语言实现了词法分析器、语法分析器、代码生成器,能直接生成80x96运行的可执行文件。 3、在声明中实现了对静态常量、变量、数组和过程的声明支持;在赋值语句中实现了+=、++、-=、--、*=、/=、%=、:=(赋值);数学运算支持+、-、*、/(/除法编译可以通过,但汇编时会出现溢出错误!正在努力修正。 4、使用函数实现了对过程的调用。 5、使用{}实现了复合语句。 6、使用read()来同时读入一个到多个数据,使用write()来同时输出一个到多个数据。printf()来输出一个字符串。 7、在循环分支语句中实现了if语句,if……else……语句,for(…,……,……)…语句,while……语句。 8、条件语句包括奇偶判断、=(等于)、#(不等于)、<、<=、>、>=。主要用到的关键词:关键字: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汇编语言,汇编成可执行文件,可在相应机型的机器上运行。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"["//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=="("//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"//13.`write``(`expression`)``;`//m_token.str=="write"//14.`printf``(``"`STRING`"``)``;`//m_token.str=="printf"//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=="("//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"//33.while_stmt->`while``(`expression`)`compound_stmt//m_token.str=="while"//34.for_stmt->`for``(`var`=`expression`;`expression`;`var`=`expression`)`compound_stmt//m_token.str=="for"//35.goto_stmt->`goto`ID`;`//m_token.str=="goto"//36.break_stmt->`break``;`//m_token.str=="break"//37.continue_stmt->`continue``;`//m_token.str="continue"//38.return_stmt->`return``;`|`return`expression`;`//m_token.str="return"系统设计与实现BY5编译程序主要模块功能

表1BY5编译程序的过程或函数的功能表过程或函数名简要功能说明CTokenizer::NextToken()略去空格、回车、注释取得输入串的下一个有效字符CScaner::NextToken()取得下一个有效字符的相关属性CParser::BuildSyntaxTree()构造输入串的语法树CAnalyzer::BuildSymbolTable()构造符号表CAnalyzer::traverse遍历语法树,并设置相应的属性CAnalyzer::TraceTypeCheck()语义分析处理CAsmCodeGenerator::GenerateAsmCode生成汇编语言代码CBY5Doc::OnCompileBuildAsm()调用语义类,生成汇编代码CBY5Doc::OnCompileBuildExe()调用汇编程序,生成可执行文件OnCompileRun()运行生成的可执行文件词法分析子程序词法分析类为CTokenizer,功能是从源程序中读出一个单词符号(token),把它的信息放入类中中,语法分析器需要单词时,直接从这个类中获得。词法分析器的分析过程:构造CTokenizer时,它通过构造函数从源程序中获得一个字符串。如果这个字符是字母,则继续获取字符或数字,最终可以拼成一个单词,查保留字表,如果查到为保留字,则把m_tType变量赋成相应的保留字类型值;如果没有查到,则这个单词应是一个用户自定义的标识符(可能是变量名、常量名或是过程的名字),把m_tType置为相应的关键值,把这个单词存入m_tType变量。查保留字表时使用了二分法查找以提高效率。如果m_tType获得的字符是数字,则继续用NextToken()获取数字,并把它们拼成一个整数,然后把m_tType置为number,并把拼成的数值放入m_dVal变量。如果识别出其它合法的符号(比如:赋值号、大于号、小于等于号等),则把m_tType则成相应的类型。对应的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 //"//"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;语法语义分析子程序语法分析子程序采用了自顶向下的递归子程序法,语法分析同时也根据程序的语义生成相应的代码,并提供了出错处理的机制。语法分析主要由分程序分析过程(fun_declaration)、变量定义分析过程(var_declaration)、语句分析过程(statement)、表达式处理过程(expression)、项处理过程(term)、因子处理过程(factor)和条件处理过程(if_stmt)、循环处理过程(for_stmt)、调用子过程(call)构成。这些过程在结构上构成一个嵌套的层次结构。除此之外,还有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;表达式、项、因子处理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;逻辑表达式的处理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;判断单词合法性与出错恢复过程分析://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();系统测试与运行结果分析测试程序:/*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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论