Ready for the real parser

This commit is contained in:
Valk Richard Li 2019-09-18 10:39:09 -05:00 committed by GitHub
parent 34adb12ab4
commit 67205bf0a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 1340 additions and 0 deletions

View File

@ -0,0 +1,218 @@
#ifndef __ABSTRACT_SYNTAX_TREE_H__
#define __ABSTRACT_SYNTAX_TREE_H__
class ast_tree_node
{
protected:
int line;
int type;
std::list<ast_tree_node> children;// for node that can be extended
double num; // for number
std::string str; // for string
public:
ast_tree_node()
{
line=0;
type=__root;
children.clear();
num=0;
str="";
}
void set_line(int _line)
{
line=_line;
return;
}
int return_type()
{
return type;
}
int return_line()
{
return line;
}
double run()
{
if(type==__number)
return num;
else if(type==__root)
{
std::cout<<">>[Result(s)]"<<std::endl;
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
std::cout<<i->run()<<std::endl;
return 1;
}
else
{
std::list<ast_tree_node>::iterator i=children.begin();
double left_child=i->run();
++i;
double right_child=i->run();
switch(type)
{
case __add_operator:return left_child+right_child;break;
case __sub_operator:return left_child-right_child;break;
case __mul_operator:return left_child*right_child;break;
case __div_operator:return left_child/right_child;break;
}
}
return 0;
}
void add_child(ast_tree_node& new_child)
{
children.push_back(new_child);
return;
}
ast_tree_node& return_last_child()
{
std::list<ast_tree_node>::iterator i;
for(i=children.begin();i!=children.end();++i)
;
--i;
return *i;
}
void clear_tree()
{
line=0;
type=__root;
children.clear();
num=0;
str="";
return;
}
int child_num()
{
int cnt=0;
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
++cnt;
return cnt;
}
void print(int tab_num)
{
for(int i=0;i<tab_num;++i)
std::cout<<" ";
if(type==__number)
{
std::cout<<"[ number:";
std::cout<<num;
std::cout<<" ]"<<std::endl;
return;
}
else if(type==__string)
{
std::cout<<"[ string:"<<str<<" ]"<<std::endl;
return;
}
std::cout<<"{ [ node type:";
print_token(type);
std::cout<<" ]"<<std::endl;
for(std::list<ast_tree_node>::iterator i=children.begin();i!=children.end();++i)
i->print(tab_num+1);
for(int i=0;i<tab_num;++i)
std::cout<<" ";
std::cout<<"}"<<std::endl;
return;
}
};
class struct_node_expr:public ast_tree_node
{
public:
struct_node_expr():ast_tree_node()
{
type=__root;
}
void set_type(int _type)
{
type=_type;
return;
}
};
class operator_expr:public ast_tree_node
{
public:
operator_expr():ast_tree_node()
{
type=__operator;
}
void set_operator_type(int oprt_type)
{
type=oprt_type;
return;
}
};
class number_expr:public ast_tree_node
{
public:
number_expr():ast_tree_node()
{
type=__number;
}
double return_num()
{
return num;
}
void set_number(std::string& str)
{
type=__number;
double is_float=false;
int DotPlace=0;
for(int i=0;i<(int)str.length();++i)
if(str[i]=='.')
{
is_float=true;
DotPlace=i;
break;
}
if(!is_float)
{
num=0;
double acc=1;
for(int i=(int)str.length()-1;i>=0;--i)
{
num+=acc*((double)(str[i]-'0'));
acc*=10;
}
}
else
{
num=0;
double acc=1;
double aff=0.1;
for(int i=DotPlace+1;i<(int)str.length();++i)
{
num+=aff*((double)(str[i]-'0'));
aff*=0.1;
}
for(int i=DotPlace-1;i>=0;--i)
{
num+=acc*((double)(str[i]-'0'));
acc*=10;
}
}
return;
}
};
class string_expr:public ast_tree_node
{
public:
string_expr():ast_tree_node()
{
type=__string;
}
void set_str(std::string& t)
{
str=t;
return;
}
std::string& return_str()
{
return str;
}
};
#endif

