Bug fixed

This commit is contained in:
Valk Richard Li 2019-08-27 09:22:35 -05:00 committed by GitHub
parent bdbace0cab
commit ca7a2f1459
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 269 additions and 157 deletions

View File

@ -22,15 +22,17 @@ enum token_type
__dot,// .
__var,// var reserve word
__func,// func reserve word
__id,__identifier,__identifiers,__identifier_end,
__id,__identifier,__identifiers,__identifier_end,__lacked_identifier,
__parameter,__parameters,__parameter_end,
__number,__string,
__scalar,__scalars,__scalar_end,
__list,__hash,
__hash_member,__hash_members,
__hash_member,__hash_members,__hash_member_end,__hash_suffix,
__statement,__statements,
__function,//function(){}
__definition,__assignment,__calculation,
__definition,
__assignment,__pre_assignment,
__calculation,
__loop,__continue,__break,__for,__forindex,__foreach,__while,// for()while() continue; break;
__choose,__if,__elsif,__else,// if else if else
__return
@ -43,19 +45,10 @@ struct cmp_seq
int res;
}par[]=
{
{{__var,__identifier,__equal,__scalar,__semi}, __definition},
{{__var,__identifier,__equal,__identifier,__semi}, __definition},
{{__var,__identifier,__equal,__list,__semi}, __definition},
{{__var,__identifier,__equal,__hash,__semi}, __definition},
{{__var,__identifier,__equal,__function}, __definition},
{{__var,__left_curve,__identifiers,__right_curve,__equal,__identifier,__semi},__definition},
{{__var,__left_curve,__identifiers,__right_curve,__equal,__list,__semi}, __definition},
{{__identifier,__dot,__identifier}, __identifier},
{{__identifier,__left_bracket,__scalar,__right_bracket}, __identifier},
{{__identifier,__left_bracket,__identifier,__right_bracket}, __identifier},
{{__identifier,__left_bracket,__calculation,__right_bracket}, __identifier},
{{__identifier,__left_curve,__right_curve}, __identifier},
{{__identifier,__left_curve,__scalar,__right_curve}, __identifier}
{{__var,__id,__equal,__string,__semi},__definition},
{{__number}, __scalar},
{{__var,__id,__equal,__scalar,__semi},__definition},
{{__var,__id,__equal,__id,__semi}, __definition}
};
int num_of_par=sizeof(par)/sizeof(cmp_seq);
@ -164,7 +157,7 @@ void print_token_type(int type)
context="func";
break;
case __id:
context="unknown_id";
context="id";
break;
case __identifier:
context="identifier";
@ -250,8 +243,6 @@ class PDA
{
private:
std::stack<parse_unit> main_stack;
std::stack<parse_unit> comp_stack;
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> error_stack;
public:
void stack_input(std::stack<parse_unit>& temp)
@ -263,71 +254,8 @@ class PDA
}
return;
}
void turn_back_stack_member(const int num)
void print_error()
{
for(int i=0;i<num;++i)
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
return;
}
void comp_progress()
{
parse_unit temp;
temp.line=0;
temp.type=0;
while(!main_stack.empty())
{
bool ispar=false;
for(int i=0;i<num_of_par;++i)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
int cnt=0;
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==__null_end)
{
++cnt;
comp_stack.pop();
if(comp_stack.empty())
break;
}
if(comp_stack.top().type==main_stack.top().type)
{
++cnt;
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else
{
turn_back_stack_member(cnt);
break;
}
}
if(comp_stack.empty())
{
ispar=true;
break;
}
}
if(!ispar)
{
error_stack.push(main_stack.top());
main_stack.pop();
}
}
if(!error_stack.empty())
{
std::stack<parse_unit> temp;
@ -355,6 +283,127 @@ class PDA
std::cout<<">>[Parse] 0 error occurred."<<std::endl;
return;
}
bool extend_comp_progress(const int type)
{
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> comp_stack;
parse_unit temp;
temp.line=0;
temp.type=0;
for(int i=0;i<num_of_par;++i)
{
if(par[i].res==type)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==main_stack.top().type)
{
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else if(comp_stack.top().type==__null_end)
comp_stack.pop();
else if(comp_stack.top().type!=main_stack.top().type)
{
if(!extend_comp_progress(comp_stack.top().type))
{
while(!recognized_stack.empty())
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
break;
}
else
comp_stack.pop();
}
}
if(comp_stack.empty())
{
while(!recognized_stack.empty())
recognized_stack.pop();
return true;
}
}
}
return false;
}
void main_comp_progress()
{
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> comp_stack;
parse_unit temp;
temp.line=0;
temp.type=0;
while(!main_stack.empty())
{
bool ispar=false;
for(int i=0;i<num_of_par;++i)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==main_stack.top().type)
{
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else if(comp_stack.top().type==__null_end)
comp_stack.pop();
else if(comp_stack.top().type!=main_stack.top().type)
{
if(!extend_comp_progress(comp_stack.top().type))
{
while(!recognized_stack.empty())
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
break;
}
else
comp_stack.pop();
}
}
if(comp_stack.empty())
{
while(!recognized_stack.empty())
recognized_stack.pop();
ispar=true;
break;
}
}
if(!ispar && !main_stack.empty())
{
error_stack.push(main_stack.top());
main_stack.pop();
}
}
print_error();
return;
}
};
class nasal_parser
@ -391,7 +440,7 @@ class nasal_parser
}
else if((*i).type==IDENTIFIER)
{
temp_parse.type=__identifier;
temp_parse.type=__id;
}
else if(((*i).content=="for") || ((*i).content=="foreach") || ((*i).content=="while") || ((*i).content=="forindex"))
{
@ -443,7 +492,10 @@ class nasal_parser
}
else if(((*i).type==NUMBER) || ((*i).type==STRING))
{
temp_parse.type=__scalar;
if((*i).type==NUMBER)
temp_parse.type=__number;
else
temp_parse.type=__string;
}
else if(((*i).content=="+") || ((*i).content=="-") || ((*i).content=="*") || ((*i).content=="/") || ((*i).content=="~") || ((*i).content=="!"))
{
@ -502,7 +554,7 @@ class nasal_parser
}
PDA automata;
automata.stack_input(parser);
automata.comp_progress();
automata.main_comp_progress();
return;
}
};
@ -515,7 +567,6 @@ int main()
std::string str="a.txt";
prog.input_file(str);
lex.lexer_process(prog.use_file());
lex.print_lexer();
pas.parse_quiet_process(lex.return_list());
return 0;
}

