diff --git a/version0.11/main.cpp b/version0.11/main.cpp index 77cd364..a6888b3 100644 --- a/version0.11/main.cpp +++ b/version0.11/main.cpp @@ -10,24 +10,22 @@ int main() nasal_parser pas; std::string command; std::cout<<">> Nasal interpreter by ValKmjolnir"<> input \"help\" to find help."<> Be careful: this interpreter uses strict methods to check if there's an error."<> input [help] to find help."<> "; std::getline(std::cin,command); if(command=="help") { - std::cout<<">> nasal script interpreter by ValKmjolnir"<> 1. |input file name to load the file."<> 2. \"cls\" |clear the screen."<> 3. \"exit\" |shut down the interpreter."<> 4. \"lexer\" |run and show the lexer. (-lexer)"<> 5. \"parser\"|run parser. (-parser)"<> 6. \"del\" |delete all elements in stack."<> 7. \"run\" |run the programme in stack. (-lexer -parser)"<> 8. \"rs\" |check the source program."<> Nasal interpreter by ValKmjolnir"<> 1. [ ] |input file name to load the file."<> 2. [cls ] |clear the screen."<> 3. [exit ] |shut down the interpreter."<> 4. [lexer ] |run and show the lexer. (-lexer)"<> 5. [parser] |run parser. (-parser)"<> 6. [del ] |delete all elements in stack."<> 7. [run ] |run the programme in stack. (-lexer -parser)"<> 8. [rs ] |check the source program."< main_stack; - std::stack comp_stack; + std::stack main_stack; + std::stack comp_stack; public: void set_stack_empty() { @@ -576,30 +580,39 @@ class PDA main_stack.pop(); while(!comp_stack.empty()) comp_stack.pop(); - main_stack.push(__stack_end); - main_stack.push(__var); - main_stack.push(__id); - main_stack.push(__semi); - comp_stack.push(__stack_end); - comp_stack.push(__program); + parse_unit temp; + temp.line=0; + temp.type=__stack_end; + main_stack.push(temp); + temp.type=__var; + main_stack.push(temp); + temp.type=__id; + main_stack.push(temp); + temp.type=__semi; + main_stack.push(temp); + + temp.type=__stack_end; + comp_stack.push(temp); + temp.type=__program; + comp_stack.push(temp); } void stack_input(std::stack& temp) { set_stack_empty(); while(!temp.empty()) { - main_stack.push(temp.top().type); + main_stack.push(temp.top()); temp.pop(); } return; } - void print_stack(std::stack& temp) + void print_stack(std::stack& temp) { - std::stack t; + std::stack t; while(!temp.empty()) { t.push(temp.top()); - print_token(t.top()); + print_token(t.top().type); std::cout<<" "; temp.pop(); } @@ -621,7 +634,7 @@ class PDA } bool reducable() { - std::stack temp; + std::stack temp; for(int i=0;i=0;--j) @@ -631,7 +644,7 @@ class PDA // if par[i].tokens[j] is 0 then continue until the tokens[j] is not 0 temp.push(comp_stack.top()); comp_stack.pop(); - if((par[i].tokens[j]!=temp.top()) || (comp_stack.empty() && par[i].tokens[j])) + if((par[i].tokens[j]!=temp.top().type) || (comp_stack.empty() && par[i].tokens[j])) { while(!temp.empty()) { @@ -640,9 +653,12 @@ class PDA } break; } - if((par[i].tokens[j]==temp.top()) && (j==0)) + if((par[i].tokens[j]==temp.top().type) && (j==0)) { - comp_stack.push(par[i].res); + parse_unit t; + t.line=temp.top().line; + t.type=par[i].res; + comp_stack.push(t); return true; } } @@ -651,15 +667,18 @@ class PDA } void print_error() { + int cnt_line=0; while(!comp_stack.empty()) { - if((comp_stack.top()!=__statement) && (comp_stack.top()!=__program) && (comp_stack.top()!=__stack_end)) + if((comp_stack.top().type!=__program) && (comp_stack.top().type!=__stack_end)) { - print_token(comp_stack.top()); + if(cnt_line!=comp_stack.top().line) + { + cnt_line=comp_stack.top().line; + std::cout<>[Parse] 0 error(s)."<>[Parse] Error:"<>[Parse] Error:"; print_error(); } } else { - std::cout<<">>[Parse] Error:"<>[Parse] Error:"; print_error(); } std::cout<<">>[Parse] Complete checking."<