?? analyze.cpp
字號:
/* 不是該記錄類型中的域成員。 */
/************************************************************/
TypeIR * recordVar(TreeNode * t)
{
int present = FALSE;
int result = TRUE;
SymbTable * entry = NULL;
TypeIR * Eptr0 = NULL;
TypeIR * Eptr1 = NULL;
TypeIR * Eptr = NULL;
fieldchain * currentP = NULL;
/*在符號表中查找此標識符*/
present = FindEntry(t->name[0],&entry);
t->table[0] = entry;
/*找到*/
if(present!=FALSE)
{
/*Var0不是變量*/
if (FindAttr(entry).kind!=varKind)
{
ErrorPrompt(t->lineno,t->name[0],"is not variable error!\n");
Eptr = NULL;
}
else/*Var0不是記錄類型變量*/
if(FindAttr(entry).idtype->kind!=recordTy)
{
ErrorPrompt(t->lineno,t->name[0],"is not record variable error !\n");
Eptr = NULL;
}
else/*檢查id是否是合法域名*/
{
Eptr0 = entry->attrIR.idtype;
currentP = Eptr0->More.body;
while((currentP!=NULL)&&(result!=FALSE))
{
result = strcmp(t->child[0]->name[0],currentP->id);
/*如果相等*/
if(result==FALSE)
Eptr = currentP->UnitType;
else
currentP = currentP->Next;
}
if(currentP==NULL)
if(result!=FALSE)
{
ErrorPrompt(t->child[0]->lineno,t->child[0]->name[0],
"is not field type!\n");
Eptr = NULL;
}
else/*如果id是數組變量*/
if(t->child[0]->child[0]!=NULL)
Eptr = arrayVar(t->child[0]);
}
}
else/*標識符無聲明*/
ErrorPrompt(t->lineno,t->name[0],"is not declarations!\n");
return Eptr;
}
/************************************************************/
/* 函數名 assignstatement */
/* 功 能 該函數處理賦值語句分析 */
/* 說 明 賦值語句的語義分析的重點是檢查賦值號兩端分量的類 */
/* 型相容性。 */
/************************************************************/
void assignstatement(TreeNode * t)
{
SymbTable * entry = NULL;
int present = FALSE;
TypeIR * ptr = NULL;
TypeIR * Eptr = NULL;
TreeNode * child1 = NULL;
TreeNode * child2 = NULL;
child1 = t->child[0];
child2 = t->child[1];
if(child1->child[0]==NULL)
{
/*在符號表中查找此標識符*/
present = FindEntry(child1->name[0],&entry);
if(present!=FALSE)
{ /*id不是變量*/
if (FindAttr(entry).kind!=varKind)
{
ErrorPrompt(child1->lineno,child1->name[0],"is not variable error!\n");
Eptr = NULL;
}
else
{
Eptr = entry->attrIR.idtype;
child1->table[0] = entry;
}
}
else /*標識符無聲明*/
ErrorPrompt(child1->lineno,child1->name[0],"is not declarations!\n");
}
else/*Var0[E]的情形*/
{ if(child1->attr.ExpAttr.varkind==ArrayMembV)
Eptr = arrayVar(child1);
else /*Var0.id的情形*/
if(child1->attr.ExpAttr.varkind==FieldMembV)
Eptr = recordVar(child1);
}
if(Eptr != NULL)
{
if((t->nodekind==StmtK)&&(t->kind.stmt==AssignK))
{
/*檢查是不是賦值號兩側 類型等價*/
ptr = Expr(child2,NULL);
if (!Compat(ptr,Eptr))
ErrorPrompt(t->lineno,"","ass_expression error!\n");
}
/*賦值語句中不能出現函數調用*/
}
}
/************************************************************/
/* 函數名 callstatement */
/* 功 能 該函數處理函數調用語句分析 */
/* 說 明 函數調用語句的語義分析首先檢查符號表求出其屬性中 */
/* 的Param部分(形參符號表項地址表),并用它檢查形參*/
/* 和實參之間的對應關系是否正確。 */
/************************************************************/
void callstatement(TreeNode * t)
{
AccessKind Ekind;
int present = FALSE;
SymbTable * entry=NULL;
TreeNode * p = NULL;
/*用id檢查整個符號表*/
present = FindEntry(t->child[0]->name[0],&entry);
t->child[0]->table[0] = entry;
/*未查到表示函數無聲明*/
if (present==FALSE)
{
ErrorPrompt(t->lineno,t->child[0]->name[0],"function is not declarationed!\n");
}
else
/*id不是函數名*/
if (FindAttr(entry).kind!=procKind)
ErrorPrompt(t->lineno,t->name[0],"is not function name!\n");
else/*形實參匹配*/
{
p = t->child[1];
/*paramP指向形參符號表的表頭*/
ParamTable * paramP = FindAttr(entry).More.ProcAttr.param;
while((p!=NULL)&&(paramP!=NULL))
{
SymbTable * paraEntry = paramP->entry;
TypeIR * Etp = Expr(p,&Ekind);/*實參*/
/*參數類別不匹配*/
if ((FindAttr(paraEntry).More.VarAttr.access==indir)&&(Ekind==dir))
ErrorPrompt(p->lineno,"","param kind is not match!\n");
else
/*參數類型不匹配*/
if((FindAttr(paraEntry).idtype)!=Etp)
ErrorPrompt(p->lineno,"","param type is not match!\n");
p = p->sibling;
paramP = paramP->next;
}
/*參數個數不匹配*/
if ((p!=NULL)||(paramP!=NULL))
ErrorPrompt(t->child[1]->lineno,"","param num is not match!\n");
}
}
/************************************************************/
/* 函數名 ifstatement */
/* 功 能 該函數處理條件語句分析 */
/* 說 明 分析語法樹的三個兒子節點 */
/************************************************************/
void ifstatment(TreeNode * t )
{
AccessKind * Ekind = NULL;
TypeIR * Etp = Expr(t->child[0],Ekind);
if(Etp!=NULL)
/*處理條件表達式*/
if (Etp->kind!= boolTy)
ErrorPrompt(t->lineno,"","condition expressrion error!\n"); /*邏輯表達式錯誤*/
else
{
TreeNode * p = t->child[1];
/*處理then語句序列部分*/
while ( p!=NULL)
{
statement(p);
p=p->sibling;
}
t = t->child[2]; /*必有三兒子*/
/*處理else語句不分*/
while ( t!=NULL)
{
statement(t);
t=t->sibling;
}
}
}
/************************************************************/
/* 函數名 whilestatement */
/* 功 能 該函數處理循環語句分析 */
/* 說 明 分析語法樹的兩個兒子節點 */
/************************************************************/
void whilestatement(TreeNode * t)
{
TypeIR * Etp = Expr(t->child[0],NULL);
if (Etp!=NULL)
/*處理條件表達式部分*/
if (Etp->kind!= boolTy)
ErrorPrompt(t->lineno,"","condition expression error!\n"); /*邏輯表達式錯誤*/
else
{
t = t->child[1];
/*處理循環部分*/
while ( t!=NULL)
{
statement(t);
t=t->sibling;
}
}
}
/************************************************************/
/* 函數名 readstatement */
/* 功 能 該函數處理輸入語句分析 */
/* 說 明 分析語法樹節點,檢查變量有無聲明和是否為變量錯誤 */
/************************************************************/
void readstatement (TreeNode * t)
{
SymbTable * entry=NULL;
int present = FALSE;
/*用id檢查整個符號表*/
present = FindEntry(t->name[0],&entry);
t->table[0] = entry;
/*未查到表示變量無聲明*/
if (present==FALSE)
ErrorPrompt(t->lineno,t->name[0], " is not declarationed!\n");
else
/*不是變量標識符錯誤*/
if(entry->attrIR.kind!=varKind)
ErrorPrompt(t->lineno,t->name[0],"is not var name!\n ");
}
/************************************************************/
/* 函數名 writestatement */
/* 功 能 該函數處理輸出語句分析 */
/* 說 明 分析輸出語句中的表達式是否合法 */
/************************************************************/
void writestatement(TreeNode * t )
{
TypeIR * Etp = Expr (t->child[0],NULL);
if(Etp!=NULL)
/*如果表達式類型為bool類型,報錯*/
if (Etp->kind==boolTy)
ErrorPrompt(t->lineno,"","exprssion type error!");
}
/************************************************************/
/* 函數名 returnstatement */
/* 功 能 該函數處理函數返回語句分析 */
/* 說 明 分析函數返回語句是否在主程序中出現 */
/************************************************************/
void returnstatement(TreeNode * t )
{
if(Level==0)
/*如果返回語句出現在主程序中,報錯*/
ErrorPrompt(t->lineno,"","return statement error!");
}
/************************************************************/
/* 函數名 analyze */
/* 功 能 該函數處理總的語義分析 */
/* 說 明 對語法樹進行分析 */
/************************************************************/
void analyze(TreeNode * t)
{
SymbTable * entry= NULL;
TreeNode * p = NULL;
TreeNode * pp = t;
/*創建符號表*/
CreatTable();
/*調用類型內部表示初始化函數*/
initialize();
/*語法樹的聲明節點*/
p=t->child[1];
while (p!=NULL)
{
switch ( p->nodekind )
{ case TypeK: TypeDecPart(p->child[0]); break ;
case VarK : VarDecPart(p->child[0]); break ;
case ProcDecK: procDecPart(p); break ;
default:
ErrorPrompt(p->lineno,"","no this node kind in syntax tree!");
break;
}
p = p->sibling ;/*循環處理*/
}
/*程序體*/
t = t->child[2];
if(t->nodekind==StmLK)
Body(t);
/*撤銷符號表*/
if (Level!=-1)
DestroyTable();
/*輸出語義錯誤*/
if(Error==TRUE)
fprintf(listing,"\nanalyze error:\n");
/*如果無錯誤,則輸出提示信息*/
else
fprintf(listing,"\n........ no error!\n");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -