This commit is contained in:
Saturneric 2021-05-13 00:27:10 +08:00
parent 9bbc406364
commit 79d9c3976a
No known key found for this signature in database
GPG Key ID: 90536ECE5E7D918A
2 changed files with 30 additions and 28 deletions

View File

@ -21,7 +21,10 @@ int main(int argc, const char* argv[]) {
try { 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) { if (argc < 2) {
printf("Usage: <Input Path>\n"); printf("Usage: <Input Path>\n");

View File

@ -8,24 +8,24 @@ void SyntaxParser::parse() {
status_stack.push(0); status_stack.push(0);
now_line = 1; now_line = 1;
size_t _line_index = 0, max_line_index = lines_index[now_line-1]; size_t _line_index = 0, max_line_index = lines_index[now_line - 1];
while(!tokens_queue.empty()) { while (!tokens_queue.empty()) {
auto *p_step = atg->findActionStep(status_stack.top(), tokens_queue.front()); auto *p_step = atg->findActionStep(status_stack.top(), tokens_queue.front());
if(p_step == nullptr) { if (p_step == nullptr) {
printError(output); printError();
return; return;
} }
if(p_step->action == MOVE) { if (p_step->action == MOVE) {
output << "MOVE IN" << "(AUTOMATA STATUS " << status_stack.top() <<"): "; output << "MOVE IN" << "(AUTOMATA STATUS " << status_stack.top() << "): ";
printSymbol(tokens_queue.front()); printSymbol(tokens_queue.front());
auto *node = new TreeNode(tokens_queue.front()); auto *node = new TreeNode(tokens_queue.front());
auto *p_symbol = pool->getSymbol(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(L"terminator");
} }
node->addInfo(p_symbol->name); node->addInfo(p_symbol->name);
@ -34,7 +34,7 @@ void SyntaxParser::parse() {
analyse_stack.push(tokens_queue.front()); analyse_stack.push(tokens_queue.front());
tree_stack.push(node); tree_stack.push(node);
if(_line_index > max_line_index) { if (_line_index > max_line_index) {
string_buffer.str(L""); string_buffer.str(L"");
string_buffer.clear(); string_buffer.clear();
max_line_index = lines_index[now_line++]; max_line_index = lines_index[now_line++];
@ -42,18 +42,17 @@ void SyntaxParser::parse() {
string_buffer << p_symbol->name << " "; string_buffer << p_symbol->name << " ";
tokens_queue.pop(); tokens_queue.pop();
_line_index++; _line_index++;
} } else if (p_step->action == REDUCE) {
else if(p_step->action == REDUCE) {
auto *p_pdt = p_step->target.production; 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); printProduction(p_pdt);
output << "]"; output << "]";
std::stack<TreeNode *> temp_stack; std::stack<TreeNode *> temp_stack;
for(int i : p_pdt->right) { for (int i : p_pdt->right) {
if(i == 0) if (i == 0)
continue; continue;
analyse_stack.pop(); analyse_stack.pop();
status_stack.pop(); status_stack.pop();
@ -67,13 +66,13 @@ void SyntaxParser::parse() {
// std::wcout << fatherNode->getInfoVec()[0] << std::endl; // std::wcout << fatherNode->getInfoVec()[0] << std::endl;
while(!temp_stack.empty()) { while (!temp_stack.empty()) {
// std::wcout << temp_stack.top()->getInfoVec()[0] << std::endl; // std::wcout << temp_stack.top()->getInfoVec()[0] << std::endl;
const auto &childInfo = temp_stack.top()->getInfoVec(); const auto &childInfo = temp_stack.top()->getInfoVec();
if(childInfo[0] == L"terminator") { if (childInfo[0] == L"terminator") {
for(int i = 1; i < childInfo.size() ; i++) { for (int i = 1; i < childInfo.size(); i++) {
fatherNode->addInfo(childInfo[i]); fatherNode->addInfo(childInfo[i]);
} }
delete temp_stack.top(); delete temp_stack.top();
@ -88,7 +87,7 @@ void SyntaxParser::parse() {
auto *p_goto_step = auto *p_goto_step =
atg->findGotoStep(status_stack.top(), p_pdt->left); atg->findGotoStep(status_stack.top(), p_pdt->left);
if(p_goto_step == nullptr) { if (p_goto_step == nullptr) {
printError(); printError();
return; return;
} }
@ -113,11 +112,11 @@ void SyntaxParser::parse() {
} }
void SyntaxParser::printProduction(const Production *p_pdt) { 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; 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); printSymbol(symbol_index);
@ -127,11 +126,11 @@ void SyntaxParser::printProduction(const Production *p_pdt) {
void SyntaxParser::printSymbol(int symbol_index) { void SyntaxParser::printSymbol(int symbol_index) {
auto *symbol = pool->getSymbol(symbol_index); auto *symbol = pool->getSymbol(symbol_index);
if(!symbol->index) { if (!symbol->index) {
output << L"[Epsilon]"; output << L"[Epsilon]";
return; return;
} }
if(!symbol->terminator) if (!symbol->terminator)
output << pool->getSymbol(symbol_index)->name; output << pool->getSymbol(symbol_index)->name;
else else
output << L'"' << pool->getSymbol(symbol_index)->name << L'"'; output << L'"' << pool->getSymbol(symbol_index)->name << L'"';
@ -144,11 +143,11 @@ void SyntaxParser::getToken() {
size_t _line_index = 0; size_t _line_index = 0;
while (getline(input, temp_line)) { while (getline(input, temp_line)) {
if(temp_line.size() > 2 && temp_line[0] != '#') { if (temp_line.size() > 2 && temp_line[0] != '#') {
std::vector<std::wstring> tokens = ws_split(temp_line, L" "); std::vector<std::wstring> tokens = ws_split(temp_line, L" ");
for(int i = 1; i < tokens.size(); i++) { for (int i = 1; i < tokens.size(); i++) {
if(tokens[i] == L"\r") continue;; if (tokens[i] == L"\r") continue;;
auto token_info = get_token_info(tokens[i]); auto token_info = get_token_info(tokens[i]);
tokens_queue.push(pool->getSymbolIndex(token_info.first)); tokens_queue.push(pool->getSymbolIndex(token_info.first));
_line_index++; _line_index++;
@ -164,8 +163,8 @@ void SyntaxParser::getToken() {
} }
std::vector<std::wstring> SyntaxParser::ws_split(const std::wstring &in, const std::wstring &delim) { std::vector<std::wstring> SyntaxParser::ws_split(const std::wstring &in, const std::wstring &delim) {
std::wregex re{ delim }; std::wregex re{delim};
return std::vector<std::wstring> { return std::vector<std::wstring>{
std::wsregex_token_iterator(in.begin(), in.end(), re, -1), std::wsregex_token_iterator(in.begin(), in.end(), re, -1),
std::wsregex_token_iterator() std::wsregex_token_iterator()
}; };