加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
SemAnalysis.cpp 25.86 KB
一键复制 编辑 原始数据 按行查看 历史
LittleMike 提交于 2015-08-31 23:59 . Create SemAnalysis.cpp
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
#include "lexAPI.h"
#include "SemAnalysis.h"
#include<iostream>
using namespace std;
#include "SyntaxAnalysis.cpp"
//初始化
int STable::Level = 0;//从1开始,而0没有使用
SymbTable** STable::Scope = new SymbTable*[SCOPENUM];
FILE * result;
void STable::CreatTable(void)
{
//并非真正建表
Level++;
Scope[Level] = NULL;
}
void STable::DestroyTable(void)
{
if(Level > 0)
Level--;
else
{
//err
}
}
//登记
bool STable::Enter(string Id,AttrbuteIR* attribP,SymbTable** Entry)
{
*Entry = NULL;
SymbTable* q = NULL;
bool present = FindEntry(Id,true,Entry);
if(present)
{
//err
//标识符重复声明
cout<<(*Entry)->idname<<' '<<"与已有标识符重名"<<endl;
SyntaxAnalysis::dispErr();
return false;
}
SymbTable* p = new SymbTable();
p->idname = Id;
p->attrIR = (*attribP);
if(Scope[Level] == NULL)
{
Scope[Level] = p;
}
else
{
q = Scope[Level];
while(q->next != NULL)
q = q->next;
q->next = p;
}
//if(Entry)
//{
*Entry = p;
//}
return true;
}
bool STable::FindEntry(string id,bool flag,SymbTable** Entry)
{
*Entry = NULL;
bool present = SearchoneTable(id,Level,Entry);
if(flag || present)
{
return present;
}
int level = Level - 1;
while(level>0)
{
present = SearchoneTable(id,level,Entry);
if(present)
return present;
level--;
}
return false;
}
bool STable::SearchoneTable(string id,int currentLevel,SymbTable** Entry)
{
SymbTable* p = Scope[currentLevel];
//Scope[currentLevel]->idname
while(p != NULL)
{
if(p->idname == id)
{
*Entry = p;
return true;
}
p = p->next;
}
return false;
}
bool STable::FindField(string id,FieldChain* head,FieldChain** Entry)
{
*Entry = NULL;
FieldChain* p = head;
while(p != NULL)
{
if(p->idname == id)
{
*Entry = p;
return true;
}
p = p->next;
}
return false;
}
//命令行输出,临时显示
void STable::PrindSymbTable()
{
//打印符号表
SymbTable* p = NULL;
for(int level=1; level<SCOPENUM; level++)
{
p = Scope[level];
if(p == NULL)
return;
while(p != NULL)
{
cout<<p->idname<<" ";
cout<<p->attrIR.kind<<endl;
p = p->next;
}
cout<<endl;
}
}
/////////////////////////////////////////////
//打印///////////////////////////////////////////////////////////////////////////////
//打印纪录类型的域表
void STable::PrintFieldChain(FieldChain *currentP)
{
fprintf(result,"\n[Field chain]\n");
FieldChain *t=currentP;
while (t!=NULL)
{
/*输出标识符名字*/
fprintf(result ,"%s: ",t->idname.c_str());
/*输出标识符的类型信息*/
switch(t->unitType->kind)
{
case intTy :
fprintf(result ,"intTy ");
break;
case charTy:
fprintf(result ,"charTy ");
break;
case arrayTy:
fprintf(result ,"arrayTy ");
break;
case recordTy:
fprintf(result ,"recordTy ");
break;
default :
fprintf(result ,"error type! ");
break;
}
fprintf(result ,"offset = %d\n",t->offset);
t = t->next;
}
}
//打印符号表的一层
void STable::PrintOneLayer(int level)
{
SymbTable * t= Scope[level];
fprintf(result,"\n-------SymbTable in level %d ---------\n",level);
while (t!=NULL)
{
/*输出标识符名字*/
fprintf(result ,"%s: ",t->idname.c_str());
AttrbuteIR *Attrib = &(t->attrIR );
/*输出标识符的类型信息,过程标识符除外*/
if (Attrib->idtype!=NULL) /*过程标识符*/
switch(Attrib->idtype->kind)
{
case intTy :
fprintf(result ,"intTy ");
break;
case charTy:
fprintf(result ,"charTy ");
break;
case arrayTy:
fprintf(result ,"arrayTy ");
break;
case recordTy:
fprintf(result ,"recordTy ");
break;
default :
fprintf(result ,"error type! ");
break;
}
/*输出标识符的类别,并根据不同类型输出不同其它属性*/
switch(Attrib->kind)
{
case typeKind :
fprintf(result, "typekind ");
break;
case varKind :
fprintf(result, "varkind ");
fprintf(result ,"Level = %d ", Attrib->more.VarAttr.level);
if(Attrib->more.VarAttr.access == dir)
fprintf(result ,"Offset= %d ", Attrib->more.VarAttr.off - Attrib->idtype->size);
else
fprintf(result ,"Offset= %d ", Attrib->more.VarAttr.off - 1);
fprintf(result,"size = %d ",Attrib->idtype->size);
switch(Attrib->more.VarAttr.access)
{
case dir :
fprintf(result ,"dir ");
break;
case indir:
fprintf(result ,"indir ");
break;
default :
fprintf(result ,"errorkind ");
break;
}
if(Attrib->idtype->kind == recordTy)
PrintFieldChain(Attrib->idtype->more.body);
//if(Attrib->idtype->kind = arrayTy)
break;
case procKind:
fprintf(result ,"funckind ");
fprintf(result ,"Level= %d ",Attrib->more.ProcAttr.level);
fprintf(result ,"CODE= %d ",Attrib->more.ProcAttr.code);
break;
default :
fprintf(result ,"error ");
}
fprintf(result,"\n");
t = t->next;
}
}
//打印生成的符号表
void STable::PrintSymbTable( )
{
/*层数从0开始*/
int level=1;
while (Scope[level]!=NULL)
{
PrintOneLayer(level);
level++;
}
}
/********************************************/
SemAnalysis::SemAnalysis(void)
{
}
SemAnalysis::~SemAnalysis(void)
{
}
//初始化
TypeIR* SemAnalysis::intPtr = NULL;
TypeIR* SemAnalysis::charPtr = NULL;
TypeIR* SemAnalysis::boolPtr = NULL;
void SemAnalysis::initialize(void)
{
intPtr = new TypeIR(intTy);
intPtr->size = 1;
charPtr = new TypeIR(charTy);
charPtr->size = 1;
boolPtr = new TypeIR(boolTy);
boolPtr->size = 1;
for(int index=0; index<SCOPENUM; index++)
STable::Scope[index] = NULL;
}
TypeIR* SemAnalysis::TypeProcess(TreeNode* t, Kind deckind)
{
TypeIR* Ptr = NULL;
if(deckind == IdK)
Ptr = nameType(t);
if(deckind == ArrayK)
Ptr = arrayType(t);
if(deckind == RecordK)
Ptr = recordType(t);
if(deckind == IntegerK)
Ptr = intPtr;
if(deckind == CharK)
Ptr = charPtr;
return Ptr;
}
TypeIR* SemAnalysis::nameType(TreeNode* t)
{
TypeIR* Ptr = NULL;
SymbTable* entry = NULL;
bool present = STable::FindEntry(t->type_name,false,&entry);
if(present != 1)
{
//err 类型无声明错误
cout<<t->type_name<<' '<<"未知类型名"<<endl;
SyntaxAnalysis::dispErr();
return Ptr;
}
if(entry->attrIR.kind == typeKind)
Ptr = entry->attrIR.idtype;
else
{
//err 非类型标识符错
cout<<entry->idname<<' '<<"非类型标识符"<<endl;
SyntaxAnalysis::dispErr();
}
return Ptr;
}
void SemAnalysis::statement(TreeNode* t)
{
if(t->nodekind != StmtK)
{
//err
return;
}
if(t->kind.stmt == IfK)
{
ifstatement(t);
return;
}
if(t->kind.stmt == WhileK)
{
whilestatement(t);
return;
}
if(t->kind.stmt == AssignK)
{
assignstatement(t);
return;
}
if(t->kind.stmt == ReadK)
{
readstatement(t);
return;
}
if(t->kind.stmt == WriteK)
{
writestatement(t);
return;
}
if(t->kind.stmt == CallK)
{
callstatement(t);
return;
}
if(t->kind.stmt == ReturnK)
{
returnstatement(t);
return;
}
}
TypeIR* SemAnalysis::arrayType(TreeNode* t)
{
TypeIR* ptr = NULL;
if(t->attr.ArrayAttr.low > t->attr.ArrayAttr.up)
{
//err 下界大于上界
return ptr;
}
ptr = new TypeIR(arrayTy);
ptr->size = t->attr.ArrayAttr.up - t->attr.ArrayAttr.low + 1;
ptr->more.arrayAttr.indexTy = intPtr;
ptr->more.arrayAttr.elemTy = TypeProcess(t,t->attr.ArrayAttr.childType);
return ptr;
}
TypeIR* SemAnalysis::recordType(TreeNode* t)
{
TypeIR* ptr = new TypeIR(recordTy);
FieldChain* ptr2 = NULL;
FieldChain* prePtr = NULL;
int tmpsize = -1;
TreeNode* tt = t;
int off = 0;
tt = tt->child[0];
while(tt != NULL)
{
int num = 0;
while(num < (tt->idnum))//针对[类型 i,j,k...]的情形
{
ptr2 = new FieldChain();
ptr2->idname = tt->name[num];
ptr2->unitType = TypeProcess(tt,tt->kind.dec);
//prePtr = ptr2;
if(prePtr == NULL)
{
ptr2->offset = 0;
tmpsize = ptr2->unitType->size;
//将域链链入记录类型的body部分
ptr->more.body = ptr2;
}
else
{
ptr2->offset = off + ptr2->unitType->size;
off = ptr2->offset;
prePtr->next = ptr2;
}
prePtr = ptr2;
num++;
}
tt = tt->sibling;
}
ptr->size = off + tmpsize;
return ptr;
}
void SemAnalysis::TypeDecPart(TreeNode *t)
{
bool present = false;
SymbTable* entry = NULL;
AttrbuteIR* attribp = NULL;
TreeNode* tt = t;
tt = tt->child[0];
while(tt != NULL)
{
attribp = new AttrbuteIR();
attribp->kind = typeKind;
attribp->idtype = TypeProcess(tt,tt->kind.dec);
present = STable::Enter(tt->name[0],attribp,&entry);
if(!present)
{
//err 重复声明报错
cout<<tt->name[0]<<' '<<"类型重复声明"<<endl;
SyntaxAnalysis::dispErr();
}
tt = tt->sibling;
}
}
void SemAnalysis::VarDecPart(TreeNode* t,int paramsize)//要考虑到函数参数的偏移
{
TreeNode* tt = t;
AttrbuteIR* ptr = NULL;
SymbTable* entry = NULL;
int offset = paramsize;
//int ooffset = 0;
while(tt != NULL)
{
int num = 0;
while(num < tt->idnum)
{
ptr = new AttrbuteIR();
ptr->kind = varKind;
ptr->idtype = TypeProcess(tt,tt->kind.dec);
if(STable::Scope[STable::Level] == NULL)
{
if(tt->attr.procAttr.paramt == VarParamType)
{
ptr->more.VarAttr.level = STable::Level;
ptr->more.VarAttr.access = indir;
ptr->more.VarAttr.off = offset + 1;//间接变量认为是1
offset = ptr->more.VarAttr.off;
}
else
{
ptr->more.VarAttr.level = STable::Level;
ptr->more.VarAttr.access = dir;
ptr->more.VarAttr.off = offset + ptr->idtype->size;
offset = ptr->more.VarAttr.off;
}
}
else//此处多余了...paramsize
{
if(tt->attr.procAttr.paramt == VarParamType)
{
ptr->more.VarAttr.level = STable::Level;
ptr->more.VarAttr.access = indir;
ptr->more.VarAttr.off = offset + 1;//间接变量认为是1
offset = ptr->more.VarAttr.off;
}
else
{
ptr->more.VarAttr.level = STable::Level;
ptr->more.VarAttr.access = dir;
ptr->more.VarAttr.off = offset + ptr->idtype->size;
offset = ptr->more.VarAttr.off;
}
}
bool present = STable::Enter(tt->name[num],ptr,&entry);
if(!present)
{
//err 重复定义
cout<<tt->name[num]<<' '<<"变量重复定义"<<endl;
SyntaxAnalysis::dispErr();
}
num++;
}
tt = tt->sibling;
}
}
void SemAnalysis::ProcDecPart(TreeNode* t)
{
SymbTable* entry = NULL;
TreeNode* tt = t->child[0];
TreeNode* t2 = NULL;
while(tt != NULL)
{
entry = HeadProcess(tt);//
//处理有参数的情况
ParamTable* entry1 = entry->attrIR.more.ProcAttr.param;
int paramsize = 0;
if(entry1 != NULL)
{
while(entry1->next != NULL)
entry1 = entry1->next;
//if(entry1->entry->attrIR.more.VarAttr.access ==dir)
paramsize = entry1->entry->attrIR.more.VarAttr.off;
//else
//paramsize = entry1->entry->attrIR.more.VarAttr.off + 1;
}
t2 = tt->child[1];
if(t2 != NULL)
{
//处理声明部分
TreeNode* p = t2;
TreeNode* q = NULL;
if(p != NULL)
{
if(p->nodekind == TypeK)
{
TypeDecPart(p);
if(p->sibling != NULL)
p = p->sibling;
}
if(p->nodekind == VarK)
{
q = p->child[0];
VarDecPart(q,paramsize);
if(p->sibling != NULL)
p = p->sibling;
}
if(p->nodekind == ProcK)
{
ProcDecPart(p);
//p = p->sibling;
}
}
}
t2 = tt->child[2];
if(t2 != NULL)
Body(t2);
STable::DestroyTable();//退到上一层
tt = tt->sibling;
}
}
SymbTable* SemAnalysis::HeadProcess(TreeNode* t)
{
SymbTable* entry = NULL;
AttrbuteIR* attrbi = new AttrbuteIR();
attrbi->kind = procKind;
attrbi->idtype = NULL;
attrbi->more.ProcAttr.level = STable::Level;
attrbi->more.ProcAttr.code = -1;//默认
attrbi->more.ProcAttr.size = -1;//默认
STable::Enter(t->name[0],attrbi,&entry);
//attrbi->more.ProcAttr.param = ParaDecList(t);
entry->attrIR.more.ProcAttr.param = ParaDecList(t);
return entry;
}
ParamTable* SemAnalysis::ParaDecList(TreeNode* t)
{
STable::CreatTable();//没有参数也要建新局部化区
if(t->child[0] == NULL)
return NULL;
ParamTable* head = NULL;
TreeNode* p = t->child[0];
//STable::CreatTable();
VarDecPart(p,0);
SymbTable* pp = NULL;
pp = STable::Scope[STable::Level];
head = new ParamTable();
head->entry = pp;
pp = pp->next;
ParamTable* ptr = head;
ParamTable* ptr2 = NULL;
//head->next = pp;
while(pp != NULL)
{
ptr2 = new ParamTable();
ptr2->entry = pp;
ptr2->next = NULL;
ptr->next = ptr2;
ptr = ptr2;
pp = pp->next;
}
return head;
}
void SemAnalysis::Body(TreeNode* t)
{
if(t->child[0] == NULL)
return;//err 无语句,是错误吗?
//循环处理语句序列
TreeNode* p = t->child[0];
while(p != NULL)
{
statement(p);
p = p->sibling;
}
}
//void SemAnalysis::statement(TreeNode* t)
//{
//
//}
//一直不明白书上写的参数AccessKind* Ekind的作用和必要性,选择去除
TypeIR* SemAnalysis::Expr(TreeNode* t/*,AccessKind* Ekind*/)
{
TypeIR* Eptr = NULL;
switch(t->kind.exp)
{
case ConstK:
{
Eptr = intPtr;
//(*Ekind) = dir;
break;
}
case IdK:
{
if(t->child[0] == NULL)
{
SymbTable* entry = NULL;
if(STable::FindEntry(t->name[0],false,&entry))
{
if(entry->attrIR.kind == varKind)
{
Eptr = entry->attrIR.idtype;
//(*Ekind) = indir;
}
else
{
//err
cout<<"line:"<<t->lineno<<' ';
cout<<t->name[0]<<' ';
SyntaxAnalysis::dispErr("非变量标识符");
}
}
else
{
//err 未声明
//string s = "line:" + (t->lineno) + ' ' + "未声明" + (t->name[0]);
cout<<"line:"<<t->lineno<<' ';
cout<<t->name[0]<<' ';
SyntaxAnalysis::dispErr("未声明标识符");
}
}
else
{
if(t->attr.ExpAttr.varkind == ArrayMembV)
{
Eptr = arrayVar(t);
}
else
{
Eptr = recordVar(t);
}
}
break;
}
case OpK:
{
TypeIR* eptr1 = Expr(t->child[0]);
TypeIR* eptr2 = Expr(t->child[1]);
//if(eptr1 != intPtr || eptr2 != intPtr)
if(eptr1 != eptr2)
{
//err 分量类型错
cout<<"line:"<<t->lineno<<' ';
SyntaxAnalysis::dispErr("运算分量不相容");
}
if(t->attr.ExpAttr.op == EQ || t->attr.ExpAttr.op == LT)
Eptr = boolPtr;//对于逻辑表达式,赋予bool类型
else
Eptr = intPtr;
break;
}
}
return Eptr;
}
TypeIR* SemAnalysis::arrayVar(TreeNode* t)
{
SymbTable* entry = NULL;
bool present = STable::FindEntry(t->name[0],false,&entry);
if(!present)
{
//err 无声明
cout<<"line:"<<t->lineno<<' ';
cout<<t->name[0]<<' ';
SyntaxAnalysis::dispErr("未声明");
return NULL;
}
if(entry->attrIR.kind != varKind)
{
//err
cout<<"line:"<<t->lineno<<' ';
cout<<t->name[0]<<' ';
SyntaxAnalysis::dispErr("非变量");
return NULL;
}
if(entry->attrIR.idtype->kind != arrayTy)
{
//err
cout<<"line:"<<t->lineno<<' ';
cout<<t->name[0]<<' ';
SyntaxAnalysis::dispErr("不是数组类型变量");
return NULL;
}
//if(t->child[0]->kind.dec != entry->attrIR.idtype->more.arrayAttr.indexTy->)
if(Expr(t->child[0]/*,entry->attrIR.more.VarAttr.access*/) != intPtr)
{
//err 类型不符
cout<<"line:"<<t->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("不合法下标类型");
return NULL;
}
return entry->attrIR.idtype->more.arrayAttr.elemTy;
}
TypeIR* SemAnalysis::recordVar(TreeNode* t)
{
SymbTable* entry = NULL;
FieldChain* entry2 = NULL;
bool present = STable::FindEntry(t->name[0],false,&entry);
if(!present)
{
//err
return NULL;
}
if(entry->attrIR.kind != varKind)
{
//err
return NULL;
}
if(entry->attrIR.idtype->kind != recordTy)
{
//err
return NULL;
}
bool present2 = STable::FindField(t->child[0]->name[0],entry->attrIR.idtype->more.body,&entry2);
if(!present2)
{
//err
cout<<"line:"<<t->child[0]->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("未声明的域变量");
return NULL;
}
return entry2->unitType;
}
void SemAnalysis::assignstatement(TreeNode* t)
{
TreeNode* Child1 = t->child[0];
TreeNode* Child2 = t->child[1];
TypeIR* Eptr = NULL;
SymbTable* entry = NULL;
if(Child1->child[0] != NULL)
{
if(Child1->attr.ExpAttr.varkind != ArrayMembV)
{
Eptr = recordVar(Child1);
}
else
Eptr = arrayVar(Child1);
}
else
{
bool present = STable::FindEntry(Child1->name[0],false,&entry);
if(!present)
{
//err
cout<<"line:"<<Child1->lineno<<' ';
cout<<Child1->name[0]<<' ';
SyntaxAnalysis::dispErr("未声明");
}
else if(entry->attrIR.kind != varKind)
{
//err
cout<<"line:"<<Child1->lineno<<' ';
cout<<Child1->name[0]<<' ';
SyntaxAnalysis::dispErr("非变量");
}
else
Eptr = entry->attrIR.idtype;
}
if(Eptr != Expr(Child2/*,*/))
{
//err 两边不等价
cout<<"line:"<<Child1->lineno<<' ';
SyntaxAnalysis::dispErr("类型不相容");
}
}
void SemAnalysis::callstatement(TreeNode* t)
{
SymbTable* entry = NULL;
//TreeNode* p
bool present = STable::FindEntry(t->child[0]->name[0],false,&entry);
if(!present)
{
//err
cout<<"line:"<<t->child[0]->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("未声明");
//cout<<"callstatement"<<endl;
return;
}
if(entry->attrIR.kind != procKind)
{
//err
cout<<"line:"<<t->child[0]->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("非过程标识符");
//cout<<"callstatement"<<endl;
return;
}
//处理形参实参结合
ParamTable* Param = entry->attrIR.more.ProcAttr.param;
TreeNode* p = t->child[1];
while(Param != NULL && p != NULL)
{
if(Param->entry->attrIR.idtype != Expr(p/*,*/))
{
//err 形参实参不匹配
cout<<"line:"<<p->lineno<<' ';
cout<<p->name[0]<<' ';
SyntaxAnalysis::dispErr("形参实参不匹配");
//cout<<"callstatement"<<endl;
return;
}
Param = Param->next;
p = p->sibling;
}
if(p == NULL && Param != NULL)
{
//err 实参个数不够
cout<<"line:"<<t->child[0]->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("实参个数不够");
//cout<<"callstatement"<<endl;
return;
}
if(p != NULL && Param == NULL)
{
//err 实参个数太多
cout<<"line:"<<t->child[0]->lineno<<' ';
cout<<t->child[0]->name[0]<<' ';
SyntaxAnalysis::dispErr("实参个数太多");
//cout<<"callstatement"<<endl;
return;
}
}
void SemAnalysis::readstatement(TreeNode* t)
{
SymbTable* entry = NULL;
bool present = STable::FindEntry(t->name[0],false,&entry);
if(!present)
{
//err 未声明
cout<<"readstatement"<<endl;
return;
}
if(entry->attrIR.kind != varKind)
{
//err 不一致
cout<<"readstatement"<<endl;
return;
}
}
void SemAnalysis::writestatement(TreeNode* t)
{
TypeIR* type = Expr(t->child[0]/*,*/);
if(t->kind.exp == OpK)
{
if(t->attr.ExpAttr.op == LT || t->attr.ExpAttr.op == EQ)
{
if(type->kind != boolTy)
{
//err
cout<<"writestatement"<<endl;
return;
}
}
}
}
void SemAnalysis::whilestatement(TreeNode* t)
{
TreeNode* p = t->child[0];
TypeIR* type = Expr(p/*,*/);
if(type->kind != boolTy)
{
//err
SyntaxAnalysis::dispErr("while条件部分非bool类型");
//cout<<"whilestatement"<<endl;
return;
}
/*
if(p->kind.exp == OpK)
{
if(p->attr.ExpAttr.op == LT || p->attr.ExpAttr.op == EQ)
{
if(type->kind != boolTy)
{
//err
SyntaxAnalysis::dispErr("while条件部分非bool类型");
//cout<<"whilestatement"<<endl;
return;
}
}
}*/
p = t->child[1];
while(p != NULL)
{
statement(p);
p = p->sibling;
}
}
void SemAnalysis::ifstatement(TreeNode* t)
{
TreeNode* p = t->child[0];
TypeIR* type = Expr(p/*,*/);
if(type->kind != boolTy)
{
//err
SyntaxAnalysis::dispErr("if条件部分非bool类型");
//cout<<"ifstatement"<<endl;
return;
}
/*
if(p->kind.exp == OpK)
{
if(p->attr.ExpAttr.op == LT || p->attr.ExpAttr.op == EQ)
{
if(type->kind != boolTy)
{
//err
SyntaxAnalysis::dispErr("if条件部分非bool类型");
//cout<<"ifstatement"<<endl;
return;
}
}
}*/
p = t->child[1];
while(p != NULL)
{
statement(p);
p = p->sibling;
}
p = t->child[2];
while(p != NULL)
{
statement(p);
p = p->sibling;
}
}
//主体
void SemAnalysis::Analyze(TreeNode* t)
{
initialize();
STable::CreatTable();
//处理声明部分
TreeNode* p = t->child[1];
TreeNode* q = NULL;
if(p != NULL)
{
if(p->nodekind == TypeK)
{
TypeDecPart(p);
if(p->sibling != NULL)
p = p->sibling;
}
if(p->nodekind == VarK)
{
q = p->child[0];
VarDecPart(q,0);
if(p->sibling != NULL)
p = p->sibling;
}
if(p->nodekind == ProcK)
{
ProcDecPart(p);
//p = p->sibling;
}
}
//处理主程序体
p = t->child[2];
if(p != NULL)
{
q = p->child[0];
while(q != NULL)
{
statement(q);
q = q->sibling;
}
}
else
SyntaxAnalysis::dispErr("err 无语句");
//撤销符号表
//...
}
void SemAnalysis::returnstatement(TreeNode* t)
{
return;
}
////////////////////////////////////////////////////////////
int main(void)
{
TreeNode* root = NULL;
root = SyntaxAnalysis::parse();
if(root == NULL)
cout<<"error"<<endl;
SemAnalysis::Analyze(root);
STable::PrindSymbTable();
result = fopen("result.txt","w");
STable::PrintSymbTable();
fclose(result);
system("pause");
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化