From 79d9c3976a9ef67911646fd96555726f603be240 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Thu, 13 May 2021 00:27:10 +0800 Subject: [PATCH] Fixed. --- Compiler.cpp | 5 ++++- src/SyntaxParser.cpp | 53 ++++++++++++++++++++++---------------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/Compiler.cpp b/Compiler.cpp index 451c341..09b387f 100644 --- a/Compiler.cpp +++ b/Compiler.cpp @@ -21,7 +21,10 @@ int main(int argc, const char* argv[]) { try { - printf("Compile Program Based on LR(1) Written By Saturneric\n"); + wcout << "Compile Program Based on LR(1) Written By Saturneric(胡宇 2018303206)" << endl; + + wcout << "老师注意:本程序区分关键字的大小写!!!" << endl + << "这样做的原因是本人在实践上还没有见过有哪门语言不区分关键字大小写的" << endl; if (argc < 2) { printf("Usage: \n"); diff --git a/src/SyntaxParser.cpp b/src/SyntaxParser.cpp index 9627dad..a3e5c49 100644 --- a/src/SyntaxParser.cpp +++ b/src/SyntaxParser.cpp @@ -8,24 +8,24 @@ void SyntaxParser::parse() { status_stack.push(0); now_line = 1; - size_t _line_index = 0, max_line_index = lines_index[now_line-1]; - while(!tokens_queue.empty()) { + size_t _line_index = 0, max_line_index = lines_index[now_line - 1]; + while (!tokens_queue.empty()) { auto *p_step = atg->findActionStep(status_stack.top(), tokens_queue.front()); - if(p_step == nullptr) { - printError(output); + if (p_step == nullptr) { + printError(); return; } - if(p_step->action == MOVE) { - output << "MOVE IN" << "(AUTOMATA STATUS " << status_stack.top() <<"): "; + if (p_step->action == MOVE) { + output << "MOVE IN" << "(AUTOMATA STATUS " << status_stack.top() << "): "; printSymbol(tokens_queue.front()); auto *node = new TreeNode(tokens_queue.front()); auto *p_symbol = pool->getSymbol(tokens_queue.front()); - if(p_symbol->terminator) { + if (p_symbol->terminator) { node->addInfo(L"terminator"); } node->addInfo(p_symbol->name); @@ -34,7 +34,7 @@ void SyntaxParser::parse() { analyse_stack.push(tokens_queue.front()); tree_stack.push(node); - if(_line_index > max_line_index) { + if (_line_index > max_line_index) { string_buffer.str(L""); string_buffer.clear(); max_line_index = lines_index[now_line++]; @@ -42,18 +42,17 @@ void SyntaxParser::parse() { string_buffer << p_symbol->name << " "; tokens_queue.pop(); _line_index++; - } - else if(p_step->action == REDUCE) { + } else if (p_step->action == REDUCE) { auto *p_pdt = p_step->target.production; - output << "REDUCE BY" << "(AUTOMATA STATUS " << status_stack.top() <<"): ["; + output << "REDUCE BY" << "(AUTOMATA STATUS " << status_stack.top() << "): ["; printProduction(p_pdt); output << "]"; std::stack temp_stack; - for(int i : p_pdt->right) { - if(i == 0) + for (int i : p_pdt->right) { + if (i == 0) continue; analyse_stack.pop(); status_stack.pop(); @@ -67,13 +66,13 @@ void SyntaxParser::parse() { // std::wcout << fatherNode->getInfoVec()[0] << std::endl; - while(!temp_stack.empty()) { + while (!temp_stack.empty()) { // std::wcout << temp_stack.top()->getInfoVec()[0] << std::endl; const auto &childInfo = temp_stack.top()->getInfoVec(); - if(childInfo[0] == L"terminator") { - for(int i = 1; i < childInfo.size() ; i++) { + if (childInfo[0] == L"terminator") { + for (int i = 1; i < childInfo.size(); i++) { fatherNode->addInfo(childInfo[i]); } delete temp_stack.top(); @@ -88,7 +87,7 @@ void SyntaxParser::parse() { auto *p_goto_step = atg->findGotoStep(status_stack.top(), p_pdt->left); - if(p_goto_step == nullptr) { + if (p_goto_step == nullptr) { printError(); return; } @@ -113,11 +112,11 @@ void SyntaxParser::parse() { } void SyntaxParser::printProduction(const Production *p_pdt) { - output << pool->getSymbol(p_pdt->left)->name << L" -> " ; + output << pool->getSymbol(p_pdt->left)->name << L" -> "; int i = 0; - for(const auto &symbol_index : p_pdt->right) { + for (const auto &symbol_index : p_pdt->right) { - if(i++ > 0) output << " "; + if (i++ > 0) output << " "; printSymbol(symbol_index); @@ -127,11 +126,11 @@ void SyntaxParser::printProduction(const Production *p_pdt) { void SyntaxParser::printSymbol(int symbol_index) { auto *symbol = pool->getSymbol(symbol_index); - if(!symbol->index) { + if (!symbol->index) { output << L"[Epsilon]"; return; } - if(!symbol->terminator) + if (!symbol->terminator) output << pool->getSymbol(symbol_index)->name; else output << L'"' << pool->getSymbol(symbol_index)->name << L'"'; @@ -144,11 +143,11 @@ void SyntaxParser::getToken() { size_t _line_index = 0; while (getline(input, temp_line)) { - if(temp_line.size() > 2 && temp_line[0] != '#') { + if (temp_line.size() > 2 && temp_line[0] != '#') { std::vector tokens = ws_split(temp_line, L" "); - for(int i = 1; i < tokens.size(); i++) { - if(tokens[i] == L"\r") continue;; + for (int i = 1; i < tokens.size(); i++) { + if (tokens[i] == L"\r") continue;; auto token_info = get_token_info(tokens[i]); tokens_queue.push(pool->getSymbolIndex(token_info.first)); _line_index++; @@ -164,8 +163,8 @@ void SyntaxParser::getToken() { } std::vector SyntaxParser::ws_split(const std::wstring &in, const std::wstring &delim) { - std::wregex re{ delim }; - return std::vector { + std::wregex re{delim}; + return std::vector{ std::wsregex_token_iterator(in.begin(), in.end(), re, -1), std::wsregex_token_iterator() };