?? prase.cpp
字號:
m_Entoken=m_pScaner->getToken();
CTreeNode *t=newStmtNode(ForK);
CTreeNode *p=t;
CTreeNode *p2;
match(LLPAREN,"missing '(' after 'for'");
p->m_pchild[0]=expression();
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
p=p->m_pchild[0];
p->m_pbrother=expression();
p=p->m_pbrother;
m_EnEndexp=ERPAREN; //指示下一個表達式將以)結(jié)尾。
p->m_pbrother=expression();
match(LGPAREN,"missing '{' after 'for'");
p2=t->m_pchild[1]=compound_stmt();
if(p2) p2->m_pfather=t;
while(p2 && p2->m_pbrother){
p2=p2->m_pbrother;
p2->m_pfather=t;
}
return t;
}
/***************************************************************
**文法:jump_stmt->goto ID;
****************************************************************/
CTreeNode *CPraser::jump_stmt(void){
CTreeNode *t=newStmtNode(GotoK);
m_Entoken=m_pScaner->getToken();
strcpy(m_strIDname, m_pScaner->GettokenString());
if(m_Entoken!=ID)
throw CInvalid_type(AllGlobals.lineno,"goto an illegal destination");
t->m_pchild[0]=newExpNode(IdK);
if(t->m_pchild[0]) t->m_pchild[0]->m_pfather=t;
m_Entoken=m_pScaner->getToken();
match(SEMI,"missing ';'.");
return t;
}
/*************************************************
**文法:break_stmt->break;
*************************************************/
CTreeNode *CPraser::break_stmt(void){
CTreeNode *t=newStmtNode(BreakK);
m_Entoken=m_pScaner->getToken();
match(SEMI, "missing ';'");
return t;
}
/*************************************************
**文法:continue_stmt->continue;
************************************************/
CTreeNode *CPraser::continue_stmt(void){
CTreeNode *t=newStmtNode(ContinueK);
m_Entoken=m_pScaner->getToken();
match(SEMI,"missing ';'");
return t;
}
/**********************************************
**文法:return_stmt->return;|return expression;
***********************************************/
CTreeNode *CPraser::return_stmt(void){
CTreeNode *t=newStmtNode(ReturnK);
m_Entoken=m_pScaner->getToken();
strcpy(m_strIDname, m_pScaner->GettokenString());
if(m_Entoken==SEMI) m_Entoken=m_pScaner->getToken();
else{
t->m_pchild[0]=expression();
if(t->m_pchild[0]) t->m_pchild[0]->m_pfather=t;}
return t;
}
/**************************************************
**文法:expression->var=expression|logic1_expression
***************************************************/
CTreeNode *CPraser::expression(void){
CTreeNode *t=logic1_expression();
CTreeNode *p=t;
if(m_Entoken==ASSIGH){
if(t->m_pchild[0]!=NULL || t->m_EnTypevalue!=ID)//賦值語句左邊不能是個復雜表達式。
throw CInvalid_type(AllGlobals.lineno,"expression error.");
p=newExpNode(OpK); //建立賦值結(jié)點。
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
READSYMBOL
t=expression();
p->m_pchild[1]=t;
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
else{
if(m_EnEndexp==ERPAREN){
m_EnEndexp=ESEMI; //恢復表達式以分號結(jié)束的缺省狀態(tài)。
if(m_Entoken!=RLPAREN) //表達式結(jié)束處,只能是).
throw CInvalid_type(AllGlobals.lineno,"missing ')' after expression");}
else if(m_EnEndexp==ECOMMA){ //對于表達式由逗號結(jié)束的情況,可能讀到逗號(后沒有結(jié)
if(m_Entoken==COMMA){} //束,繼續(xù),可能讀到右括號,則通知調(diào)用函數(shù)。
else if(m_Entoken==RLPAREN) m_EnEndexp=ERPAREN;
else throw CInvalid_type(AllGlobals.lineno,"missing ',' after expression");
return p;}
else{
if(m_Entoken!=SEMI) //表達式結(jié)尾處不能出現(xiàn)三種情況以外的情況。
throw CInvalid_type(AllGlobals.lineno,"missing ';' after expression");}
//表達式處理完畢,則取消向前多讀一個記號的狀態(tài)(這由最初的設計缺陷造成)
m_Entoken=m_Enforexp;
strcpy(m_strIDname, m_pScaner->GettokenString());
m_Enforexp=ERROR;
}
return p;
}
/***************************************************************
**文法:logic1_expression->logic1_expression || logic2_expression()|logic2_expression
****************************************************************/
CTreeNode *CPraser::logic1_expression(void){
CTreeNode *t=logic2_expression();
CTreeNode *p=t;
while(m_Entoken==OR){
CTreeNode *p=newExpNode(OpK);
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
t=p;
READSYMBOL
p->m_pchild[1]=logic2_expression();
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
return t;
}
/******************************************************************
**文法: logic2_expression-> logic2_expression && simple_expression | simple_expression
*********************************************************************/
CTreeNode *CPraser::logic2_expression(void){
CTreeNode *t=simple_expression();
while(m_Entoken==AND){
CTreeNode *p=newExpNode(OpK);
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
t=p;
READSYMBOL
p->m_pchild[1]=simple_expression();
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
return t;
}
/***************************************************************
**文法:simple_expression->add_expression relop add_expression|add_expression
** relop-> <=|>=|>|<|==;
******************************************************************/
CTreeNode *CPraser::simple_expression(void){
CTreeNode *t=add_expression();
CTreeNode *p=t;
while(m_Entoken>=EQ && m_Entoken<=NGT){
p=newExpNode(OpK);
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
t=p;
READSYMBOL
p->m_pchild[1]=add_expression();
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
return t;
}
/******************************************************************
** 文法: add_expression -> add_expression addop term | term
** addop-> +|-
*****************************************************************/
CTreeNode *CPraser::add_expression(void){
CTreeNode *t=term();
while(m_Entoken==PLUS || m_Entoken==MINUS){
CTreeNode *p=newExpNode(OpK);
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
t=p;
READSYMBOL
p->m_pchild[1]=term();
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
return t;
}
/****************************************************************
** 文法:term->logic3_expression mulop term|logic3_expression
** mulop-> *|/
*****************************************************************/
CTreeNode *CPraser::term(void){
CTreeNode *t=logic3_expression();
while(m_Entoken==TIMES || m_Entoken==OVER){
CTreeNode *p=newExpNode(OpK);
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
t=p;
READSYMBOL
p->m_pchild[1]=logic3_expression();
if(p->m_pchild[1]) p->m_pchild[1]->m_pfather=p;
}
return t;
}
/***************************************************************
**文法: logic3_expression->!logic3_expression|factor
***************************************************************/
CTreeNode *CPraser::logic3_expression(void){
CTreeNode *t=NULL, *p=NULL;
if(m_Entoken==NOT){
p=newExpNode(OpK);
READSYMBOL
t=factor();
p->m_pchild[0]=t;
if(p->m_pchild[0]) p->m_pchild[0]->m_pfather=p;
}
else{
p=factor();}
return p;
}
/*******************************************************************
**文法: factor->(expression)|ID|NUM
*******************************************************************/
CTreeNode *CPraser::factor(void){
CTreeNode *t=NULL;
char *p=NULL;int i=0;
switch(m_Entoken){
case NUM:
p=m_strIDname; //對數(shù)字進行有效性檢查(不可出現(xiàn)兩個小數(shù)點)
while(*p!='\0'){
if(*p=='.') i++;
p++;}
if(i>1) throw CInvalid_type(AllGlobals.lineno,"Invalid numeric");
t=newExpNode(ConstK);
break;
case ID:
if(m_Enforexp==LLPAREN) t=call_func();
else t=newExpNode(IdK);
break;
case CCHAR:
t=newExpNode(ConstK);
break;
case LLPAREN:
READSYMBOL
m_EnEndexp=ERPAREN; //指示下一個表達式將以)結(jié)束。
t=expression();
m_Enforexp=m_pScaner->getToken();
return t;
default:
throw CInvalid_type(AllGlobals.lineno,"expression error.");
break;
}
if(m_Enforexp!=ERROR){ //m_Enforexp不為ERROR,說明它保留著下一個記號,不必從文件中讀取了。
READSYMBOL}
else{
m_Entoken=m_pScaner->getToken();
strcpy(m_strIDname, m_pScaner->GettokenString());
m_Enforexp=m_pScaner->getToken();
}//由于設計上的缺陷,在表達式處理時,掃描器始終保持向前多讀一個記號,而其他部分則不具備
//這個功能,所以必須在這兩者之間轉(zhuǎn)換。
return t;
}
/****************************************
**建一個新的函數(shù)結(jié)點或聲明結(jié)點。
*****************************************/
CTreeNode *CPraser::newNode(NodeKind pa_kind){
CTreeNode *t=new CTreeNode;
t->m_pchild[0]=NULL;
t->m_pchild[1]=NULL;
t->m_pchild[2]=NULL;
t->m_pfather=NULL;
t->m_pbrother=NULL;
t->m_Ennodekind=pa_kind;
t->m_EnTypevalue=m_Enfirst;
strcpy(t->m_strIDname, m_strIDname);
strcpy(t->m_strScope, m_strScope);
t->m_ilineno=AllGlobals.lineno;
return t;
}
/**************************************************
**建一個語句體結(jié)點。
****************************************************/
CTreeNode *CPraser::newStmtNode(StmtKind pa_subkind){
CTreeNode *t= new CTreeNode;
t->m_pchild[0]=NULL;
t->m_pchild[1]=NULL;
t->m_pchild[2]=NULL;
t->m_pfather=NULL;
t->m_pbrother=NULL;
t->m_Ennodekind=StmtK;
strcpy(t->m_strScope, m_strScope);
t->kind.m_EnStmtKind=pa_subkind;
t->m_ilineno=AllGlobals.lineno;
return t;
}
/***************************************************
**建一個表達式結(jié)點。
***************************************************/
CTreeNode *CPraser::newExpNode(ExpKind pa_subkind){
CTreeNode *t= new CTreeNode;
t->m_pchild[0]=NULL;
t->m_pchild[1]=NULL;
t->m_pchild[2]=NULL;
t->m_pfather=NULL;
t->m_pbrother=NULL;
t->m_Ennodekind=ExpK;
t->kind.m_EnExpKind=pa_subkind;
t->m_EnTypevalue=m_Entoken;
strcpy(t->m_strIDname, m_strIDname);
strcpy(t->m_strScope, m_strScope);
t->m_ilineno=AllGlobals.lineno;
return t;
}
/*************************************************
**匹配掉程序中的特殊記號,如:{ (等。
*************************************************/
void CPraser::match(TokenType pa_expcted,string pa_errorifno){
TokenType token=m_Entoken;
m_Entoken=m_pScaner->getToken();
strcpy(m_strIDname, m_pScaner->GettokenString());
if(token!=pa_expcted)
throw CInvalid_type(AllGlobals.lineno,pa_errorifno);
return;
}
/************************************************
**打印出語法樹。
***********************************************/
void CPraser::PrintTree(CTreeNode *pa_Ctree){
int i;
m_indentno+=4; //用于控制縮進格式。
while(pa_Ctree!=NULL){
for(i=0;i<m_indentno;i++) cout<<" ";
if(pa_Ctree->m_Ennodekind==StmtK){
switch(pa_Ctree->kind.m_EnStmtKind){
case IfK: cout<<"if"<<endl; break;
case WhileK: cout<<"while"<<endl; break;
case ForK: cout<<"for"<<endl; break;
case GotoK: cout<<"goto"<<endl; break;
case BreakK: cout<<"break"<<endl; break;
case ContinueK: cout<<"continue"<<endl; break;
case ReturnK: cout<<"return"<<endl; break;
case AddressK: cout<<"Address"<<endl; break;
case CallK: cout<<"FuncCall"<<endl; break;
case WritebK: cout<<"writechar"<<endl; break;
case WritedK: cout<<"writeint"<<endl; break;
default:
cout<<"Unknown ExpNode kind"<<endl;
break;}
}
else if(pa_Ctree->m_Ennodekind==ExpK){
switch(pa_Ctree->kind.m_EnExpKind){
case OpK:
cout<<"Op: ";
m_pScaner->printToken(pa_Ctree->m_EnTypevalue,"\0");
break;
case ConstK:
cout<<"const: "<<pa_Ctree->m_strIDname<<endl;
break;
case IdK:
cout<<"ID: "<<pa_Ctree->m_strIDname<<endl;
break;
default:
cout<<"Unknown ExpNode kind"<<endl;
break;}
}
else if(pa_Ctree->m_Ennodekind==FuncK){
cout<<"Function: ";
for(i=8;i<12;i++){
if(m_pScaner->m_ReservedWords[i].m_Enwords==pa_Ctree->m_EnTypevalue){
cout<<m_pScaner->m_ReservedWords[i].m_chwords<<" ";
break;}
}
cout<<pa_Ctree->m_strIDname<<endl;}
else if(pa_Ctree->m_Ennodekind==ParaK){
cout<<"parameters: ";
for(i=8;i<12;i++){
if(m_pScaner->m_ReservedWords[i].m_Enwords==pa_Ctree->m_EnTypevalue){
cout<<m_pScaner->m_ReservedWords[i].m_chwords<<" ";
break;}
}
cout<<pa_Ctree->m_strIDname<<endl;}
else if(pa_Ctree->m_Ennodekind==DeclK){
cout<<"declaration: ";
for(i=8;i<12;i++){
if(m_pScaner->m_ReservedWords[i].m_Enwords==pa_Ctree->m_EnTypevalue){
cout<<m_pScaner->m_ReservedWords[i].m_chwords<<" ";
break;}
}
cout<<pa_Ctree->m_strIDname<<endl;}
else
cout<<"Unknown ExpNode kind"<<endl;
for(i=0;i<3;i++)
PrintTree(pa_Ctree->m_pchild[i]);
pa_Ctree=pa_Ctree->m_pbrother;
}
m_indentno-=4;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -