From 62e183770450c3ba2ad98ed084bb96055433a02c Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Wed, 14 Aug 2019 02:26:42 +0800 Subject: [PATCH] some functions of PDA in parser updated. --- version0.3/ebnf.cpp | 4 +- version0.3/nasal.h | 1 + version0.3/nasal_interpreter.cpp | 25 ++++---- version0.3/nasal_lexer.h | 4 -- version0.3/nasal_parse.h | 102 ++++++++++++++++++++++--------- version0.3/nasal_print.h | 2 - 6 files changed, 89 insertions(+), 49 deletions(-) diff --git a/version0.3/ebnf.cpp b/version0.3/ebnf.cpp index a3306ec..1ca076d 100644 --- a/version0.3/ebnf.cpp +++ b/version0.3/ebnf.cpp @@ -83,6 +83,8 @@ //statement ::= - ::= ||||| <;> + ::= + ::= + ::= ||||||| <;> ::= ::= diff --git a/version0.3/nasal.h b/version0.3/nasal.h index 7191ab5..33228c7 100644 --- a/version0.3/nasal.h +++ b/version0.3/nasal.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace nasal { diff --git a/version0.3/nasal_interpreter.cpp b/version0.3/nasal_interpreter.cpp index 5598b59..72a9a66 100644 --- a/version0.3/nasal_interpreter.cpp +++ b/version0.3/nasal_interpreter.cpp @@ -1,11 +1,9 @@ -#include -#include -#include #include "nasal.h" int main() { std::string command; + std::cout<<">> nasal-- script by ValKmjolnir"<> input \"help\" to find help."<> 1. input file name to run the nasal script."<> 2. command cls to clear the screen."<> 3. command exit to shut down the program."<> 4. command lexer to see tokens in stack."<> 5. command del to delete all elements in stack."<> 6. command run to run the programme in stack."<> 7. command rs to check the source program."<> nasal-- script by ValKmjolnir"<> 1. input file name to run the lexer."<> 2. command \"cls\" to clear the screen."<> 3. command \"exit\" to shut down the program."<> 4. command \"lexer\" to see tokens in stack."<> 5. command \"parser\" to run parser."<> 6. command \"del\" to delete all elements in stack."<> 7. command \"run\" to run the programme in stack."<> 8. command \"rs\" to check the source program."< -#include -#include #include "nasal_functional.h" #define FAIL -1 //ʧ°Ü diff --git a/version0.3/nasal_parse.h b/version0.3/nasal_parse.h index a8ba9db..999e72e 100644 --- a/version0.3/nasal_parse.h +++ b/version0.3/nasal_parse.h @@ -1,55 +1,97 @@ #ifndef __NASAL_PARSE_H__ #define __NASAL_PARSE_H__ #include "nasal_lexer.h" +#include "nasal_token.h" + +enum token_type +{ + __stack_end,__identifier,__use_identifier, + __scalar,__function,__var,__equal,__semi,__definition +}; +token_type token_type_tbl; struct parse_unit { int type; - std::string content; int line; }; class parse { - private: - parse_unit *content_array; - parse_unit *statement; - int len; public: - parse(); - ~parse(); - void content_array_set_empty(); - void statement_set_empty(); + std::stack parser; + public: + void definition_reduction(); + void parse_work(token_list&); }; -parse::parse() +void parse::definition_reduction() { - len=0; - content_array=new parse_unit[4096]; - statement=new parse_unit[1024]; -} -parse::~parse() -{ - if(content_array) - delete []content_array; - if(statement) - delete []statement; -} -void parse::content_array_set_empty() -{ - for(int i=0;i<4096;++i) + int tbl[5]; + std::stack temp; + for(int i=4;i>=0;--i) { - content_array[i].line=0; - content_array[i].type=0; + if(parser.empty()) + break; + temp.push(parser.top()); + tbl[i]=temp.top().type; + parser.pop(); } + if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__scalar && tbl[4]==__semi) + { + parse_unit temp_parse_unit; + temp_parse_unit.type=__definition; + temp_parse_unit.line=temp.top().line; + parser.push(temp_parse_unit); + } + else + for(int i=0;i<5;++i) + { + if(temp.empty()) + break; + parser.push(temp.top()); + temp.pop(); + } return; } -void parse::statement_set_empty() + +void parse::parse_work(token_list& lexer) { - for(int i=0;i<1024;++i) + parse_unit temp_parse; + token_unit *temp=lexer.get_head(); + while(temp->next) { - statement[i].line=0; - statement[i].type=0; + temp=temp->next; + temp_parse.line=temp->line; + if(temp->content=="var") + { + token_type_tbl=__var; + temp_parse.type=token_type_tbl; + } + else if(temp->content=="=") + { + token_type_tbl=__equal; + temp_parse.type=token_type_tbl; + } + else if(temp->type==IDENTIFIER) + { + token_type_tbl=__identifier; + temp_parse.type=token_type_tbl; + } + else if(temp->content==";") + { + token_type_tbl=__semi; + temp_parse.type=token_type_tbl; + } + else if(temp->type==NUMBER || temp->type==STRING) + { + token_type_tbl=__scalar; + temp_parse.type=token_type_tbl; + } + parser.push(temp_parse); + definition_reduction(); + if(parser.top().type==__definition) + std::cout<<"Definition in "< -#include #include "nasal_hash.cpp" #include "nasal_list.cpp"