自动机变元
自动机结构
草稿结构图:
| if自动机
while自动机 | bool自动机
我自己倒是看得懂..
bool自动机归约成B,可以被if,while等自动机调用;
if和while自动机归约成S,被主自动机调用;
主自动机负责代码块;
算术自动机:
只能被bool自动机调用,最终归约成E(不是S)
代码部分
bool自动机代码
int boolautomat(enum Symbols * pst, enum Symbols * tpst, struct _token *sst)
{
int * ppst = NULL;//栈指针,用来遍历栈,返回操作码
ppst = tpst;
int state = 1;//状态
int isok = 0;//结束?
int opr = 0;
while (isok == 0)
{
switch (state)
{
case 1://开始态
switch (*ppst)
{
case NEGA:
state = 2;
break;
case NTS_E:
state = 4;
break;
case NTS_B:
state = 15;
break;
case L_PARENS:
state = 20;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 2://B->!B
switch (*ppst)
{
case NTS_B:
state = 3;
break;
default:
state = 1;
}
break;
case 4://B->E||E rop E
switch (*ppst)
{
case LT:
state = 5;
break;
case GT:
state = 6;
break;
case LE:
state = 7;
break;
case GE:
state = 8;
break;
case EQUL:
state = 9;
break;
default:
state = -1;
}
break;
case 5://B->E<.E
switch (*ppst)
{
case NTS_E:
state = 10;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 6://B->E>.E
switch (*ppst)
{
case NTS_E:
state = 11;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 7://B->E<=.E
switch (*ppst)
{
case NTS_E:
state = 12;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 8://B->E>=.E
switch (*ppst)
{
case NTS_E:
state = 13;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 9://B->E==.E
switch (*ppst)
{
case NTS_E:
state = 14;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = -1;
}
break;
case 15://B->B.||B |B.&&B |B.
switch (*ppst)
{
case OR:
state = 16;
break;
case AND:
state = 18;
break;
default:
state = -1;
}
break;
case 16://B->B||.B
switch (*ppst)
{
case NTS_B:
state = 17;
break;
case NEGA:
state = 2;
break;
case NTS_E:
state = 4;
break;
case L_PARENS:
state = 20;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = 1;
}
break;
case 18://B->B&&.B
switch (*ppst)
{
case NTS_B:
state = 19;
break;
case NEGA:
state = 2;
break;
case NTS_E:
state = 4;
break;
case L_PARENS:
state = 20;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = 1;
}
break;
case 20://B->(.B)
switch (*ppst)
{
case NTS_B:
state = 21;
break;
case NEGA:
state = 2;
break;
case NTS_E:
state = 4;
break;
case L_PARENS:
state = 20;
break;
case INT:
state = 23;
break;
case ID:
state = 23;
break;
default:
state = 1;
}
break;
case 21://B->(B.)
switch (*ppst)
{
case R_PARENS:
state = 22;
break;
default:
state = -1;
}
break;
}
ppst++;
if (ppst == (pst + pstack))//指针在栈顶
{
int z = 0;
switch (state)
{
case -1:
opr = -1;
break;
case 3:
opr= 2;
break;
case 4://预读 rop
switch (tokentable[ptid].value)//预读
{
case LE:
opr = 0;
break;
case LT:
opr = 0;
break;
case GE:
opr = 0;
break;
case GT:
opr = 0;
break;
case EQUL:
opr = 0;
break;
case L_PARENS:
opr = -1;
break;
default:
opr = 3;
break;
}
break;
case 10://B->E<E
opr = 4;
break;
case 11://B->E>E
opr = 5;
break;
case 12://B->E<=E
opr = 6;
break;
case 13://B->E>=E
opr = 7;
break;
case 14://B->E==E
opr = 8;
break;
case 15://预读 || &&
switch (tokentable[ptid].value)//预读
{
case AND:
opr = 0;
break;
case OR:
opr = 0;
break;
case L_PARENS:
opr = -1;
break;
default://结束状态
return 1;
break;
}
break;
case 17://B->B||B
opr = 9;
break;
case 19://B->B && B
opr = 10;
break;
case 22://B->(B)
opr = 11;
break;
case 23://调用算术自动机
z = 0;
z = arithautomat(pst,(ppst - 1), sst);
if (z == 0)
{
return 0;
}
opr = 99;
break;
default:
opr = 0;//其他情况均请求归约器入栈
}
int ok = boolredu(pst, sst, opr);
if (ok == -1)
{
return 0;
}
state = 1;
ppst = tpst;//栈指针归位
outs(pst);
}
}
}
while自动机代码
int whileautomat(enum Symbols * pst, enum Symbols * tpst, struct _token *sst)
{
int * ppst = NULL;//栈指针,用来遍历栈,返回操作码
ppst = tpst;
int state = 1;//状态
int isok = 0;//结束?
int opr = 0;
while (isok == 0)
{
switch (state)
{
case 1://开始态
switch (*ppst)
{
case WHILE:
state = 2;
break;
case NTS_W:
state = 3;
break;
case NTS_WD:
state = 6;
break;
case NTS_S:
return 1;
default:
state = -1;
}
break;
case 3://WD->W.B do
switch (*ppst)
{
case NTS_B:
state = 4;
break;
default:
state = 8;
}
break;
case 4://WD->W B. do
switch (*ppst)
{
case DO:
state = 5;
break;
default:
state = -1;
}
break;
case 6://S->WD.S
switch (*ppst)
{
case NTS_S:
state = 7;
break;
default:
state = 9;
}
break;
}
ppst++;
if (ppst == (pst + pstack))//指针在栈顶
{
int z = 0;
switch (state)
{
case -1:
opr = -1;
break;
case 2:
opr = 2;
break;
case 5://WD->W B do
opr = 3;
break;
case 7://S->WD S
opr = 4;
break;
case 8://调用bool自动机
z = 0;
z = boolautomat(pst, (ppst - 1), sst);
if (z == 0)
{
return 0;
}
opr = 99;
break;
case 9://调用主自动机
z = 0;
z = mainautomat(pst, (ppst - 1), sst);
if (z == 0)
{
return 0;
}
opr = 99;
break;
default:
opr = 0;//其他情况均请求归约器入栈
}
int ok = whileredu(pst, sst, opr);
if (ok == -1)
{
return 0;
}
state = 1;
ppst = tpst;//栈指针归位
outs(pst);
}
}
}
if自动机代码
int ifautomat(enum Symbols * pst, enum Symbols * tpst, struct _token *sst)
{
int * ppst = NULL;//栈指针,用来遍历栈,返回操作码
ppst = tpst;
int state = 1;//状态
int isok = 0;//结束?
int opr = 0;
while (isok == 0)
{
switch (state)
{
case 1://开始态
switch (*ppst)
{
case IF:
state = 2;
break;
case NTS_CA:
state = 5;
break;
case NTS_FA:
state = 17;
break;
case NTS_S:
return 1;
default:
state = -1;
}
break;
case 2://CA->if. B them
switch (*ppst)
{
case NTS_B:
state = 3;
break;
default:
state = 18;
}
break;
case 3://CA->if B. them
switch (*ppst)
{
case THEN:
state = 4;
break;
default:
state = -1;
}
break;
case 5://FA->CA.S
switch (*ppst)
{
case NTS_S:
state = 6;
break;
case BEGIN:
state = 19;
break;
default:
state = -1;
}
break;
case 7://CBE->else. if
switch (*ppst)
{
case IF:
state = 9;
break;
default:
state = -1;
}
break;
case 10://CB->CBE. B then
switch (*ppst)
{
case NTS_B:
state = 11;
break;
default:
state = 18;
}
break;
case 11://CB->CBE B. then
switch (*ppst)
{
case THEN:
state = 12;
break;
default:
state = -1;
}
break;
case 13://FB->CB. S
switch (*ppst)
{
case NTS_S:
state = 14;
break;
case BEGIN:
state = 19;
default:
state = 19;
}
break;
case 15://FC->CE.S
switch (*ppst)
{
case NTS_S:
state = 16;
break;
case BEGIN:
state = 19;
break;
default:
state = 19;
}
break;
case 17://S->FA.***
switch (*ppst)
{
case ELSE:
state = 7;
break;
case NTS_CBE:
state = 10;
break;
case NTS_CB:
state = 13;
break;
case NTS_CE:
state = 15;
break;
case NTS_FB:
state = 20;
break;
case NTS_FC:
state = 22;
break;
default:
state = -1;
}
break;
case 20://S->FA FB...FB FC
switch (*ppst)
{
case NTS_FB:
state = 20;
break;
case NTS_FC:
state = 21;
break;
default:
state = 17;
ppst--;
}
break;
}
ppst++;
if (ppst == (pst + pstack))//指针在栈顶
{
int z = 0;
switch (state)
{
case -1:
opr = -1;
break;
case 4://CA->if B then
opr = 2;
break;
case 6://FA->CA S
opr = 3;
break;
case 7://预读 IF
switch (tokentable[ptid].value)//预读
{
case IF:
opr = 0;
break;
default:
opr = 4;//CE->else
break;
}
break;
case 9://CBE->else if
opr = 5;
break;
case 12://CB->CBE B then
opr = 6;
break;
case 14://FB->CB S
opr = 7;
break;
case 16://FC->CE S
opr = 8;
break;
case 17://预读
switch (tokentable[ptid].value)//预读
{
case ELSE:
opr = 0;
break;
case NTS_CBE:
opr = 0;
break;
case NTS_CB:
opr = 0;
break;
case NTS_CE:
opr = 0;
break;
case NTS_FB:
opr = 0;
break;
default:
opr = 10;//S->FA
break;
}
break;
case 18://调布尔自动机
z = 0;
z = boolautomat(pst, (ppst - 1), sst);
if (z == 0)
{
return 0;
}
opr = 99;
break;
case 19://调主自动机
z = 0;
z = mainautomat(pst, (ppst - 1), sst);
if (z == 0)
{
return 0;
}
opr = 99;
break;
case 20://预读
switch (tokentable[ptid].value)//预读
{
case ELSE:
opr = 0;
break;
case NTS_FB:
opr = 0;
break;
case NTS_FC:
opr = 0;
break;
default:
opr = 12;//S->FAFB...FB
break;
}
break;
case 21://S->FA FB...FB FC
opr = 9;
break;
case 22:
opr = 11;
break;
default:
opr = 0;//其他情况均请求归约器入栈
}
int ok = ifredu(pst, sst, opr);
if (ok == -1)
{
return 0;
}
state = 1;
ppst = tpst;//栈指针归位
outs(pst);
}
}
}
算术自动机代码
int arithautomat(enum Symbols * pst, enum Symbols * tpst, struct _token *sst)//(语法栈,语法栈当前位置,语义栈,)
{
int * ppst = NULL;//栈指针,用来遍历栈,返回操作码
ppst = tpst;
int state = 1;//状态
int isok = 0;//结束?
int opr = 0;
while (isok == 0)
{
//不在栈顶
switch (state)
{
case 1://开始态
switch (*ppst)
{
case NTS_T:
state = 2;
break;
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case L_PARENS:
state = 4;
break;
case NTS_E:
state = 15;
break;
default:
state = -1;
}
break;
case 2://E->T|T.+E|T.-E
switch (*ppst)
{
case PLUS:
state = 5;
break;
case SUB:
state = 6;
break;
default:
state = -1;
}
break;
case 3://T->INT.*T|INT./T|INT
switch (*ppst)
{
case MULT:
state = 7;
break;
case DIV:
state = 8;
break;
default:
state = -1;
}
break;
case 4://T->(.E)|E->|T->|
switch (*ppst)
{
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case NTS_T:
state = 2;
break;
case NTS_E:
state = 13;
break;
default:
state = -1;
}
break;
case 5://T->INT|INT*T|INT/T|(E)|E->T-.E|T+.E|T
switch (*ppst)
{
case NTS_E:
state = 9;
break;
case NTS_T:
state = 2;
break;
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case L_PARENS:
state = 4;
break;
default:
state = -1;
}
break;
case 6://同5,-推导出6状态
switch (*ppst)
{
case NTS_E:
state = 10;
break;
case NTS_T:
state = 2;
break;
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case L_PARENS:
state = 4;
break;
default:
state = -1;
}
break;
case 7://T->INT*.T|INT/.T|INT|.INT*T|.INT/T
switch (*ppst)
{
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case NTS_T:
state = 11;
break;
case L_PARENS:
state = 4;
break;
default:
state = -1;
}
break;
case 8://同7,/推导出8状态
switch (*ppst)
{
case INT:
state = 3;
break;
case ID:
state = 3;
break;
case NTS_T:
state = 12;
break;
case L_PARENS:
state = 4;
break;
default:
state = -1;
}
break;
case 13://E->(E.)
switch (*ppst)
{
case R_PARENS:
state = 14;
break;
default:
state = -1;
}
break;
}
ppst++;
if (ppst == (pst + pstack))//指针在栈顶
{
switch (state)
{
case -1:
opr=-1;
break;
case 2://预读+.-
switch (tokentable[ptid].value)//预读
{
case PLUS:
opr = 0;
break;
case SUB:
opr = 0;
break;
case L_PARENS:
opr = -1;
break;
default:
opr = 2;
break;
}
break;
case 3://预读*./
switch (tokentable[ptid].value)//预读
{
case MULT:
opr = 0;
break;
case DIV:
opr = 0;
break;
case L_PARENS:
opr = -1;
break;
default:
opr = 8;
break;
}
break;
case 9://T+E.
opr = 3;
break;
case 10://T-E.
opr = 4;
break;
case 11://INT*T.
opr = 6;
break;
case 12://INT/T.
opr = 7;
break;
case 14://T->(E).
opr = 5;
break;
case 15://E.归约完成
return 1;
default:
opr = 0;//其他情况均请求归约器入栈
}
int ok=arithredu(pst, sst, opr);
if (ok == -1)
{
return 0;//错误,返回0
}
state = 1;
ppst = tpst;//栈指针归位
outs(pst);
}
}
}
归约部分则是弹栈压栈,然后结合语义填四元式,之后放上来