diff --git a/LexicalAnalyzer.cpp b/LexicalAnalyzer.cpp index 3d722e0..ddc02b1 100644 --- a/LexicalAnalyzer.cpp +++ b/LexicalAnalyzer.cpp @@ -82,7 +82,7 @@ void LexicalAnalyzer::getTokenList() //文件读入 ifstream file; string inputFile; - inputFile = "text.cpp"; //输入文件名 + inputFile = "text.txt"; //输入文件名 file.open(inputFile); if (!file) cout << "文件打开失败!"; diff --git a/Parsing_RD.cpp b/Parsing_RD.cpp index fd08e0f..f5d956f 100644 --- a/Parsing_RD.cpp +++ b/Parsing_RD.cpp @@ -24,6 +24,7 @@ int indentation; // ofstream treeFile; //ļ FILE* listing; bool Error; +bool hasTypeK; //﷨ӻʹñ int circleX, circleY, circleR; //﷨ͼλڵx, y, 뾶r @@ -63,6 +64,7 @@ void RecursiveDescentParsing::initial() procNum = 0; indentation = 0; stmlkNum = 2; //ֵΪ2Ϊ1ںмΪ1 + hasTypeK = false; } void RecursiveDescentParsing::ReadNextToken() @@ -298,7 +300,7 @@ TreeNode* RecursiveDescentParsing::DeclarePart(void) return pp; } -//< typeDec > ::= | TypeDeclaration +//?< typeDec > ::= | TypeDeclaration TreeNode* RecursiveDescentParsing::TypeDec(void) { TreeNode* t = nullptr; @@ -358,7 +360,7 @@ TreeNode* RecursiveDescentParsing::TypeDecList(void) return t; } -//< typeDecMore > ::= | TypeDecList +//?< typeDecMore > ::= | TypeDecList TreeNode* RecursiveDescentParsing::TypeDecMore(void) { 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) { if (t != nullptr) @@ -413,7 +415,7 @@ void RecursiveDescentParsing::TypeName(TreeNode* t) } } -//< baseType > ::= INTEGER | CHAR +//?< baseType > ::= INTEGER | CHAR void RecursiveDescentParsing::BaseType(TreeNode* t) { if (t != nullptr) @@ -437,7 +439,7 @@ void RecursiveDescentParsing::BaseType(TreeNode* t) } } -//< structureType > ::= arrayType | recType +//?< structureType > ::= arrayType | recType void RecursiveDescentParsing::StructureType(TreeNode* t) { if (t != nullptr) @@ -645,7 +647,7 @@ TreeNode* RecursiveDescentParsing::VarDecList(void) return t; } -//< varDecMore > ::= | varDecList +//?< varDecMore > ::= | varDecList TreeNode* RecursiveDescentParsing::VarDecMore(void) { TreeNode* t = nullptr; @@ -678,7 +680,7 @@ void RecursiveDescentParsing::VarIdList(TreeNode* t) VarIdMore(t); } -//< varIdMore > ::= | , varIdList +//?< varIdMore > ::= | , varIdList void RecursiveDescentParsing::VarIdMore(TreeNode* t) { if (token.word.Lex == SEMI){} @@ -695,7 +697,7 @@ void RecursiveDescentParsing::VarIdMore(TreeNode* t) } } -//< procDec > ::= | procDeclaration +//?< procDec > ::= | procDeclaration TreeNode* RecursiveDescentParsing::ProcDec(void) { TreeNode* t = nullptr; @@ -780,7 +782,7 @@ TreeNode* RecursiveDescentParsing::ParamDecList(void) return t; } -//< paramMore > ::= | ; paramDecList +//?< paramMore > ::= | ; paramDecList TreeNode* RecursiveDescentParsing::ParamMore(void) { TreeNode* t = nullptr; @@ -801,7 +803,7 @@ TreeNode* RecursiveDescentParsing::ParamMore(void) return t; } -//< param > ::= typeName formList | VAR typeName formList +//?< param > ::= typeName formList | VAR typeName formList TreeNode* RecursiveDescentParsing::Param(void) { TreeNode* t = new TreeNode; @@ -1009,11 +1011,11 @@ TreeNode* RecursiveDescentParsing::StmList(void) return t; } -//?< stmMore > ::= | ; stmList ???END,ENDWH,FI,ELSE +//?< stmMore > ::= | ; stmList TreeNode* RecursiveDescentParsing::StmMore(void) { 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) { match(SEMI); @@ -1059,11 +1061,11 @@ TreeNode* RecursiveDescentParsing::Stm(void) return t; } -//< assCall > ::= assignmentRest {:=,LMIDPAREN,DOT} | callStmRest {(} ???LMIDPAREN,DOT +//?< assCall > ::= assignmentRest {:=,LMIDPAREN,DOT} | callStmRest {(} TreeNode* RecursiveDescentParsing::AssCall(void) { TreeNode* t = nullptr; - if (token.word.Lex == ASSIGN) + if (token.word.Lex == ASSIGN || token.word.Lex == DOT || token.word.Lex == LMIDPAREN) t = AssignmentRest(); else if (token.word.Lex == LPAREN) t = CallStmRest(); @@ -1358,7 +1360,7 @@ TreeNode* RecursiveDescentParsing::ActParamList(void) { TreeNode* t = nullptr; 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(); if (t != nullptr) @@ -1431,7 +1433,6 @@ TreeNode* RecursiveDescentParsing::Exp(void) return t; } -//< simple_exp > ::= term | PlusOp term TreeNode* RecursiveDescentParsing::Simple_exp(void) { TreeNode* t = Term(); @@ -1593,7 +1594,7 @@ TreeNode* RecursiveDescentParsing::Variable(void) return t; } -//variMore ::= +//?variMore ::= // | [exp] // | . fieldvar void RecursiveDescentParsing::VariMore(TreeNode* t) @@ -1686,8 +1687,8 @@ void RecursiveDescentParsing::FieldvarMore(TreeNode* t) void RecursiveDescentParsing::printTree(TreeNode* tree) { //fopen_s(&listing, "treeFile.txt", "w"); - if (Error == false) - { + //if (Error == false) + //{ indentation += 4; //4 while (tree != nullptr) @@ -2054,6 +2055,7 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) line(circleX, circleY - 40, 610, 50); lastNode = tree->nodekind; lastIsDeck = false; + hasTypeK = true; } break; @@ -2067,7 +2069,10 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) circleX = 450, circleY = 150, circleR = 40; circle(circleX, circleY, circleR); // Բ 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; lastIsDeck = false; } @@ -2097,8 +2102,6 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) circleX = 250, circleY = 550, circleR = 40; circle(circleX, circleY, circleR); // Բ 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); lastX = circleX + 40; lastY = circleY; @@ -2110,8 +2113,6 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) circleX = lastX + 60, circleY = lastY, circleR = 40; circle(circleX, circleY, circleR); // Բ 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); lastX = circleX + 40; lastY = circleY; @@ -2210,7 +2211,7 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) break; case CallK: - treeFile << "Call "; break; + treeFile << "Call "; treeFile << tree->name[0] << " "; _stprintf_s(temp, _T("Call")); if (stmlkNum == 1) @@ -2327,8 +2328,8 @@ void RecursiveDescentParsing::printTree(TreeNode* tree) tree = tree->sibling; } indentation -= 4; //4 - } - else - treeFile << "﷨﷨ʧ!"; + //} + //else + // treeFile << "﷨﷨ʧ!"; //fclose(listing); } \ No newline at end of file diff --git a/SemanticAnalysis.cpp b/SemanticAnalysis.cpp index 38eb27b..eaaba3b 100644 --- a/SemanticAnalysis.cpp +++ b/SemanticAnalysis.cpp @@ -29,8 +29,8 @@ void SemanticAnalysis::initial() scope[i] = NULL; scopeLevel = -1; hasError = false; - errorFile.open("parseError.txt", ios::out | ios::app); - errorFile.open("symbTable.txt"); + errorFile.open("semanticError.txt"); + tableFile.open("symbTable.txt"); intPtr = NewTy(intTy); charPtr = NewTy(charTy); @@ -39,6 +39,7 @@ void SemanticAnalysis::initial() void SemanticAnalysis::fileClose() { + tableFile.close(); errorFile.close(); } @@ -492,7 +493,7 @@ void SemanticAnalysis::TypeDecPart(TreeNode* t) if (Enter(t->name[0], &attrI, &entry) != false) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "ظ"); + semanticError(t->lineno, temp + " ظ"); entry = NULL; } else @@ -510,6 +511,7 @@ void SemanticAnalysis::varDecList(TreeNode* t) { //KindΪ attrIr.kind = varKind; + //ѭͬһڵidʹ 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.level = scopeLevel; attrIr.More.VarAttr.off = Off; - Off = Off + attrIr.idtype->size; + + if (attrIr.idtype != NULL) + Off = Off + attrIr.idtype->size; }//ƫƼӱ͵size else @@ -539,12 +543,12 @@ void SemanticAnalysis::varDecList(TreeNode* t) if (Enter(t->name[i], &attrIr, &entry) != false) { string temp = t->name[i].c_str(); - semanticError(t->lineno, temp + "ظ"); + semanticError(t->lineno, temp + " ظ"); entry = NULL; } else - t->table[i] = entry; //¼ + t->table[i] = entry; } t = t->sibling; } @@ -555,6 +559,7 @@ void SemanticAnalysis::varDecList(TreeNode* t) void SemanticAnalysis::procDecPart(TreeNode* t) { SymbTable* entry = HeadProcess(t); + TreeNode* temp = t; t = t->child[1]; while (t != NULL) { @@ -577,6 +582,9 @@ void SemanticAnalysis::procDecPart(TreeNode* t) t = t->sibling; } + t = temp; + Body(t->child[2]); + //ǰscope DestroyTable(); } @@ -597,12 +605,13 @@ SymbTable* SemanticAnalysis::HeadProcess(TreeNode* t) t->table[0] = entry; //βδ - entry->attrIR.More.ProcAttr.param = ParaDecList(t); + if (entry != nullptr) + entry->attrIR.More.ProcAttr.param = ParaDecList(t); return entry; } -//? +//?βη ParamTable* SemanticAnalysis::ParaDecList(TreeNode* t) { TreeNode* p = NULL; @@ -617,14 +626,36 @@ ParamTable* SemanticAnalysis::ParaDecList(TreeNode* t) //úvarDecPart varDecList(p); - //Ҫѭ + //?Ҫѭ + + //βηű SymbTable* temp = scope[scopeLevel]; - ParamTable* Ptemp;//ű - Ptemp = NewParam(); - Ptemp->entry = temp; - Ptemp->next = NULL; - result = Ptemp; + ParamTable* Pcurrent = NULL;//ű + ParamTable* Plast = NULL;//ű + while (temp != NULL) + { + 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; @@ -706,14 +737,14 @@ TypeIR* SemanticAnalysis::Expr(TreeNode* t, AccessKind* Ekind) else { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); Eptr = NULL; } } else { 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 { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + ""); + semanticError(t->lineno, temp + " "); } } break; @@ -777,10 +808,10 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t) { TypeIR* Eptr = NULL; SymbTable* entry = NULL; - if (FindEntry(t->name[0], &entry)) + if (!FindEntry(t->name[0], &entry)) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "δҵ˱ʶ"); + semanticError(t->lineno, temp + " ȱʧ"); entry = NULL; } else @@ -788,14 +819,14 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t) if (entry->attrIR.kind != varKind) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); } else { - if (entry->attrIR.idtype != NULL) + if (entry->attrIR.idtype == NULL) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + ""); + semanticError(t->lineno, temp + " "); } else { @@ -812,7 +843,7 @@ TypeIR* SemanticAnalysis::arrayVar(TreeNode* t) else { 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) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "δҵ˱ʶ"); + semanticError(t->lineno, temp + " ȱʧ"); } else { if (entry->attrIR.kind != varKind) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); } else { if (entry->attrIR.idtype->kind != recordTy) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); } //Ϸ,idǷ else @@ -862,7 +893,7 @@ TypeIR* SemanticAnalysis::recordVar(TreeNode* t) if (tempF == NULL) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "ǺϷ"); + semanticError(t->lineno, temp + " ǺϷ"); } else { @@ -886,10 +917,10 @@ void SemanticAnalysis::assignstatement(TreeNode* t) 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(); - semanticError(t->lineno, temp + "δҵ˱ʶ"); + string temp = t->child[0]->name[0].c_str(); + semanticError(t->lineno, temp + " ȱʧ"); entry = NULL; } else @@ -897,7 +928,7 @@ void SemanticAnalysis::assignstatement(TreeNode* t) if (entry->attrIR.kind != varKind) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); } else { @@ -931,24 +962,28 @@ void SemanticAnalysis::callstatement(TreeNode* t) if (temp == false) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "δҵ˺"); + semanticError(t->lineno, temp + " δ"); } else { + ParamTable* tempTable = entry->attrIR.More.ProcAttr.param; if (entry->attrIR.kind != procKind) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "Ǻ"); + semanticError(t->lineno, temp + " Ǻ"); } + //жβʵζӦ + //ָ else { //ָ - ParamTable* tempTable = entry->attrIR.More.ProcAttr.param; + SymbTable* paraEntry = tempTable->entry; + //ʵָ TreeNode* pNode = t->child[1]; - while (tempTable != NULL && pNode != NULL) + while ((paraEntry != NULL && paraEntry->attrIR.More.VarAttr.isParam != false) && pNode != NULL) { AccessKind tempA; TypeIR* tempT = Expr(pNode, &tempA); @@ -960,11 +995,14 @@ void SemanticAnalysis::callstatement(TreeNode* t) semanticError(t->lineno, "Ͳƥ"); 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, "ƥ"); } @@ -1026,13 +1064,13 @@ void SemanticAnalysis::readstatement(TreeNode* t) if (FindEntry(t->name[0], &entry) == false) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "δҵ˱ʶ"); + semanticError(t->lineno, temp + " ȱʧ"); } else if (entry->attrIR.kind != varKind) { string temp = t->name[0].c_str(); - semanticError(t->lineno, temp + "DZ"); + semanticError(t->lineno, temp + " DZ"); } } @@ -1041,8 +1079,9 @@ void SemanticAnalysis::writestatement(TreeNode* t) { TypeIR* Etp = Expr(t->child[0], NULL); - if (Etp->kind == boolTy) - semanticError(t->lineno, "ʽϷ"); + if (Etp != NULL) + if (Etp->kind == boolTy) + semanticError(t->lineno, "ʽϷ"); } void SemanticAnalysis::returnstatement(TreeNode* t) diff --git a/SyntaxTree.txt b/SyntaxTree.txt index de8ade6..b9f3bd5 100644 --- a/SyntaxTree.txt +++ b/SyntaxTree.txt @@ -1,27 +1,111 @@ ProK - PheadK p - TypeK -line: 2 Deck Integer t -line: 3 Deck Chark c + PheadK bubble VarK -line: 4 Deck Integer v7 v8 -line: 5 Deck Chark v9 -line: 6 Deck Integer v4 +line: 2 Deck Integer i j num +line: 3 Deck ArrayK 20 1 Integer a +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 -line: 8 Stmtk Assign -line: 8 ExpK Vari Id v1 -line: 8 ExpK Op + -line: 8 ExpK Vari Id v2 -line: 8 ExpK Const Id 10 -line: 9 Stmtk Assign -line: 9 ExpK Vari Id v3 -line: 9 ExpK Op + -line: 9 ExpK Vari Id v4 -line: 9 ExpK Vari Id v5 -line: 10 Stmtk Assign -line: 10 ExpK Vari Id v6 -line: 10 ExpK Op + -line: 10 ExpK Const Id 10 -line: 10 ExpK Const Id 10 -line: 11 Stmtk Write -line: 11 ExpK Vari Id v1 +line: 29 Stmtk Read num +line: 30 Stmtk Assign +line: 30 ExpK Vari Id i +line: 30 ExpK Const Id 1 +line: 31 Stmtk While +line: 31 ExpK Op < +line: 31 ExpK Vari Id i +line: 31 ExpK Op + +line: 31 ExpK Vari Id num +line: 31 ExpK Const Id 1 +line: 32 Stmtk Read j +line: 33 Stmtk Assign +line: 33 ExpK Vari ArrayMember a +line: 33 ExpK Vari Id i +line: 33 ExpK Vari Id j +line: 34 Stmtk Assign +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 diff --git a/semanticError.txt b/semanticError.txt new file mode 100644 index 0000000..e69de29 diff --git a/symbTable.txt b/symbTable.txt new file mode 100644 index 0000000..e32cff5 --- /dev/null +++ b/symbTable.txt @@ -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 diff --git a/text.cpp b/text.cpp deleted file mode 100644 index 04d6d2a..0000000 --- a/text.cpp +++ /dev/null @@ -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. diff --git a/text.txt b/text.txt new file mode 100644 index 0000000..f2f743d --- /dev/null +++ b/text.txt @@ -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 #include"LexicalAnalyzer.h" #include"Parsing_RD.h" +#include"SemanticAnalysis.h" using namespace std; int main() { + /*======ʷ=====*/ LexicalAnalyzer lexicalanalyzer; lexicalanalyzer.getTokenList(); int count = lexicalanalyzer.TokenList.size(); @@ -25,6 +27,7 @@ int main() // << ">" << endl; //} + /*=====﷨=====*/ ofstream file; file.open("tokenList.txt"); for (int i = 0; i < count; i++) @@ -51,8 +54,15 @@ int main() rd.printTree(root); saveimage(_T("treeFile.bmp")); } - closegraph(); rd.fileClose(); + + /*==========*/ + SemanticAnalysis sa; + sa.initial(); + sa.analyze(root); + sa.PrintSymbTable(); + sa.fileClose(); + cout << "гɹ" << endl; } \ No newline at end of file diff --git a/tokenList.txt b/tokenList.txt index 1fe5cb8..8d192e1 100644 --- a/tokenList.txt +++ b/tokenList.txt @@ -1,49 +1,198 @@ 1 2 program -1 23 p -2 4 type -2 23 t -2 27 = +1 23 bubble +2 5 var 2 21 integer +2 23 i +2 38 , +2 23 j +2 38 , +2 23 num 2 37 ; -3 23 c -3 27 = -3 22 char +3 17 array +3 39 [ +3 24 1 +3 41 .. +3 24 20 +3 40 ] +3 18 of +3 21 integer +3 23 a 3 37 ; -4 5 var -4 21 integer -4 23 v7 -4 38 , -4 23 v8 -4 37 ; -5 22 char -5 23 v9 +5 3 procedure +5 23 q +5 33 ( +5 21 integer +5 23 num +5 34 ) 5 37 ; -6 21 integer -6 23 v4 -6 37 ; -7 13 begin -8 23 v1 -8 26 := -8 23 v2 -8 29 + -8 24 10 +7 5 var +7 21 integer +7 23 i +7 38 , +7 23 j +7 38 , +7 23 k +7 37 ; +8 21 integer +8 23 t 8 37 ; -9 23 v3 -9 26 := -9 23 v4 -9 29 + -9 23 v5 -9 37 ; -10 23 v6 +9 13 begin +10 23 i 10 26 := -10 24 10 -10 29 + -10 24 10 +10 24 1 10 37 ; -11 16 write -11 33 ( -11 23 v1 -11 34 ) -12 14 end -12 35 . -13 0 +11 10 while +11 23 i +11 28 < +11 23 num +11 11 do +12 23 j +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 diff --git a/treeFile.bmp b/treeFile.bmp index 990bb4a..9db153b 100644 Binary files a/treeFile.bmp and b/treeFile.bmp differ