View File

@ -12,6 +12,7 @@ struct parse_unit
enum token_type
{
__stack_end,
__null_end,// $
__equal,// =
__cmp_equal,// ==
__cmp_not_equal,// !=
@ -29,13 +30,17 @@ enum token_type
__dot,// .
__var,// var reserve word
__func,// func reserve word
__unknown_type_id,__identifier,__identifiers,
__id,__identifier,__identifiers,__identifier_end,__lacked_identifier,
__parameter,__parameters,__parameter_end,
__number,__string,
__scalar,__scalars,__list,__hash,
__hash_member,__hash_members,
__scalar,__scalars,__scalar_end,
__list,__hash,
__hash_member,__hash_members,__hash_member_end,__hash_suffix,
__statement,__statements,
__function,//function(){}
__definition,__assignment,__calculation,
__definition,
__assignment,__pre_assignment,
__calculation,
__loop,__continue,__break,__for,__forindex,__foreach,__while,// for()while() continue; break;
__choose,__if,__elsif,__else,// if else if else
__return
@ -68,8 +73,6 @@ class PDA
{
private:
std::stack<parse_unit> main_stack;
std::stack<parse_unit> comp_stack;
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> error_stack;
public:
void stack_input(std::stack<parse_unit>& temp)
@ -81,71 +84,8 @@ class PDA
}
return;
}
void turn_back_stack_member(const int num)
void print_error()
{
for(int i=0;i<num;++i)
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
return;
}
void comp_progress()
{
parse_unit temp;
temp.line=0;
temp.type=0;
while(!main_stack.empty())
{
bool ispar=false;
for(int i=0;i<num_of_par;++i)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
int cnt=0;
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==__null_end)
{
++cnt;
comp_stack.pop();
if(comp_stack.empty())
break;
}
if(comp_stack.top().type==main_stack.top().type)
{
++cnt;
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else
{
turn_back_stack_member(cnt);
break;
}
}
if(comp_stack.empty())
{
ispar=true;
break;
}
}
if(!ispar)
{
error_stack.push(main_stack.top());
main_stack.pop();
}
}
if(!error_stack.empty())
{
std::stack<parse_unit> temp;
@ -173,4 +113,125 @@ class PDA
std::cout<<">>[Parse] 0 error occurred."<<std::endl;
return;
}
bool extend_comp_progress(const int type)
{
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> comp_stack;
parse_unit temp;
temp.line=0;
temp.type=0;
for(int i=0;i<num_of_par;++i)
{
if(par[i].res==type)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==main_stack.top().type)
{
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else if(comp_stack.top().type==__null_end)
comp_stack.pop();
else if(comp_stack.top().type!=main_stack.top().type)
{
if(!extend_comp_progress(comp_stack.top().type))
{
while(!recognized_stack.empty())
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
break;
}
else
comp_stack.pop();
}
}
if(comp_stack.empty())
{
while(!recognized_stack.empty())
recognized_stack.pop();
return true;
}
}
}
return false;
}
void main_comp_progress()
{
std::stack<parse_unit> recognized_stack;
std::stack<parse_unit> comp_stack;
parse_unit temp;
temp.line=0;
temp.type=0;
while(!main_stack.empty())
{
bool ispar=false;
for(int i=0;i<num_of_par;++i)
{
while(!comp_stack.empty())
comp_stack.pop();
for(int j=max_token_len-1;j>=0;--j)
{
if(par[i].tokens[j])
{
temp.type=par[i].tokens[j];
comp_stack.push(temp);
}
}
while((!comp_stack.empty()) && (!main_stack.empty()))
{
if(comp_stack.top().type==main_stack.top().type)
{
comp_stack.pop();
recognized_stack.push(main_stack.top());
main_stack.pop();
}
else if(comp_stack.top().type==__null_end)
comp_stack.pop();
else if(comp_stack.top().type!=main_stack.top().type)
{
if(!extend_comp_progress(comp_stack.top().type))
{
while(!recognized_stack.empty())
{
main_stack.push(recognized_stack.top());
recognized_stack.pop();
}
break;
}
else
comp_stack.pop();
}
}
if(comp_stack.empty())
{
while(!recognized_stack.empty())
recognized_stack.pop();
ispar=true;
break;
}
}
if(!ispar && !main_stack.empty())
{
error_stack.push(main_stack.top());
main_stack.pop();
}
}
print_error();
return;
}
};