Fix errors in ebnf | add assignment in parser
This commit is contained in:
parent
62e1837704
commit
6ea09de3bc
|
@ -9,38 +9,38 @@
|
|||
<string> ::= STRING
|
||||
|
||||
//scalar
|
||||
<use_identifier> ::= <identifier>|<array_search>|<hash_search>
|
||||
<scalar> ::= <number>|<string>|<array>|<hash>|<use_identifier>|<use_function>
|
||||
<identifier> ::= <identifier>|<array_search>|<hash_search>
|
||||
<identifiers> ::= <identifier> <,> <identifier>
|
||||
<identifiers> ::= <identifiers> <,> <identifier>
|
||||
<scalar> ::= <number>|<string>|<array>|<hash>|<use_function>
|
||||
<scalars> ::= <scalar> <,> <scalar>
|
||||
<scalars> ::= <scalars> <,> <scalar>
|
||||
|
||||
//array
|
||||
<array_search> ::= <use_identifier> <[> <number> <]>
|
||||
<array> ::= <[> <scalar>|<scalars> <]>
|
||||
<array_search> ::= <identifier> <[> <number> <]>
|
||||
<array> ::= <[> <scalar>|<scalars>|<identifier>|<identifiers> <]>
|
||||
|
||||
//hash
|
||||
<hash_search> ::= <use_identifier> <.> <use_identifier>|<use_function>
|
||||
<hash_member> ::= <identifier> <:> <scalar>|<function>
|
||||
<hash_search> ::= <identifier> <.> <identifier>|<use_function>
|
||||
<hash_member> ::= <identifier> <:> <identifier>|<scalar>|<function>
|
||||
<hash_members> ::= <hash_member> <,> <hash_member>
|
||||
<hash_members> ::= <hash_members> <,> <hash_member>
|
||||
<hash> ::= <{> <hash_member>|<hash_members> <}>
|
||||
|
||||
//function
|
||||
<parameter> ::= <scalar>
|
||||
<parameters> ::= <scalars>
|
||||
<function> ::= <func> <(> <parameter>|<parameters> <)> <{> <statement>|<statements> <}>
|
||||
<use_function> ::= <use_identifier> <(> <parameter>|<parameters> <)>
|
||||
<function> ::= <func> <(> <identifier>|<identifiers> <)> <{> <statement>|<statements> <}>
|
||||
<use_function> ::= <use_identifier> <(> <scalar>|<scalars>|<identifier>|<identifiers> <)>
|
||||
|
||||
//definition & assignment
|
||||
<definition> ::= <var> <identifier> <=> <scalar>|<function> <;>
|
||||
<assignment> ::= <identifier> <=>|<+=>|<-=>|<*=>|</=>|<~=> <scalar> <;>
|
||||
<definition> ::= <var> <identifier> <=> <scalar>|<identifier>|<function> <;>
|
||||
<assignment> ::= <identifier> <=>|<+=>|<-=>|<*=>|</=>|<~=> <scalar>|<identifier> <;>
|
||||
|
||||
//calculation
|
||||
<add> ::= <scalar> <+> <scalar>
|
||||
<sub> ::= <scalar> <-> <scalar>
|
||||
<mul> ::= <scalar> <*> <scalar>
|
||||
<div> ::= <scalar> </> <scalar>
|
||||
<link> ::= <scalar> <~> <scalar>
|
||||
<add> ::= <scalar>|<identifier> <+> <scalar>|<identifier>
|
||||
<sub> ::= <scalar>|<identifier> <-> <scalar>|<identifier>
|
||||
<mul> ::= <scalar>|<identifier> <*> <scalar>|<identifier>
|
||||
<div> ::= <scalar>|<identifier> </> <scalar>|<identifier>
|
||||
<link> ::= <scalar>|<identifier> <~> <scalar>|<identifier>
|
||||
<calc> ::= <add>|<sub>|<mul>|<div>
|
||||
<calcs> ::= <(> <calc>|<calcs> <)>
|
||||
<calcs> ::= <calc> <+>|<->|<*>|</>|<~> <calc>
|
||||
|
@ -55,8 +55,8 @@
|
|||
//if else
|
||||
<condition_link_operator> ::= <and>|<or>|<!>
|
||||
<cmp_operator> ::= <==>|<>>|<<>|<<=>|<>=>|<!=>|<condition_link_operator>
|
||||
<condition> ::= <scalar>
|
||||
<condition> ::= <scalar> <cmp_operator> <scalar>
|
||||
<condition> ::= <scalar>|<identifier>
|
||||
<condition> ::= <scalar>|<identifier> <cmp_operator> <scalar>|<identifier>
|
||||
<conditions> ::= <(> <condition> <)> <condition_link_operator> <(> <condition> <)>
|
||||
<conditions> ::= <(> <conditions> <)> <condition_link_operator> <(> <condition> <)>
|
||||
<conditions> ::= <(> <condition> <)> <condition_link_operator> <(> <conditions> <)>
|
||||
|
@ -82,7 +82,7 @@
|
|||
<choose> ::= <_if> <else_ifs> <_else>
|
||||
|
||||
//statement
|
||||
<func_return> ::= <return> <scalar>
|
||||
<func_return> ::= <return> <scalar>|<identifier>
|
||||
<loop_continue> ::= <continue>
|
||||
<loop_break> ::= <break>
|
||||
<statement> ::= <definition>|<assignment>|<loop>|<choose>|<use_function>|<func_return>|<loop_continue>|<loop_break> <;>
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue