?? symbtable.cpp
字號:
}
/***********************************************************/
/* 函數名 Compat */
/* 功 能 判斷類型是否相容 */
/* 說 明 由于TINY語言中只有整數類型、字符類型、數組類型和 */
/* 記錄類型,故類型相容等于類型等價,只需判斷每個結 */
/* 構類型的內部表示產生的指針值是否相同即可。 */
/***********************************************************/
int Compat(TypeIR * tp1,TypeIR * tp2)
{
int present;
if (tp1!=tp2)
present = FALSE; /*類型不等*/
else
present = TRUE; /*類型等價*/
return present;
}
/***********************************************************/
/* 函數名 NewTy */
/* 功 能 創建當前空類型內部表示 */
/* 說 明 參數為類型,函數返回該類型的內部表示的地址 */
/***********************************************************/
TypeIR * NewTy(TypeKind kind)
{
/* 內存中動態申請分配單元,
返回指向該單元的類型內部表示類型指針t */
TypeIR * table = (TypeIR *) malloc(sizeof(TypeIR));
/* 類型內部表示類型指針table為NULL,
未能成功分配內存單元將顯示提示錯誤信息*/
if (table==NULL)
{
fprintf(listing,"Out of memory error !");
Error = TRUE;
}
/* 類型內部表示類型指針table不是NULL,內存單元已經成功分配 */
else
switch(kind)
{
case intTy:
case charTy:
case boolTy:
table->kind = kind;
table->size = 1;
break;
case arrayTy:
table->kind = arrayTy;
table->More.ArrayAttr.indexTy = NULL;
table->More.ArrayAttr.elemTy = NULL;
break;
case recordTy:
table->kind = recordTy;
table->More.body = NULL;
break;
}
return table;
}
/***********************************************************/
/* 函數名 NewBody */
/* 功 能 創建當前空記錄類型中域的鏈表 */
/* 說 明 函數返回該類型的新的鏈表的單元地址 */
/***********************************************************/
fieldChain * NewBody(void)
{
fieldChain * Ptr = (fieldChain *) malloc(sizeof(fieldChain));
if (Ptr==NULL)
{
fprintf(listing,"Out of memory error !");
Error = TRUE;
}
else
{
Ptr->Next = NULL;
Ptr->off = 0;
Ptr->UnitType = NULL;
}
return Ptr;
}
/***********************************************************/
/* 函數名 NewParam */
/* 功 能 創建當前空形參鏈表 */
/* 說 明 函數返回新申請的單元地址 */
/***********************************************************/
ParamTable * NewParam(void)
{
ParamTable * Ptr = (ParamTable *) malloc(sizeof(ParamTable));
if (Ptr==NULL)
{
fprintf(listing,"Out of memory error !");
Error = TRUE;
}
else
{
Ptr->entry = NULL;
Ptr->next = NULL;
}
return Ptr;
}
/***********************************************************/
/* 函數名 ErrorPrompt */
/* 功 能 錯誤提示 */
/* 說 明 在輸出文件中顯示錯誤提示,并給全局量Error賦值為1 */
/***********************************************************/
void ErrorPrompt(int line,char * name,char * message)
{
fprintf(listing,">>>Line: %d, %s %s",line,name,message);
Error = TRUE;
exit(0);
}
/***********************************************************/
/* 函數名 printTab */
/* 功 能 打印空格 */
/* 說 明 在輸出文件中打印個數為參數tabnum的空格 */
/***********************************************************/
void printTab(int tabnum)
{
for(int i=0;i<tabnum;i++)
fprintf(listing," ");
}
/***********************************************************/
/* 函數名 printTable */
/* 功 能 把符號表在輸出文件中顯示出來 */
/* 說 明 分層顯示符號表內容,并在下面顯示該符號的類型內部*/
/* 表示 */
/***********************************************************/
/*void printTable(void)
{
SymbTable * table = NULL;
TypeIR * tp = NULL;
//fprintf(listing,"\n symble table:\n\n");
/*while*if (scope[Level]!=NULL)
{
fprintf(listing,"\n------ level: %d ------\n",Level);
table = scope[Level];
while (table!=NULL)
{
switch(table->attrIR.kind)
{
case typeKind:
fprintf(listing,"typeDec:\n");
printTab(5);
fprintf(listing,"name->%s;\n",table->idName);
printTab(5);
tp = table->attrIR.idtype;
if(tp!=NULL)
printTy(tp);
else
fprintf(listing,"type error!\n");
break;
case varKind:
fprintf(listing,"varDec:\n");
printTab(5);
printVar(table);
break;
case procKind:
fprintf(listing,"procDec:\n");
printTab(5);
printProc(table);
break;
}
table = table->next;
}
}
}
*/
/***********************************************************/
/* 函數名 printTy */
/* 功 能 把類型內部表示在輸出文件中顯示出來 */
/* 說 明 顯示其類型名,大小,及其他相關信息 */
/***********************************************************/
/*void printTy(TypeIR * ty)
{
switch(ty->kind)
{
case intTy:
fprintf(listing,"kind->intTy;\n");
printTab(5);
fprintf(listing,"size->%d;\n",ty->size);
break;
case charTy:
fprintf(listing,"kind->charTy;\n");
printTab(5);
fprintf(listing,"size->%d;\n",ty->size);
break;
case arrayTy:
fprintf(listing,"kind->arrayTy;\n");
printTab(5);
fprintf(listing,"size->%d;\n",ty->size);
printTab(5);
if(ty->More.ArrayAttr.indexTy->kind==intTy)
fprintf(listing,"indextype->intTy;\n");
else
fprintf(listing,"indextype->charTy;\n");
printTab(5);
if(ty->More.ArrayAttr.elemTy->kind==intTy)
fprintf(listing,"elemtype->intTy;\n");
else
fprintf(listing,"elemtype->charTy;\n");
break;
case recordTy:
fprintf(listing,"kind->recordTy;\n");
printTab(5);
fprintf(listing,"size->%d;\n",ty->size);
fieldChain * tyBody = ty->More.body;
fprintf(listing,"\n");
printTab(3);
fprintf(listing,"field:\n");
while(tyBody!=NULL)
{
printTab(5);
fprintf(listing,"id->%s;\n",tyBody->id);
printTab(5);
fprintf(listing,"unit");
printTy(tyBody->UnitType);
printTab(5);
fprintf(listing,"off->%d;\n",tyBody->off);
tyBody = tyBody->Next;
fprintf(listing,"\n");
}
break;
}
}
*/
/***********************************************************/
/* 函數名 printVar */
/* 功 能 把變量內部表示在輸出文件中顯示出來 */
/* 說 明 顯示其變量名稱,偏移,類型名及其他 */
/***********************************************************/
/*void printVar(SymbTable * entry)
{
fprintf(listing,"name->%s;\n",entry->idName);
AttributeIR attr = entry->attrIR;
printTab(5);
fprintf(listing,"level->%d;\n",attr.More.VarAttr.level);
printTab(5);
fprintf(listing,"off->%d;\n",attr.More.VarAttr.off);
printTab(5);
if(attr.More.VarAttr.isParam==true)
fprintf(listing,"param\n");
else
fprintf(listing,"not param\n");
printTab(5);
if(attr.More.VarAttr.access==dir)
fprintf(listing,"access->dir;\n");
else
fprintf(listing,"access->indir;\n");
printTab(5);
fprintf(listing,"\n");
if(entry->attrIR.idtype!=NULL)
{
printTab(5);
printTy(entry->attrIR.idtype);
}
}
*/
/***********************************************************/
/* 函數名 printProc */
/* 功 能 把過程內部表示在輸出文件中顯示出來 */
/* 說 明 顯示過程名,層數 */
/***********************************************************/
/*void printProc(SymbTable * entry)
{
SymbTable * entry0 = NULL;
fprintf(listing,"name->%s;\n",entry->idName);
AttributeIR attr = entry->attrIR;
printTab(5);
fprintf(listing,"level->%d;\n",attr.More.ProcAttr.level);
ParamTable * ptable = attr.More.ProcAttr.param;
printTab(5);
fprintf(listing,"noff->%d; \n",entry->attrIR.More.ProcAttr.nOff);
printTab(5);
fprintf(listing,"moff->%d; \n",entry->attrIR.More.ProcAttr.mOff);
}
*/
/********************************************************/
/* 函數名 FindField */
/* 功 能 查找紀錄的域名 */
/* 說 明 返回值為是否找到標志,變量Entry返回此域名在 */
/* 紀錄的域表中的位置. */
/********************************************************/
bool FindField(char * Id , fieldChain *head,fieldChain **Entry )
{
bool present =false;
/*記錄當前節點*/
fieldChain *currentItem = head;
/*從表頭開始查找這個標識符,直到找到或到達表尾*/
while ((currentItem!=NULL)&&( present==false))
{
if (strcmp(currentItem->id, Id )==0)
{ present= true;
if (Entry!=NULL)
(*Entry)=currentItem;
}
else currentItem=currentItem->Next;
}
return(present);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -