1.完善了语法树可视化图

2.更正了predict集
3.更正了语义分析中形参数量不匹配的问题
4.更正了语义分析中过程分析的错误
This commit is contained in:
不点 小 2022-04-06 22:54:34 +08:00
parent ce066f4a16
commit 3481c4eb76
11 changed files with 473 additions and 148 deletions

View File

@ -82,7 +82,7 @@ void LexicalAnalyzer::getTokenList()
//文件读入 //文件读入
ifstream file; ifstream file;
string inputFile; string inputFile;
inputFile = "text.cpp"; //输入文件名 inputFile = "text.txt"; //输入文件名
file.open(inputFile); file.open(inputFile);
if (!file) if (!file)
cout << "文件打开失败!"; cout << "文件打开失败!";

View File

@ -24,6 +24,7 @@ int indentation; //
ofstream treeFile; //树形文件 ofstream treeFile; //树形文件
FILE* listing; FILE* listing;
bool Error; bool Error;
bool hasTypeK;
//语法树可视化使用变量 //语法树可视化使用变量
int circleX, circleY, circleR; //语法树图形化节点坐标x, y, 半径r int circleX, circleY, circleR; //语法树图形化节点坐标x, y, 半径r
@ -63,6 +64,7 @@ void RecursiveDescentParsing::initial()
procNum = 0; procNum = 0;
indentation = 0; indentation = 0;
stmlkNum = 2; //值为2但代码段为1。在函数中减为1 stmlkNum = 2; //值为2但代码段为1。在函数中减为1
hasTypeK = false;
} }
void RecursiveDescentParsing::ReadNextToken() void RecursiveDescentParsing::ReadNextToken()
@ -298,7 +300,7 @@ TreeNode* RecursiveDescentParsing::DeclarePart(void)
return pp; return pp;
} }
//< typeDec > ::= ε | TypeDeclaration //?< typeDec > ::= ε | TypeDeclaration
TreeNode* RecursiveDescentParsing::TypeDec(void) TreeNode* RecursiveDescentParsing::TypeDec(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
@ -358,7 +360,7 @@ TreeNode* RecursiveDescentParsing::TypeDecList(void)
return t; return t;
} }
//< typeDecMore > ::= ε | TypeDecList //?< typeDecMore > ::= ε | TypeDecList
TreeNode* RecursiveDescentParsing::TypeDecMore(void) TreeNode* RecursiveDescentParsing::TypeDecMore(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
@ -389,7 +391,7 @@ void RecursiveDescentParsing::TypeId(TreeNode* t)
} }
} }
//< typeName > ::= baseType | structureType | id //?< typeName > ::= baseType | structureType | id
void RecursiveDescentParsing::TypeName(TreeNode* t) void RecursiveDescentParsing::TypeName(TreeNode* t)
{ {
if (t != nullptr) if (t != nullptr)
@ -413,7 +415,7 @@ void RecursiveDescentParsing::TypeName(TreeNode* t)
} }
} }
//< baseType > ::= INTEGER | CHAR //?< baseType > ::= INTEGER | CHAR
void RecursiveDescentParsing::BaseType(TreeNode* t) void RecursiveDescentParsing::BaseType(TreeNode* t)
{ {
if (t != nullptr) if (t != nullptr)
@ -437,7 +439,7 @@ void RecursiveDescentParsing::BaseType(TreeNode* t)
} }
} }
//< structureType > ::= arrayType | recType //?< structureType > ::= arrayType | recType
void RecursiveDescentParsing::StructureType(TreeNode* t) void RecursiveDescentParsing::StructureType(TreeNode* t)
{ {
if (t != nullptr) if (t != nullptr)
@ -645,7 +647,7 @@ TreeNode* RecursiveDescentParsing::VarDecList(void)
return t; return t;
} }
//< varDecMore > ::= ε | varDecList //?< varDecMore > ::= ε | varDecList
TreeNode* RecursiveDescentParsing::VarDecMore(void) TreeNode* RecursiveDescentParsing::VarDecMore(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
@ -678,7 +680,7 @@ void RecursiveDescentParsing::VarIdList(TreeNode* t)
VarIdMore(t); VarIdMore(t);
} }
//< varIdMore > ::= ε | , varIdList //?< varIdMore > ::= ε | , varIdList
void RecursiveDescentParsing::VarIdMore(TreeNode* t) void RecursiveDescentParsing::VarIdMore(TreeNode* t)
{ {
if (token.word.Lex == SEMI){} if (token.word.Lex == SEMI){}
@ -695,7 +697,7 @@ void RecursiveDescentParsing::VarIdMore(TreeNode* t)
} }
} }
//< procDec > ::= ε | procDeclaration //?< procDec > ::= ε | procDeclaration
TreeNode* RecursiveDescentParsing::ProcDec(void) TreeNode* RecursiveDescentParsing::ProcDec(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
@ -780,7 +782,7 @@ TreeNode* RecursiveDescentParsing::ParamDecList(void)
return t; return t;
} }
//< paramMore > ::= ε | ; paramDecList //?< paramMore > ::= ε | ; paramDecList
TreeNode* RecursiveDescentParsing::ParamMore(void) TreeNode* RecursiveDescentParsing::ParamMore(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
@ -801,7 +803,7 @@ TreeNode* RecursiveDescentParsing::ParamMore(void)
return t; return t;
} }
//< param > ::= typeName formList | VAR typeName formList //?< param > ::= typeName formList | VAR typeName formList
TreeNode* RecursiveDescentParsing::Param(void) TreeNode* RecursiveDescentParsing::Param(void)
{ {
TreeNode* t = new TreeNode; TreeNode* t = new TreeNode;
@ -1009,11 +1011,11 @@ TreeNode* RecursiveDescentParsing::StmList(void)
return t; return t;
} }
//?< stmMore > ::= ε | ; stmList ???END,ENDWH,FI,ELSE //?< stmMore > ::= ε | ; stmList
TreeNode* RecursiveDescentParsing::StmMore(void) TreeNode* RecursiveDescentParsing::StmMore(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
if (token.word.Lex == END || token.word.Lex == ENDWH){} if (token.word.Lex == FI || token.word.Lex == ELSE || token.word.Lex == END || token.word.Lex == ENDWH){}
else if (token.word.Lex == SEMI) else if (token.word.Lex == SEMI)
{ {
match(SEMI); match(SEMI);
@ -1059,11 +1061,11 @@ TreeNode* RecursiveDescentParsing::Stm(void)
return t; return t;
} }
//< assCall > ::= assignmentRest {:=,LMIDPAREN,DOT} | callStmRest {(} ???LMIDPAREN,DOT //?< assCall > ::= assignmentRest {:=,LMIDPAREN,DOT} | callStmRest {(}
TreeNode* RecursiveDescentParsing::AssCall(void) TreeNode* RecursiveDescentParsing::AssCall(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
if (token.word.Lex == ASSIGN) if (token.word.Lex == ASSIGN || token.word.Lex == DOT || token.word.Lex == LMIDPAREN)
t = AssignmentRest(); t = AssignmentRest();
else if (token.word.Lex == LPAREN) else if (token.word.Lex == LPAREN)
t = CallStmRest(); t = CallStmRest();
@ -1358,7 +1360,7 @@ TreeNode* RecursiveDescentParsing::ActParamList(void)
{ {
TreeNode* t = nullptr; TreeNode* t = nullptr;
if (token.word.Lex == RPAREN) {} if (token.word.Lex == RPAREN) {}
else if (token.word.Lex == ID || token.word.Lex == INTC) else if (token.word.Lex == ID || token.word.Lex == INTC || token.word.Lex == LPAREN)
{ {
t = Exp(); t = Exp();
if (t != nullptr) if (t != nullptr)
@ -1431,7 +1433,6 @@ TreeNode* RecursiveDescentParsing::Exp(void)
return t; return t;
} }
//< simple_exp > ::= term | PlusOp term
TreeNode* RecursiveDescentParsing::Simple_exp(void) TreeNode* RecursiveDescentParsing::Simple_exp(void)
{ {
TreeNode* t = Term(); TreeNode* t = Term();
@ -1593,7 +1594,7 @@ TreeNode* RecursiveDescentParsing::Variable(void)
return t; return t;
} }
//variMore ::= ε //?variMore ::= ε
// | [exp] // | [exp]
// | . fieldvar // | . fieldvar
void RecursiveDescentParsing::VariMore(TreeNode* t) void RecursiveDescentParsing::VariMore(TreeNode* t)
@ -1686,8 +1687,8 @@ void RecursiveDescentParsing::FieldvarMore(TreeNode* t)
void RecursiveDescentParsing::printTree(TreeNode* tree) void RecursiveDescentParsing::printTree(TreeNode* tree)
{ {
//fopen_s(&listing, "treeFile.txt", "w"); //fopen_s(&listing, "treeFile.txt", "w");
if (Error == false) //if (Error == false)
{ //{
indentation += 4; //缩进加4 indentation += 4; //缩进加4
while (tree != nullptr) while (tree != nullptr)
@ -2054,6 +2055,7 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
line(circleX, circleY - 40, 610, 50); line(circleX, circleY - 40, 610, 50);
lastNode = tree->nodekind; lastNode = tree->nodekind;
lastIsDeck = false; lastIsDeck = false;
hasTypeK = true;
} }
break; break;
@ -2067,7 +2069,10 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
circleX = 450, circleY = 150, circleR = 40; circleX = 450, circleY = 150, circleR = 40;
circle(circleX, circleY, circleR); // 画圆 circle(circleX, circleY, circleR); // 画圆
outtextxy(circleX - 20, circleY - 10, temp);// 文字 outtextxy(circleX - 20, circleY - 10, temp);// 文字
line(circleX - 40, circleY, 290, 150); if(hasTypeK == true)
line(circleX - 40, circleY, 290, 150);
else
line(circleX, circleY - 40, 610, 50);
lastNode = tree->nodekind; lastNode = tree->nodekind;
lastIsDeck = false; lastIsDeck = false;
} }
@ -2097,8 +2102,6 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
circleX = 250, circleY = 550, circleR = 40; circleX = 250, circleY = 550, circleR = 40;
circle(circleX, circleY, circleR); // 画圆 circle(circleX, circleY, circleR); // 画圆
outtextxy(circleX - 30, circleY - 10, temp);// 文字 outtextxy(circleX - 30, circleY - 10, temp);// 文字
_stprintf_s(temp, _T("%s"), tree->name[0].c_str());
outtextxy(circleX - 10, circleY + 7, temp);
line(circleX, circleY - 40, 650, 190); line(circleX, circleY - 40, 650, 190);
lastX = circleX + 40; lastX = circleX + 40;
lastY = circleY; lastY = circleY;
@ -2110,8 +2113,6 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
circleX = lastX + 60, circleY = lastY, circleR = 40; circleX = lastX + 60, circleY = lastY, circleR = 40;
circle(circleX, circleY, circleR); // 画圆 circle(circleX, circleY, circleR); // 画圆
outtextxy(circleX - 30, circleY - 10, temp);// 文字 outtextxy(circleX - 30, circleY - 10, temp);// 文字
_stprintf_s(temp, _T("%s"), tree->name[0].c_str());
outtextxy(circleX - 10, circleY + 7, temp);
line(circleX - 40, circleY, lastX, lastY); line(circleX - 40, circleY, lastX, lastY);
lastX = circleX + 40; lastX = circleX + 40;
lastY = circleY; lastY = circleY;
@ -2210,7 +2211,7 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
break; break;
case CallK: case CallK:
treeFile << "Call "; break; treeFile << "Call ";
treeFile << tree->name[0] << " "; treeFile << tree->name[0] << " ";
_stprintf_s(temp, _T("Call")); _stprintf_s(temp, _T("Call"));
if (stmlkNum == 1) if (stmlkNum == 1)
@ -2327,8 +2328,8 @@ void RecursiveDescentParsing::printTree(TreeNode* tree)
tree = tree->sibling; tree = tree->sibling;
} }
indentation -= 4; //缩进减4 indentation -= 4; //缩进减4
} //}
else //else
treeFile << "存在语法错误,语法树生成失败!"; // treeFile << "存在语法错误,语法树生成失败!";
//fclose(listing); //fclose(listing);
} }

View File

@ -29,8 +29,8 @@ void SemanticAnalysis::initial()
scope[i] = NULL; scope[i] = NULL;
scopeLevel = -1; scopeLevel = -1;
hasError = false; hasError = false;
errorFile.open("parseError.txt", ios::out | ios::app); errorFile.open("semanticError.txt");
errorFile.open("symbTable.txt"); tableFile.open("symbTable.txt");
intPtr = NewTy(intTy); intPtr = NewTy(intTy);
charPtr = NewTy(charTy); charPtr = NewTy(charTy);
@ -39,6 +39,7 @@ void SemanticAnalysis::initial()
void SemanticAnalysis::fileClose() void SemanticAnalysis::fileClose()
{ {
tableFile.close();
errorFile.close(); errorFile.close();
} }
@ -492,7 +493,7 @@ void SemanticAnalysis::TypeDecPart(TreeNode* t)
if (Enter(t->name[0], &attrI, &entry) != false) if (Enter(t->name[0], &attrI, &entry) != false)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "重复声明报错"); semanticError(t->lineno, temp + " 重复声明报错");
entry = NULL; entry = NULL;
} }
else else
@ -510,6 +511,7 @@ void SemanticAnalysis::varDecList(TreeNode* t)
{ {
//Kind为变量类型 //Kind为变量类型
attrIr.kind = varKind; attrIr.kind = varKind;
//循环处理同一个节点的id调用类型处理函数 //循环处理同一个节点的id调用类型处理函数
for (int i = 0; i < t->idnum; i++) for (int i = 0; i < t->idnum; i++)
{ {
@ -522,7 +524,9 @@ void SemanticAnalysis::varDecList(TreeNode* t)
attrIr.More.VarAttr.access = dir; attrIr.More.VarAttr.access = dir;
attrIr.More.VarAttr.level = scopeLevel; attrIr.More.VarAttr.level = scopeLevel;
attrIr.More.VarAttr.off = Off; attrIr.More.VarAttr.off = Off;
Off = Off + attrIr.idtype->size;
if (attrIr.idtype != NULL)
Off = Off + attrIr.idtype->size;
}//偏移加变量类型的size }//偏移加变量类型的size
else else
@ -539,12 +543,12 @@ void SemanticAnalysis::varDecList(TreeNode* t)
if (Enter(t->name[i], &attrIr, &entry) != false) if (Enter(t->name[i], &attrIr, &entry) != false)
{ {
string temp = t->name[i].c_str(); string temp = t->name[i].c_str();
semanticError(t->lineno, temp + "重复声明报错"); semanticError(t->lineno, temp + " 重复声明报错");
entry = NULL; entry = NULL;
} }
else else
t->table[i] = entry;
//记录类型名 //记录类型名
t->table[i] = entry;
} }
t = t->sibling; t = t->sibling;
} }
@ -555,6 +559,7 @@ void SemanticAnalysis::varDecList(TreeNode* t)
void SemanticAnalysis::procDecPart(TreeNode* t) void SemanticAnalysis::procDecPart(TreeNode* t)
{ {
SymbTable* entry = HeadProcess(t); SymbTable* entry = HeadProcess(t);
TreeNode* temp = t;
t = t->child[1]; t = t->child[1];
while (t != NULL) while (t != NULL)
{ {
@ -577,6 +582,9 @@ void SemanticAnalysis::procDecPart(TreeNode* t)
t = t->sibling; t = t->sibling;
} }
t = temp;
Body(t->child[2]);
//结束当前scope //结束当前scope
DestroyTable(); DestroyTable();
} }
@ -597,12 +605,13 @@ SymbTable* SemanticAnalysis::HeadProcess(TreeNode* t)
t->table[0] = entry; t->table[0] = entry;
//调用形参处理函数 //调用形参处理函数
entry->attrIR.More.ProcAttr.param = ParaDecList(t); if (entry != nullptr)
entry->attrIR.More.ProcAttr.param = ParaDecList(t);
return entry; return entry;
} }
//? //?形参分析处理函数
ParamTable* SemanticAnalysis::ParaDecList(TreeNode* t) ParamTable* SemanticAnalysis::ParaDecList(TreeNode* t)
{ {
TreeNode* p = NULL; TreeNode* p = NULL;
@ -617,14 +626,36 @@ ParamTable* SemanticAnalysis::ParaDecList(TreeNode* t)
//调用函数varDecPart //调用函数varDecPart
varDecList(p); varDecList(p);
//?需要循环吗 //?需要循环吗
//构造形参符号表 //构造形参符号表
SymbTable* temp = scope[scopeLevel]; SymbTable* temp = scope[scopeLevel];
ParamTable* Ptemp;//符号表 ParamTable* Pcurrent = NULL;//符号表
Ptemp = NewParam(); ParamTable* Plast = NULL;//符号表
Ptemp->entry = temp; while (temp != NULL)
Ptemp->next = NULL; {
result = Ptemp; Pcurrent = NewParam();
//第一个形参
if (result == NULL)
{
result = Pcurrent;
Plast = Pcurrent;
}
//确认形参类型
temp->attrIR.More.VarAttr.isParam = true;
Pcurrent->entry = temp;
Pcurrent->next = NULL;
//把该符号表连接到链表上
if (Plast != Pcurrent)
{
Plast->next = Pcurrent;
Plast = Pcurrent;
}
temp = temp->next;
}
} }
return result; return result;
@ -706,14 +737,14 @@ TypeIR* SemanticAnalysis::Expr(TreeNode* t, AccessKind* Ekind)
else else
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
Eptr = NULL; Eptr = NULL;
} }
} }
else else
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "声明缺失"); semanticError(t->lineno, temp + " 声明缺失");
} }
} }
@ -729,7 +760,7 @@ TypeIR* SemanticAnalysis::Expr(TreeNode* t, AccessKind* Ekind)
else else
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是数组或域变量"); semanticError(t->lineno, temp + " 不是数组或域变量");
} }
} }
break; break;
@ -777,10 +808,10 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t)
{ {
TypeIR* Eptr = NULL; TypeIR* Eptr = NULL;
SymbTable* entry = NULL; SymbTable* entry = NULL;
if (FindEntry(t->name[0], &entry)) if (!FindEntry(t->name[0], &entry))
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "未找到此标识符"); semanticError(t->lineno, temp + " 声明缺失");
entry = NULL; entry = NULL;
} }
else else
@ -788,14 +819,14 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t)
if (entry->attrIR.kind != varKind) if (entry->attrIR.kind != varKind)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
} }
else else
{ {
if (entry->attrIR.idtype != NULL) if (entry->attrIR.idtype == NULL)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是数组变量"); semanticError(t->lineno, temp + " 不是数组变量");
} }
else else
{ {
@ -812,7 +843,7 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t)
else else
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "与下标类型不相符"); semanticError(t->lineno, temp + " 与下标类型不相符");
} }
} }
} }
@ -828,21 +859,21 @@ TypeIR* SemanticAnalysis::recordVar(TreeNode* t)
if (FindEntry(t->name[0], &entry) == false) if (FindEntry(t->name[0], &entry) == false)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "未找到此标识符"); semanticError(t->lineno, temp + " 声明缺失");
} }
else else
{ {
if (entry->attrIR.kind != varKind) if (entry->attrIR.kind != varKind)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
} }
else else
{ {
if (entry->attrIR.idtype->kind != recordTy) if (entry->attrIR.idtype->kind != recordTy)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
} }
//检查合法域名,看id名是否在域里 //检查合法域名,看id名是否在域里
else else
@ -862,7 +893,7 @@ TypeIR* SemanticAnalysis::recordVar(TreeNode* t)
if (tempF == NULL) if (tempF == NULL)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是合法域名"); semanticError(t->lineno, temp + " 不是合法域名");
} }
else else
{ {
@ -886,10 +917,10 @@ void SemanticAnalysis::assignstatement(TreeNode* t)
if (child1->child[0] == NULL) if (child1->child[0] == NULL)
{ {
if (FindEntry(t->name[0], &entry) == false) if (FindEntry(t->child[0]->name[0], &entry) == false)
{ {
string temp = t->name[0].c_str(); string temp = t->child[0]->name[0].c_str();
semanticError(t->lineno, temp + "未找到此标识符"); semanticError(t->lineno, temp + " 声明缺失");
entry = NULL; entry = NULL;
} }
else else
@ -897,7 +928,7 @@ void SemanticAnalysis::assignstatement(TreeNode* t)
if (entry->attrIR.kind != varKind) if (entry->attrIR.kind != varKind)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
} }
else else
{ {
@ -931,24 +962,28 @@ void SemanticAnalysis::callstatement(TreeNode* t)
if (temp == false) if (temp == false)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "未找到此函数"); semanticError(t->lineno, temp + " 函数未声明");
} }
else else
{ {
ParamTable* tempTable = entry->attrIR.More.ProcAttr.param;
if (entry->attrIR.kind != procKind) if (entry->attrIR.kind != procKind)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是函数名"); semanticError(t->lineno, temp + " 不是函数名");
} }
//判断形参实参对应 //判断形参实参对应
//参数表指针
else else
{ {
//参数表指针 //参数表指针
ParamTable* tempTable = entry->attrIR.More.ProcAttr.param; SymbTable* paraEntry = tempTable->entry;
//实参指针 //实参指针
TreeNode* pNode = t->child[1]; TreeNode* pNode = t->child[1];
while (tempTable != NULL && pNode != NULL) while ((paraEntry != NULL && paraEntry->attrIR.More.VarAttr.isParam != false) && pNode != NULL)
{ {
AccessKind tempA; AccessKind tempA;
TypeIR* tempT = Expr(pNode, &tempA); TypeIR* tempT = Expr(pNode, &tempA);
@ -960,11 +995,14 @@ void SemanticAnalysis::callstatement(TreeNode* t)
semanticError(t->lineno, "参数类型不匹配"); semanticError(t->lineno, "参数类型不匹配");
pNode = pNode->sibling; pNode = pNode->sibling;
tempTable = tempTable->next; paraEntry = paraEntry->next;
} }
//参数数量不匹配 //参数数量不匹配
if (tempTable != NULL || pNode != NULL) if (pNode != NULL)
semanticError(t->lineno, "参数个数不匹配");
if(paraEntry != NULL && paraEntry->attrIR.More.VarAttr.isParam != false)
semanticError(t->lineno, "参数个数不匹配"); semanticError(t->lineno, "参数个数不匹配");
} }
@ -1026,13 +1064,13 @@ void SemanticAnalysis::readstatement(TreeNode* t)
if (FindEntry(t->name[0], &entry) == false) if (FindEntry(t->name[0], &entry) == false)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "未找到此标识符"); semanticError(t->lineno, temp + " 声明缺失");
} }
else else
if (entry->attrIR.kind != varKind) if (entry->attrIR.kind != varKind)
{ {
string temp = t->name[0].c_str(); string temp = t->name[0].c_str();
semanticError(t->lineno, temp + "不是变量"); semanticError(t->lineno, temp + " 不是变量");
} }
} }
@ -1041,8 +1079,9 @@ void SemanticAnalysis::writestatement(TreeNode* t)
{ {
TypeIR* Etp = Expr(t->child[0], NULL); TypeIR* Etp = Expr(t->child[0], NULL);
if (Etp->kind == boolTy) if (Etp != NULL)
semanticError(t->lineno, "表达式不合法"); if (Etp->kind == boolTy)
semanticError(t->lineno, "表达式不合法");
} }
void SemanticAnalysis::returnstatement(TreeNode* t) void SemanticAnalysis::returnstatement(TreeNode* t)

View File

@ -1,27 +1,111 @@
ProK ProK
PheadK p PheadK bubble
TypeK
line: 2 Deck Integer t
line: 3 Deck Chark c
VarK VarK
line: 4 Deck Integer v7 v8 line: 2 Deck Integer i j num
line: 5 Deck Chark v9 line: 3 Deck ArrayK 20 1 Integer a
line: 6 Deck Integer v4 line: 5 ProcDecK q
line: 5 Deck value param: Integer num
VarK
line: 7 Deck Integer i j k
line: 8 Deck Integer t
StmLk
line: 10 Stmtk Assign
line: 10 ExpK Vari Id i
line: 10 ExpK Const Id 1
line: 11 Stmtk While
line: 11 ExpK Op <
line: 11 ExpK Vari Id i
line: 11 ExpK Vari Id num
line: 12 Stmtk Assign
line: 12 ExpK Vari Id j
line: 12 ExpK Op +
line: 12 ExpK Op -
line: 12 ExpK Vari Id num
line: 12 ExpK Vari Id i
line: 12 ExpK Const Id 1
line: 13 Stmtk Assign
line: 13 ExpK Vari Id k
line: 13 ExpK Const Id 1
line: 14 Stmtk While
line: 14 ExpK Op <
line: 14 ExpK Vari Id k
line: 14 ExpK Vari Id j
line: 15 Stmtk If
line: 15 ExpK Op <
line: 15 ExpK Vari ArrayMember a
line: 15 ExpK Op +
line: 15 ExpK Vari Id k
line: 15 ExpK Const Id 1
line: 15 ExpK Vari ArrayMember a
line: 15 ExpK Vari Id k
line: 17 Stmtk Assign
line: 17 ExpK Vari Id t
line: 17 ExpK Vari ArrayMember a
line: 17 ExpK Vari Id k
line: 18 Stmtk Assign
line: 18 ExpK Vari ArrayMember a
line: 18 ExpK Vari Id k
line: 18 ExpK Vari ArrayMember a
line: 18 ExpK Op +
line: 18 ExpK Vari Id k
line: 18 ExpK Const Id 1
line: 19 Stmtk Assign
line: 19 ExpK Vari ArrayMember a
line: 19 ExpK Op +
line: 19 ExpK Vari Id k
line: 19 ExpK Const Id 1
line: 19 ExpK Vari Id t
line: 20 Stmtk Assign
line: 20 ExpK Vari Id t
line: 20 ExpK Const Id 0
line: 22 Stmtk Assign
line: 22 ExpK Vari Id k
line: 22 ExpK Op +
line: 22 ExpK Vari Id k
line: 22 ExpK Const Id 1
line: 24 Stmtk Assign
line: 24 ExpK Vari Id i
line: 24 ExpK Op +
line: 24 ExpK Vari Id i
line: 24 ExpK Const Id 1
StmLk StmLk
line: 8 Stmtk Assign line: 29 Stmtk Read num
line: 8 ExpK Vari Id v1 line: 30 Stmtk Assign
line: 8 ExpK Op + line: 30 ExpK Vari Id i
line: 8 ExpK Vari Id v2 line: 30 ExpK Const Id 1
line: 8 ExpK Const Id 10 line: 31 Stmtk While
line: 9 Stmtk Assign line: 31 ExpK Op <
line: 9 ExpK Vari Id v3 line: 31 ExpK Vari Id i
line: 9 ExpK Op + line: 31 ExpK Op +
line: 9 ExpK Vari Id v4 line: 31 ExpK Vari Id num
line: 9 ExpK Vari Id v5 line: 31 ExpK Const Id 1
line: 10 Stmtk Assign line: 32 Stmtk Read j
line: 10 ExpK Vari Id v6 line: 33 Stmtk Assign
line: 10 ExpK Op + line: 33 ExpK Vari ArrayMember a
line: 10 ExpK Const Id 10 line: 33 ExpK Vari Id i
line: 10 ExpK Const Id 10 line: 33 ExpK Vari Id j
line: 11 Stmtk Write line: 34 Stmtk Assign
line: 11 ExpK Vari Id v1 line: 34 ExpK Vari Id i
line: 34 ExpK Op +
line: 34 ExpK Vari Id i
line: 34 ExpK Const Id 1
line: 36 Stmtk Call
line: 36 ExpK Vari Id q
line: 36 ExpK Vari Id num
line: 37 Stmtk Assign
line: 37 ExpK Vari Id i
line: 37 ExpK Const Id 1
line: 38 Stmtk While
line: 38 ExpK Op <
line: 38 ExpK Vari Id i
line: 38 ExpK Op +
line: 38 ExpK Vari Id num
line: 38 ExpK Const Id 1
line: 39 Stmtk Write
line: 39 ExpK Vari ArrayMember a
line: 39 ExpK Vari Id i
line: 40 Stmtk Assign
line: 40 ExpK Vari Id i
line: 40 ExpK Op +
line: 40 ExpK Vari Id i
line: 40 ExpK Const Id 1

0
semanticError.txt Normal file
View File

12
symbTable.txt Normal file
View File

@ -0,0 +1,12 @@
==========µÚ0²ã·ûºÅ±í==========
i: intTy varKind Level: 0 Offset: 7 indir
j: intTy varKind Level: 0 Offset: 8 indir
num: intTy varKind Level: 0 Offset: 9 indir
a: arrayTy varKind Level: 0 Offset: 10 indir
q: funcKind Level: 1 nOff: -842150656
==========µÚ1²ã·ûºÅ±í==========
num: intTy varKind Level: 1 Offset: 7 dir
i: intTy varKind Level: 1 Offset: 8 indir
j: intTy varKind Level: 1 Offset: 9 indir
k: intTy varKind Level: 1 Offset: 10 indir
t: intTy varKind Level: 1 Offset: 11 indir

View File

@ -1,12 +0,0 @@
program p
type t = integer;
c = char;
var integer v7,v8;
char v9;
integer v4;
begin
v1:= v2 + 10;
v3:= v4 + v5;
v6:= 10 + 10;
write(v1)
end.

42
text.txt Normal file
View File

@ -0,0 +1,42 @@
program bubble
var integer i,j,num;
array [1..20] of integer a;
procedure q(integer num);
var integer i,j ,k;
integer t;
begin
i:=1;
while i < num do
j := num-i+ 1;
k:=1;
while k<j do
if a[k+ 1] < a[k]
then
t:=a[k ];
a[k] :=a[k+ 1 ];
a[k+ 1 ]:=t
else t:=0
fi;
k:=k+ 1
endwh;
i:=i+1
endwh
end
begin
read(num);
i:=1;
while i <(num+ 1) do
read(j);
a[i]:=j;
i:=i+1
endwh;
q(num);
i:=1;
while i <(num+ 1) do
write(a[i]);
i:=i+1
endwh
end.

View File

@ -10,9 +10,11 @@
#include <graphics.h> #include <graphics.h>
#include"LexicalAnalyzer.h" #include"LexicalAnalyzer.h"
#include"Parsing_RD.h" #include"Parsing_RD.h"
#include"SemanticAnalysis.h"
using namespace std; using namespace std;
int main() int main()
{ {
/*======词法分析=====*/
LexicalAnalyzer lexicalanalyzer; LexicalAnalyzer lexicalanalyzer;
lexicalanalyzer.getTokenList(); lexicalanalyzer.getTokenList();
int count = lexicalanalyzer.TokenList.size(); int count = lexicalanalyzer.TokenList.size();
@ -25,6 +27,7 @@ int main()
// << ">" << endl; // << ">" << endl;
//} //}
/*=====语法分析=====*/
ofstream file; ofstream file;
file.open("tokenList.txt"); file.open("tokenList.txt");
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
@ -51,8 +54,15 @@ int main()
rd.printTree(root); rd.printTree(root);
saveimage(_T("treeFile.bmp")); saveimage(_T("treeFile.bmp"));
} }
closegraph(); closegraph();
rd.fileClose(); rd.fileClose();
/*=====语义分析=====*/
SemanticAnalysis sa;
sa.initial();
sa.analyze(root);
sa.PrintSymbTable();
sa.fileClose();
cout << "ÔËÐгɹ¦" << endl; cout << "ÔËÐгɹ¦" << endl;
} }