84
version0.19/main.cpp Normal file
View File

@ -0,0 +1,84 @@
#include "nasal.h"
int main()
{
resource_programme_process prog;
nasal_lexer lex;
nasal_parser pas;
std::string command;
std::cout<<">> Nasal interpreter by ValKmjolnir"<<std::endl;
std::cout<<">> Input [help] to find help."<<std::endl;
while(1)
{
std::cout<<">> ";
std::getline(std::cin,command);
if(command=="help")
{
std::cout<<">> Nasal interpreter by ValKmjolnir"<<std::endl;
std::cout<<">> 1. [ ] |input file name to load the file."<<std::endl;
std::cout<<">> 2. [cls ] |clear the screen."<<std::endl;
std::cout<<">> 3. [exit ] |shut down the interpreter."<<std::endl;
std::cout<<">> 4. [lexer ] |run and show the lexer. (-lexer)"<<std::endl;
std::cout<<">> 5. [parser] |run parser. (-parser)"<<std::endl;
std::cout<<">> 6. [ast ] |print the abstract syntax tree."<<std::endl;
std::cout<<">> 7. [del ] |delete program in memory."<<std::endl;
std::cout<<">> 8. [run ] |run the programme in stack. (-lexer -parser)"<<std::endl;
std::cout<<">> 9. [rs ] |check the source program."<<std::endl;
std::cout<<">>10. [rule ] |see rules of the language."<<std::endl;
}
else if(command=="cls")
{
system("cls");
//windows system("cls");
//linux system("clear");
//macOS system("clear");
}
else if(command=="rs")
prog.print_file();
else if(command=="exit")
{
break;
}
else if(command=="lexer")
{
lex.lexer_process(prog.use_file());
lex.print_lexer();
}
else if(command=="del")
{
prog.del_file();
std::cout<<">>[Delete] Complete."<<std::endl;
}
else if(command=="parser")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.print_parser_stack();
pas.parse_main_work();
}
else if(command=="ast")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.gen_main_prog();
pas.print_ast();
}
else if(command=="run")
{
lex.lexer_process(prog.use_file());
lex.token_list_type_detail_edit();
pas.parse_process(lex.return_list());
pas.gen_main_prog();
pas.run();
}
else if(command=="rule")
{
std::cout<<">>[Rule] Token types that you can use: number + - * /"<<std::endl;
std::cout<<">>[Rule] Each binary operator should have two numbers around it like: 1 + 1"<<std::endl;
}
else
prog.input_file(command);
}
return 0;
}

17
version0.19/nasal.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef __NASAL_H__
#define __NASAL_H__
#include <iostream>
#include <cstring>
#include <fstream>
#include <thread>
#include <stack>
#include <list>
#include "nasal_token_type.h"
#include "abstract_syntax_tree.h"
#include "nasal_lexer.h"
#include "nasal_parser.h"
#endif

525
version0.19/nasal_lexer.h Normal file
View File

@ -0,0 +1,525 @@
#ifndef __NASAL_LEXER_H__
#define __NASAL_LEXER_H__
#include <iostream>
#include <fstream>
#include <list>
#include <cstring>
#define OPERATOR 1 // operator
#define IDENTIFIER 2 // id
#define NUMBER 3 // number
#define RESERVEWORD 4 // reserve word
#define STRING 5 // string
#define DYNAMIC_ID 6 // id...
#define FAIL -1 //fail
#define SCANEND -2 //complete scanning
#define ERRORFOUND -3 //error occurred
std::string reserve_word[15]=
{
"for","foreach","forindex","while",
"var","func","break","continue","return",
"if","else","elsif","nil","and","or"
};
int isReserveWord(std::string &p)
{
for(int i=0;i<15;++i)
if(reserve_word[i]==p)
return i+1;
return FAIL;
}
bool isLetter(char t)
{
return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z'));
}
bool isNumber(char t)
{
return (('0'<=t) && (t<='9'));
}
bool isHex(char t)
{
return ((('0'<=t) && (t<='9')) || (('a'<=t) && (t<='f')));
}
bool isOct(char t)
{
return (('0'<=t) && (t<='7'));
}
class resource_programme_process
{
private:
char *resource;
public:
resource_programme_process()
{
resource=NULL;
resource=new char[16777216];
}
~resource_programme_process()
{
if(resource)
delete []resource;
}
char* use_file()
{
return resource;
}
void input_file(std::string& filename)
{
std::ifstream fin(filename);
if(fin.fail())
{
std::cout<<">>[Error] Cannot load file: "<<filename<<" ."<<std::endl;
fin.close();
return;
}
memset(resource,0,sizeof(char));
int i=0;
bool findnote=false;// to find the note with # at the head of line.
while(!fin.eof())
{
resource[i]=fin.get();
if(resource[i]=='\n')
findnote=false;
//when meeting '\n' the findnote is set to false then the next statement can be executed.
if(resource[i]!='#' && !findnote)
++i;
else if(resource[i]=='#')
findnote=true;
if(fin.eof())
break;
}
resource[i]='\0';
fin.close();
return;
}
void print_file()
{
if(!resource[0])
{
std::cout<<"0 null"<<std::endl;
return;
}
int line=1;
std::cout<<line<<" ";
for(int i=0;i<16777216;++i)
{
if(!resource[i])
break;
std::cout<<resource[i];
if(resource[i]=='\n')
{
++line;
std::cout<<line<<" ";
}
}
std::cout<<std::endl;
return;
}
void del_file()
{
memset(resource,0,sizeof(char));
return;
}
};
struct token
{
int line;
int type;
std::string content;
};
class nasal_lexer
{
private:
std::list<token> lexer;
public:
void scanner(int &syn,const char* source,std::string &__token,int &ptr,int &line)
{
char temp;
temp=source[ptr];
while(temp==' ' || temp=='\n' || temp=='\t' || temp=='\r' || temp<0 || temp>127)
{
++ptr;
if(temp=='\n')
++line;
temp=source[ptr];
}
__token="";
if(isLetter(temp) || temp=='_')
{
__token+=temp;
++ptr;
temp=source[ptr];
while(isLetter(temp) || isNumber(temp) || temp=='_')
{
__token+=temp;
++ptr;
temp=source[ptr];
}
syn=isReserveWord(__token);
if(syn==FAIL)
syn=IDENTIFIER;
else
syn=RESERVEWORD;
if((syn==IDENTIFIER) && source[ptr]=='.' && source[ptr+1]=='.' && source[ptr+2]=='.')
{
__token+="...";
syn=DYNAMIC_ID;
ptr+=3;
}
}
else if(isNumber(temp))
{
if((source[ptr]=='0') && (source[ptr+1]=='x'))
{
__token+=source[ptr];
__token+=source[ptr+1];
ptr+=2;
temp=source[ptr];
while(isNumber(temp) || isHex(temp))
{
__token+=temp;
++ptr;
temp=source[ptr];
}
}
else if((source[ptr]=='0') && (source[ptr+1]=='o'))
{
__token+=source[ptr];
__token+=source[ptr+1];
ptr+=2;
temp=source[ptr];
while(isNumber(temp) || isOct(temp))
{
__token+=temp;
++ptr;
temp=source[ptr];
}
}
else
{
int PointCnt=0;
while(isNumber(temp))
{
__token+=temp;
++ptr;
temp=source[ptr];
if(temp=='.' && !PointCnt)
{
++PointCnt;
__token+=temp;
++ptr;
temp=source[ptr];
}
}
}
syn=NUMBER;
}
else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' ||
temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' ||
temp=='?' || temp=='.' || temp=='`' || temp=='&' || temp=='@' ||
temp=='%' || temp=='$' || temp=='^')
{
__token+=temp;
++ptr;
syn=OPERATOR;
}
else if(temp=='\'')
{
syn=STRING;
__token+=temp;
++ptr;
temp=source[ptr];
while(temp!='\'')
{
if(temp=='\\')
{
__token+=temp;
++ptr;
temp=source[ptr];
__token+=temp;
++ptr;
temp=source[ptr];
}
else
{
__token+=temp;
++ptr;
temp=source[ptr];
}
if(temp==0 || temp=='\n')
break;
}
//add the last char \"
if(temp=='\'')
{
__token+=temp;
++ptr;
}
else
__token+=" __missing_end_of_string";
}
else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~')
{
syn=OPERATOR;
__token+=temp;
++ptr;
temp=source[ptr];
if(temp=='=')
{
__token+=temp;
++ptr;
}
}
else if(temp=='\\')
{
syn=OPERATOR;
__token+=temp;
++ptr;
temp=source[ptr];
if(temp=='=' || temp=='n' || temp=='t' || temp=='r' || temp=='\\' || temp=='\'' || temp=='\"')
{
__token+=temp;
++ptr;
}
}
else if(temp=='\"')
{
syn=STRING;
__token+=temp;
++ptr;
temp=source[ptr];
while(temp!='\"')
{
if(temp=='\\')
{
__token+=temp;
++ptr;
temp=source[ptr];
__token+=temp;
++ptr;
temp=source[ptr];
}
else
{
__token+=temp;
++ptr;
temp=source[ptr];
}
if(temp==0 || temp=='\n')
break;
}
//add the last char \"
if(temp=='\"')
{
__token+=temp;
++ptr;
}
else
__token+=" __missing_end_of_string";
}
else if(temp==0)
{
syn=SCANEND;
return;
}
else
{
syn=FAIL;
std::cout<<">>[Error] Unexpected error occurred: "<<temp<<std::endl;
system("pause");
++ptr;
return;
}
if(__token=="")
{
syn=ERRORFOUND;
std::cout<<">>[Error] Cannot identify "<<std::endl;
}
return;
}
void lexer_process(const char *source)
{
std::cout<<">>[Lexer] max size: "<<lexer.max_size()<<" ."<<std::endl;
lexer.clear();
int syn=0;//token type
int ptr=0;//pointer to one char in ResourcePrograme
int line=1;
std::string __token;
token temp;
while(syn!=SCANEND && syn!=ERRORFOUND)
{
scanner(syn,source,__token,ptr,line);
if(syn>0)//all Syn type is larger than zero
{
temp.line=line;
temp.type=syn;
temp.content=__token;
lexer.push_back(temp);
}
}
std::cout<<">>[Lexer] Complete scanning."<<std::endl;
return;
}
void print_lexer()
{
token temp;
for(std::list<token>::iterator i=lexer.begin();i!=lexer.end();++i)
{
temp=*i;
std::cout<<"line "<<temp.line<<": ";
if(temp.type==OPERATOR)
std::cout<<"( Operator | ";
else if(temp.type==IDENTIFIER)
std::cout<<"( Identifier | ";
else if(temp.type==NUMBER)
std::cout<<"( Number | ";
else if(temp.type==RESERVEWORD)
std::cout<<"( ReserveWord | ";
else if(temp.type==STRING)
std::cout<<"( String | ";
else if(temp.type==DYNAMIC_ID)
std::cout<<"( Identifier | ";
std::cout<<temp.content<<" )"<<std::endl;
}
return;
}
void token_list_type_detail_edit()
{
for(std::list<token>::iterator i=lexer.begin();i!=lexer.end();++i)
{
if((*i).type==RESERVEWORD)
{
if((*i).content=="var")
(*i).type=__var;
else if((*i).content=="func")
(*i).type=__func;
else if((*i).content=="return")
(*i).type=__return;
else if((*i).content=="nil")
(*i).type=__number;
else if((*i).content=="continue")
(*i).type=__continue;
else if((*i).content=="break")
(*i).type=__break;
else if((*i).content=="and")
(*i).type=__and_operator;
else if((*i).content=="or")
(*i).type=__or_operator;
else if((*i).content=="for")
(*i).type=__for;
else if((*i).content=="forindex")
(*i).type=__forindex;
else if((*i).content=="foreach")
(*i).type=__foreach;
else if((*i).content=="while")
(*i).type=__while;
else if((*i).content=="if")
(*i).type=__if;
else if((*i).content=="else")
(*i).type=__else;
else if((*i).content=="elsif")
(*i).type=__elsif;
}
else if(((*i).content=="==") || ((*i).content=="!=") || ((*i).content==">") || ((*i).content==">=") || ((*i).content=="<") || ((*i).content=="<="))
{
if((*i).content=="==")
(*i).type=__cmp_equal;
else if((*i).content=="!=")
(*i).type=__cmp_not_equal;
else if((*i).content==">")
(*i).type=__cmp_more;
else if((*i).content==">=")
(*i).type=__cmp_more_or_equal;
else if((*i).content=="<")
(*i).type=__cmp_less;
else if((*i).content=="<=")
(*i).type=__cmp_less_or_equal;
}
else if(((*i).content==";") || ((*i).content==",") || ((*i).content=="=") || ((*i).content==":") || ((*i).content==".") || ((*i).content=="?") || ((*i).content=="%") || ((*i).content=="$") || ((*i).content=="`") || ((*i).content=="^") || ((*i).content=="@"))
{
char c=(*i).content[0];
switch(c)
{
case ';':(*i).type=__semi;break;
case ',':(*i).type=__comma;break;
case '=':(*i).type=__equal;break;
case ':':(*i).type=__colon;break;
case '.':(*i).type=__dot;break;
default:(*i).type=__unknown_operator;break;
}
}
else if(((*i).type==NUMBER) || ((*i).type==STRING) || ((*i).type==IDENTIFIER) || ((*i).type==DYNAMIC_ID))
{
int t=(*i).type;
switch(t)
{
case NUMBER:(*i).type=__number;break;
case STRING:(*i).type=__string;break;
case IDENTIFIER:(*i).type=__id;break;
case DYNAMIC_ID:(*i).type=__dynamic_id;break;
}
}
else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!"))
{
char c=(*i).content[0];
switch(c)
{
case '+':(*i).type=__add_operator;break;
case '-':(*i).type=__sub_operator;break;
case '*':(*i).type=__mul_operator;break;
case '/':(*i).type=__div_operator;break;
case '~':(*i).type=__link_operator;break;
case '!':(*i).type=__nor_operator;break;
}
}
else if(((*i).content=="+=") || ((*i).content=="-=") || ((*i).content=="*=") || ((*i).content=="/=") || ((*i).content=="~="))
{
char c=(*i).content[0];
switch(c)
{
case '+':(*i).type=__add_equal;break;
case '-':(*i).type=__sub_equal;break;
case '*':(*i).type=__mul_equal;break;
case '/':(*i).type=__div_equal;break;
case '~':(*i).type=__link_equal;break;
}
}
else if(((*i).content=="(") || ((*i).content==")") || ((*i).content=="[") || ((*i).content=="]") || ((*i).content=="{") || ((*i).content=="}"))
{
char c=(*i).content[0];
switch(c)
{
case '(':(*i).type=__left_curve;break;
case ')':(*i).type=__right_curve;break;
case '[':(*i).type=__left_bracket;break;
case ']':(*i).type=__right_bracket;break;
case '{':(*i).type=__left_brace;break;
case '}':(*i).type=__right_brace;break;
}
}
}
return;
}
std::list<token>& return_list()
{
return lexer;
}
};
#endif

