Fix errors in ebnf | add assignment in parser

This commit is contained in:
Valk Richard Li
2019-08-14 14:21:38 +08:00
committed by GitHub
parent 62e1837704
commit 6ea09de3bc
2 changed files with 95 additions and 24 deletions
+75 -4
View File
@@ -5,8 +5,18 @@
enum token_type
{
__stack_end,__identifier,__use_identifier,
__scalar,__function,__var,__equal,__semi,__definition
__stack_end,
__equal,// =
__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,// ;
__var,// var
__identifier,__scalar,__function,
__definition,__assignment,
};
token_type token_type_tbl;
@@ -22,6 +32,7 @@ class parse
std::stack<parse_unit> parser;
public:
void definition_reduction();
void assignment_reduction();
void parse_work(token_list&);
};
@@ -37,7 +48,7 @@ void parse::definition_reduction()
tbl[i]=temp.top().type;
parser.pop();
}
if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && tbl[3]==__scalar && tbl[4]==__semi)
if(tbl[0]==__var && tbl[1]==__identifier && tbl[2]==__equal && (tbl[3]==__scalar || tbl[3]==__identifier || tbl[3]==__function) && tbl[4]==__semi)
{
parse_unit temp_parse_unit;
temp_parse_unit.type=__definition;
@@ -54,6 +65,35 @@ void parse::definition_reduction()
}
return;
}
void parse::assignment_reduction()
{
int tbl[4];
std::stack<parse_unit> temp;
for(int i=3;i>=0;--i)
{
if(parser.empty())
break;
temp.push(parser.top());
tbl[i]=temp.top().type;
parser.pop();
}
if(tbl[0]==__identifier && tbl[1]==__equal && (tbl[2]==__scalar || tbl[2]==__identifier) && tbl[3]==__semi)
{
parse_unit temp_parse_unit;
temp_parse_unit.type=__assignment;
temp_parse_unit.line=temp.top().line;
parser.push(temp_parse_unit);
}
else
for(int i=0;i<4;++i)
{
if(temp.empty())
break;
parser.push(temp.top());
temp.pop();
}
return;
}
void parse::parse_work(token_list& lexer)
{
@@ -88,10 +128,41 @@ void parse::parse_work(token_list& lexer)
token_type_tbl=__scalar;
temp_parse.type=token_type_tbl;
}
else if(temp->content=="+" || temp->content=="-" || temp->content=="*" || temp->content=="/" || temp->content=="~")
{
if(temp->content=="+")
token_type_tbl=__add_operator;
else if(temp->content=="-")
token_type_tbl=__sub_operator;
else if(temp->content=="*")
token_type_tbl=__mul_operator;
else if(temp->content=="/")
token_type_tbl=__div_operator;
else if(temp->content=="~")
token_type_tbl=__link_operator;
temp_parse.type=token_type_tbl;
}
else if(temp->content=="+=" || temp->content=="-=" || temp->content=="*=" || temp->content=="/=" || temp->content=="~=")
{
if(temp->content=="+=")
token_type_tbl=__add_equal;
else if(temp->content=="-=")
token_type_tbl=__sub_equal;
else if(temp->content=="*=")
token_type_tbl=__mul_equal;
else if(temp->content=="/=")
token_type_tbl=__div_equal;
else if(temp->content=="~=")
token_type_tbl=__link_equal;
temp_parse.type=token_type_tbl;
}
parser.push(temp_parse);
definition_reduction();
assignment_reduction();
if(parser.top().type==__definition)
std::cout<<"Definition in "<<parser.top().line<<std::endl;
std::cout<<"line "<<parser.top().line<<": definition"<<std::endl;
else if(parser.top().type==__assignment)
std::cout<<"line "<<parser.top().line<<": assignment"<<std::endl;
}
return;
}