View File

@ -1,49 +1,198 @@
1 2 program 1 2 program
1 23 p 1 23 bubble
2 4 type 2 5 var
2 23 t
2 27 =
2 21 integer 2 21 integer
2 23 i
2 38 ,
2 23 j
2 38 ,
2 23 num
2 37 ; 2 37 ;
3 23 c 3 17 array
3 27 = 3 39 [
3 22 char 3 24 1
3 41 ..
3 24 20
3 40 ]
3 18 of
3 21 integer
3 23 a
3 37 ; 3 37 ;
4 5 var 5 3 procedure
4 21 integer 5 23 q
4 23 v7 5 33 (
4 38 , 5 21 integer
4 23 v8 5 23 num
4 37 ; 5 34 )
5 22 char
5 23 v9
5 37 ; 5 37 ;
6 21 integer 7 5 var
6 23 v4 7 21 integer
6 37 ; 7 23 i
7 13 begin 7 38 ,
8 23 v1 7 23 j
8 26 := 7 38 ,
8 23 v2 7 23 k
8 29 + 7 37 ;
8 24 10 8 21 integer
8 23 t
8 37 ; 8 37 ;
9 23 v3 9 13 begin
9 26 := 10 23 i
9 23 v4
9 29 +
9 23 v5
9 37 ;
10 23 v6
10 26 := 10 26 :=
10 24 10 10 24 1
10 29 +
10 24 10
10 37 ; 10 37 ;
11 16 write 11 10 while
11 33 ( 11 23 i
11 23 v1 11 28 <
11 34 ) 11 23 num
12 14 end 11 11 do
12 35 . 12 23 j
13 0 ÿ 12 26 :=
12 23 num
12 30 -
12 23 i
12 29 +
12 24 1
12 37 ;
13 23 k
13 26 :=
13 24 1
13 37 ;
14 10 while
14 23 k
14 28 <
14 23 j
14 11 do
15 6 if
15 23 a
15 39 [
15 23 k
15 29 +
15 24 1
15 40 ]
15 28 <
15 23 a
15 39 [
15 23 k
15 40 ]
16 7 then
17 23 t
17 26 :=
17 23 a
17 39 [
17 23 k
17 40 ]
17 37 ;
18 23 a
18 39 [
18 23 k
18 40 ]
18 26 :=
18 23 a
18 39 [
18 23 k
18 29 +
18 24 1
18 40 ]
18 37 ;
19 23 a
19 39 [
19 23 k
19 29 +
19 24 1
19 40 ]
19 26 :=
19 23 t
20 8 else
20 23 t
20 26 :=
20 24 0
21 9 fi
21 37 ;
22 23 k
22 26 :=
22 23 k
22 29 +
22 24 1
23 12 endwh
23 37 ;
24 23 i
24 26 :=
24 23 i
24 29 +
24 24 1
25 12 endwh
26 14 end
28 13 begin
29 15 read
29 33 (
29 23 num
29 34 )
29 37 ;
30 23 i
30 26 :=
30 24 1
30 37 ;
31 10 while
31 23 i
31 28 <
31 33 (
31 23 num
31 29 +
31 24 1
31 34 )
31 11 do
32 15 read
32 33 (
32 23 j
32 34 )
32 37 ;
33 23 a
33 39 [
33 23 i
33 40 ]
33 26 :=
33 23 j
33 37 ;
34 23 i
34 26 :=
34 23 i
34 29 +
34 24 1
35 12 endwh
35 37 ;
36 23 q
36 33 (
36 23 num
36 34 )
36 37 ;
37 23 i
37 26 :=
37 24 1
37 37 ;
38 10 while
38 23 i
38 28 <
38 33 (
38 23 num
38 29 +
38 24 1
38 34 )
38 11 do
39 16 write
39 33 (
39 23 a
39 39 [
39 23 i
39 40 ]
39 34 )
39 37 ;
40 23 i
40 26 :=
40 23 i
40 29 +
40 24 1
41 12 endwh
42 14 end
42 35 .
42 0 ÿ

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.2 MiB

After

Width:  |  Height:  |  Size: 3.2 MiB