387
version0.19/nasal_parser.h Normal file
View File

@ -0,0 +1,387 @@
#ifndef __NASAL_PARSER_H__
#define __NASAL_PARSER_H__
#include "nasal_token_type.h"
class nasal_parser
{
private:
ast_tree_node root;
std::stack<token> parse;
std::stack<ast_tree_node> node_cache;
token this_token;
bool error;
public:
nasal_parser()
{
error=false;
this_token.type=0;
}
void get_token()
{
if(parse.empty())
{
this_token.type=0;
return;
}
this_token=parse.top();
parse.pop();
return;
}
void number_expr_process()
{
get_token();
if(this_token.type==__semi)
return;
return;
}
void string_expr_process()
{
get_token();
if(this_token.type==__semi)
return;
return;
}
void identifier_call()
{
get_token();
if(this_token.type==__left_curve)
{
return;
}
else if(this_token.type==__left_brace)
{
return;
}
else if(this_token.type==__dot)
{
return;
}
else if(this_token.type==__semi)
return;
return;
}
void definition()
{
get_token();
if(this_token.type!=__id)
{
std::cout<<">>[Error] parse error in line "<<this_token.line<<": incorrect token behind the declarator."<<std::endl;
error=true;
return;
}
get_token();
if(this_token.type!=__equal && this_token.type!=__semi)
{
std::cout<<">>[Error] parse error in line "<<this_token.line<<": expect \'=\' behind the identifier."<<std::endl;
error=true;
return;
}
else if(this_token.type==__semi)
{
std::cout<<">>[Error] [-Warning] in line "<<this_token.line<<": new variable without initialization."<<std::endl;
error=true;
return;
}
/*
*/
get_token();
if(this_token.type!=__number && this_token.type!=__string)
{
std::cout<<">>[Error] parse error in line "<<this_token.line<<": missing element after \'=\'."<<std::endl;
error=true;
return;
}
get_token();
if(this_token.type!=__semi)
{
std::cout<<">>[Error] parse error in line "<<this_token.line<<": expect \';\' at the end of the statement."<<std::endl;
error=true;
return;
}
return;
}
void parse_main_work()
{
while(!parse.empty())
{
get_token();
switch(this_token.type)
{
case __var:definition();break;
case __id:identifier_call();break;
case __number:number_expr_process();break;
case __string:string_expr_process();break;
case __if:break;
case __while:break;
case __for:break;
case __foreach:break;
case __forindex:break;
case __semi:break;
default:
std::cout<<">>[Error] parse error in line "<<this_token.line<<": ";
print_token(this_token.type);
std::cout<<" in an incorrect place."<<std::endl;
error=true;
break;
}
}
return;
}
void print_parser_stack()
{
if(parse.empty())
return;
int line=0;
std::stack<token> temp;
while(!parse.empty())
{
temp.push(parse.top());
if(line!=temp.top().line)
{
if(line+1==temp.top().line)
{
line=temp.top().line;
if(line==1)
std::cout<<"line "<<line<<" ";
else
std::cout<<std::endl<<"line "<<line<<" ";
}
else
{
while(line!=temp.top().line)
{
++line;
std::cout<<std::endl<<"line "<<line<<" ";
}
}
}
std::cout<<"<";
print_token(temp.top().type);
std::cout<<"> ";
parse.pop();
}
while(!temp.empty())
{
parse.push(temp.top());
temp.pop();
}
std::cout<<std::endl;
return;
}
void parse_process(std::list<token>& lexer)
{
while(!parse.empty())
parse.pop();
std::stack<token> temp;
for(std::list<token>::iterator i=lexer.begin();i!=lexer.end();++i)
{
token temp_parse;
temp_parse=*i;
temp.push(temp_parse);//push this into stack
}
if(temp.empty())
{
std::cout<<">>[Parse] [-Warning] Empty lexer list."<<std::endl;
return;
}
while(!temp.empty())
{
parse.push(temp.top());
temp.pop();
}
return;
}
// for building abstract syntax tree
void number_gen()
{
number_expr t;
t.set_line(parse.top().line);
t.set_number(parse.top().content);
if(node_cache.empty())
node_cache.push(t);
else if((node_cache.top().return_type()==__add_operator || node_cache.top().return_type()==__sub_operator || node_cache.top().return_type()==__mul_operator || node_cache.top().return_type()==__div_operator)
&& (node_cache.top().child_num()==1))
node_cache.top().add_child(t);
else
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing \';\' at this place."<<std::endl;
error=true;
}
return;
}
void add_sub_operator_gen()
{
operator_expr opt;
opt.set_line(parse.top().line);
opt.set_operator_type(parse.top().type);
if(node_cache.empty())
{
number_expr t;
t.set_line(parse.top().line);
std::string temp_str="0";
t.set_number(temp_str);
opt.add_child(t);
node_cache.push(opt);
}
else if(node_cache.top().return_type()==__number
|| ((node_cache.top().return_type()==__add_operator
|| node_cache.top().return_type()==__sub_operator
|| node_cache.top().return_type()==__mul_operator
|| node_cache.top().return_type()==__div_operator)
&& node_cache.top().child_num()==2))
{
opt.add_child(node_cache.top());
node_cache.pop();
node_cache.push(opt);
}
else
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing number/calculation at this place."<<std::endl;
error=true;
}
return;
}
void mul_div_operator_gen()
{
operator_expr opt;
opt.set_line(parse.top().line);
opt.set_operator_type(parse.top().type);
if(node_cache.empty())
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing number at this place(you cannot use * or / as the begin of calculation)."<<std::endl;
error=true;
return;
}
if(node_cache.top().return_type()==__number)
{
// add new leaf node number to operator
opt.add_child(node_cache.top());
node_cache.pop();
node_cache.push(opt);
}
else if(node_cache.top().return_type()==__add_operator || node_cache.top().return_type()==__sub_operator)
{
if(node_cache.top().child_num()!=2)
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing number at this place."<<std::endl;
error=true;
}
else
{
// calcu * and / first so pick a number from +-'s children nodes
opt.add_child(node_cache.top().return_last_child());
parse.pop();
// add the next number to fix the operator node's children list
if(!parse.empty() && parse.top().type==__number)
{
number_expr t;
t.set_line(parse.top().line);
t.set_number(parse.top().content);
opt.add_child(t);
}
else
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing number at this place."<<std::endl;
error=true;
return;
}
node_cache.top().return_last_child()=opt;
}
}
else if(node_cache.top().return_type()==__mul_operator || node_cache.top().return_type()==__div_operator)
{
if(node_cache.top().child_num()!=2)
{
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":missing number at this place."<<std::endl;
error=true;
}
else
{
opt.add_child(node_cache.top());
node_cache.pop();
node_cache.push(opt);
}
}
return;
}
void gen_main_prog()
{
error=false;
root.clear_tree();
// initializing
while(!node_cache.empty())
node_cache.pop();
while(!parse.empty())
{
int type=parse.top().type;
bool is_correct=false;
switch(type)
{
case __number:number_gen();break;
case __add_operator:add_sub_operator_gen();break;
case __sub_operator:add_sub_operator_gen();break;
case __mul_operator:mul_div_operator_gen();break;
case __div_operator:mul_div_operator_gen();break;
case __semi:
if(!node_cache.empty())
{
root.add_child(node_cache.top());
node_cache.pop();
}
break;
default:
std::cout<<">>[Error] parse error in line "<<parse.top().line<<":";
print_token(parse.top().type);
std::cout<<" at an incorrect place."<<std::endl;
error=true;
break;
}
parse.pop();
}
if(error)
{
std::cout<<">>[Parse] Error(s) occurred."<<std::endl;
std::cout<<">>[Parse] Complete parsing."<<std::endl;
}
else
{
std::cout<<">>[Parse] 0 error."<<std::endl;
std::cout<<">>[Parse] Complete parsing."<<std::endl;
}
return;
}
void print_ast()
{
std::cout<<">>[Abstract-syntax-tree]"<<std::endl;
root.print(0);
return;
}
void run()
{
if(!error)
root.run();
else
std::cout<<">>[Parse] Error(s) occurred,stop."<<std::endl;
return;
}
};
#endif

View File

@ -0,0 +1,106 @@
#ifndef __NASAL_TOKEN_TYPE_H__
#define __NASAL_TOKEN_TYPE_H__
#include <cstring>
enum token_type
{
__stack_end=1,
__equal, // =
__cmp_equal,__cmp_not_equal, // == !=
__cmp_less,__cmp_less_or_equal, // < <=
__cmp_more,__cmp_more_or_equal, // > >=
__and_operator,__or_operator,__nor_operator, // and or !
__add_operator,__sub_operator, // + -
__mul_operator,__div_operator,__link_operator, // * / ~
__add_equal,__sub_equal, // += -=
__mul_equal,__div_equal,__link_equal, // *= /= ~=
__left_brace,__right_brace, // {}
__left_bracket,__right_bracket, // []
__left_curve,__right_curve, // ()
__semi,__comma,__colon,__dot, // ; , : .
__unknown_operator,
__var,__func,__return,
__if,__elsif,__else,
__continue,__break,
__for,__forindex,__foreach,__while,
//operators & reserve words
__number,__string,__id,__dynamic_id,
//basic elements
__root,__operator,
__definition,__assignment,
__function,__loop,__ifelse
};
void print_token(int type)
{
std::string context="";
switch(type)
{
case __stack_end: context="#"; break;
case __equal: context="="; break;
case __cmp_equal: context="==";break;
case __cmp_not_equal: context="!=";break;
case __cmp_less: context="<"; break;
case __cmp_less_or_equal: context="<=";break;
case __cmp_more: context=">"; break;
case __cmp_more_or_equal: context=">=";break;
case __and_operator: context="and";break;
case __or_operator: context="or"; break;
case __nor_operator: context="!"; break;
case __add_operator: context="+"; break;
case __sub_operator: context="-"; break;
case __mul_operator: context="*"; break;
case __div_operator: context="/"; break;
case __link_operator: context="~"; break;
case __add_equal: context="+=";break;
case __sub_equal: context="-=";break;
case __mul_equal: context="*=";break;
case __div_equal: context="/=";break;
case __link_equal: context="~=";break;
case __left_brace: context="{"; break;
case __right_brace: context="}"; break;
case __left_bracket: context="["; break;
case __right_bracket: context="]"; break;
case __left_curve: context="("; break;
case __right_curve: context=")"; break;
case __semi: context=";"; break;
case __comma: context=","; break;
case __colon: context=":"; break;
case __dot: context="."; break;
case __unknown_operator: context="unknown_operator";break;
case __var: context="var"; break;
case __func: context="func";break;
case __continue: context="ctn"; break;
case __break: context="brk"; break;
case __for: context="for"; break;
case __forindex: context="foridx";break;
case __foreach: context="foreh";break;
case __while: context="while";break;
case __if: context="if";break;
case __elsif: context="elsif";break;
case __else: context="else";break;
case __return: context="rtrn";break;
case __id: context="id";break;
case __dynamic_id: context="id...";break;
case __number: context="num";break;
case __string: context="str";break;
case __root: context="root";break;
case __operator: context="operator";break;
case __definition: context="definition";break;
case __assignment: context="assignment";break;
case __function: context="function";break;
case __loop: context="loop";break;
case __ifelse: context="if-else";break;
default: context="unknown_token";break;
}
std::cout<<context;
return;
}
#endif

3
version0.19/test.txt Normal file
View File

@ -0,0 +1,3 @@
1*1/1+1;
2*3*4/1-2+3;
1-1+20-2*10;