This commit is contained in:
Valk Richard Li 2019-08-20 23:56:03 +08:00 committed by GitHub
parent cdb9e88cbf
commit d333b706ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 135 additions and 11 deletions

View File

@ -57,14 +57,8 @@
<11><choose> ::= <11><choose> ::=
<...> <...>
<12><statement> ::= <12><statement> ::=
<definition> <definition>|<assignment>|<__func>|<loop>|<choose>
<assignment> <identifier>|<continue>|<break> <;>
<__func>
<identifier> <;>
<return> <identifier>|<scalar> <;> <return> <identifier>|<scalar> <;>
<continue> <;>
<break> <;>
<loop>
<choose>
<13><statements> ::= <13><statements> ::=
<statement>|<statements> <statement> <statement>|<statements> <statement>

View File

@ -45,6 +45,8 @@ class parse
bool identifiers_reduction(); bool identifiers_reduction();
bool hashmember_check(); bool hashmember_check();
bool hashmembers_reduction(); bool hashmembers_reduction();
bool statement_check();
bool statements_reduction();
bool definition_check(); bool definition_check();
bool assignment_check(); bool assignment_check();
void parse_work(token_list&); void parse_work(token_list&);
@ -187,9 +189,9 @@ bool parse::identifiers_reduction()
} }
bool parse::hashmember_check() bool parse::hashmember_check()
{ {
int tbl[3]={0}; int tbl[9]={0};
std::stack<parse_unit> temp; std::stack<parse_unit> temp;
for(int i=0;i<3;++i) for(int i=0;i<9;++i)
{ {
if(parser.empty()) if(parser.empty())
break; break;
@ -197,7 +199,7 @@ bool parse::hashmember_check()
tbl[i]=temp.top().type; tbl[i]=temp.top().type;
parser.pop(); parser.pop();
} }
for(int i=0;i<3;++i) for(int i=0;i<9;++i)
{ {
if(temp.empty()) if(temp.empty())
break; break;
@ -214,6 +216,46 @@ bool parse::hashmember_check()
parser.push(t); parser.push(t);
return true; return true;
} }
else if((tbl[6]==__identifier) && (tbl[5]==__colon) && (tbl[4]==__func) && (tbl[3]==__left_curve) && (tbl[2]==__right_curve) && (tbl[1]==__left_brace) && (tbl[0]==__right_brace))
{
parse_unit t;
t.type=__hash_member;
t.line=parser.top().line;
for(int i=0;i<7;++i)
parser.pop();
parser.push(t);
return true;
}
else if((tbl[7]==__identifier) && (tbl[6]==__colon) && (tbl[5]==__func) && (tbl[4]==__left_curve) && ((tbl[3]==__identifier) || (tbl[3]==__identifiers)) && (tbl[2]==__right_curve) && (tbl[1]==__left_brace) && (tbl[0]==__right_brace))
{
parse_unit t;
t.type=__hash_member;
t.line=parser.top().line;
for(int i=0;i<8;++i)
parser.pop();
parser.push(t);
return true;
}
else if((tbl[7]==__identifier) && (tbl[6]==__colon) && (tbl[5]==__func) && (tbl[4]==__left_curve) && (tbl[3]==__right_curve) && (tbl[2]==__left_brace) && ((tbl[1]==__statement) || (tbl[1]==__statements)) && (tbl[0]==__right_brace))
{
parse_unit t;
t.type=__hash_member;
t.line=parser.top().line;
for(int i=0;i<8;++i)
parser.pop();
parser.push(t);
return true;
}
else if((tbl[8]==__identifier) && (tbl[7]==__colon) && (tbl[6]==__func) && (tbl[5]==__left_curve) && ((tbl[4]==__identifier) || (tbl[4]==__identifiers)) && (tbl[3]==__right_curve) && (tbl[2]==__left_brace) && ((tbl[1]==__statement) || (tbl[1]==__statements)) && (tbl[0]==__right_brace))
{
parse_unit t;
t.type=__hash_member;
t.line=parser.top().line;
for(int i=0;i<9;++i)
parser.pop();
parser.push(t);
return true;
}
return false; return false;
} }
bool parse::hashmembers_reduction() bool parse::hashmembers_reduction()
@ -389,6 +431,87 @@ bool parse::assignment_check()
} }
return false; return false;
} }
bool parse::statement_check()
{
int tbl[3]={0};
std::stack<parse_unit> temp;
for(int i=0;i<3;++i)
{
if(parser.empty())
break;
temp.push(parser.top());
tbl[i]=temp.top().type;
parser.pop();
}
for(int i=0;i<3;++i)
{
if(temp.empty())
break;
parser.push(temp.top());
temp.pop();
}
if((tbl[0]==__definition) || (tbl[0]==__assignment) || (tbl[0]==__func) || (tbl[0]==__loop) || (tbl[0]==__choose))
{
parse_unit t;
t.type=__statement;
t.line=parser.top().line;
parser.pop();
parser.push(t);
return true;
}
else if((tbl[2]==__return) && ((tbl[1]==__identifier) || (tbl[1]==__scalar)) && (tbl[0]==__semi))
{
parse_unit t;
t.type=__statement;
t.line=parser.top().line;
for(int i=0;i<3;++i)
parser.pop();
parser.push(t);
return true;
}
else if((tbl[2]!=__return) && ((tbl[1]==__identifier) || (tbl[1]==__continue) || (tbl[1]==__break)) && (tbl[0]==__semi))
{
parse_unit t;
t.type=__statement;
t.line=parser.top().line;
for(int i=0;i<2;++i)
parser.pop();
parser.push(t);
return true;
}
return false;
}
bool parse::statements_reduction()
{
int tbl[2]={0};
std::stack<parse_unit> temp;
for(int i=0;i<2;++i)
{
if(parser.empty())
break;
temp.push(parser.top());
tbl[i]=temp.top().type;
parser.pop();
}
for(int i=0;i<2;++i)
{
if(temp.empty())
break;
parser.push(temp.top());
temp.pop();
}
if(((tbl[1]==__statement) || (tbl[1]==__statements)) && (tbl[0]==__statement))
{
parse_unit t;
t.type=__statements;
t.line=parser.top().line;
for(int i=0;i<2;++i)
parser.pop();
parser.push(t);
return true;
}
return false;
}
void parse::stack_set_empty() void parse::stack_set_empty()
{ {
while(!parser.empty()) while(!parser.empty())
@ -535,6 +658,13 @@ void parse::parse_work(token_list& lexer)
std::cout<<"line "<<parser.top().line<<": Assignment"<<std::endl; std::cout<<"line "<<parser.top().line<<": Assignment"<<std::endl;
continue; continue;
} }
if(statement_check())
{
std::cout<<"line "<<parser.top().line<<": Statement"<<std::endl;
if(statements_reduction())
std::cout<<"line "<<parser.top().line<<": Statements"<<std::endl;
continue;
}
reduction_complete=true; reduction_complete=true;
} }
} }