一个复杂一点的例子
下面是一个复杂一点的支持Pascal子集的例子。语法文件如下:
command->program;
program->stmtSeq
{
DPRINT("In Reduce:program->stmtSeq\n");
D(1)->Interpreter();
};
stmtSeq->stmtSeq NEWLINE stmt
{
DPRINT("In Reduce:stmtSeq->stmtSeq NEWLINE stmt \n");
DD = new stmtSeqInter((stmtSeqInter*)D(1),(stmtInter*)D(3));
};
stmtSeq->stmt
{
DPRINT("In Reduce:stmtSeq->stmt\n");
DD = new stmtSeqInter(NULL,(stmtInter*)D(1));
};
stmt->ifStmt
{
DPRINT("In Reduce:stmt->ifStmt\n");
DD = new stmtInter(stmtInter::stmtKind::ifStmtKind,(ifInterStmt*)D(1));
};
stmt->whileStmt
{
DPRINT("In Reduce:stmt->whileStmt\n");
DD=new stmtInter(stmtInter::stmtKind::whileStmtKind,
(whileStmtInter*)D(1));
};
stmt->assignStmt
{
DPRINT("In Reduce:stmt->assignStmt\n");
DD = new stmtInter(stmtInter::stmtKind::assignStmtKind,
(AssignInterStmt*)D(1));
};
stmt->inputStmt
{
DPRINT("In Reduce:stmt->inputStmt\n");
DD = new stmtInter(stmtInter::stmtKind::inputStmtKind,
(inputInterStmt*)D(1));
};
stmt->printStmt
{
DPRINT("In Reduce:stmt->printStmt\n");
DD = new stmtInter(stmtInter::stmtKind::printStmtKind,
(printInterStmt*)D(1));
};
inputStmt->INPUT ID
{
DPRINT("In Reduce:inputStmt->INPUT ID\n");
DD = new inputInterStmt(D(2)->tokenstr);
};
printStmt->PRINT exp
{
DPRINT("In Reduce:printStmt->PRINT exp\n");
DD = new printInterStmt((expStmt*)D(2));
};
ifStmt->IF exp THEN BEGIN stmtSeq END
{
DPRINT("In Reduce:ifStmt->IF exp THEN BEGIN stmtSeq END\n");
DD = new ifInterStmt((expStmt*)D(2),(stmtSeqInter*)D(5),NULL);
};
ifStmt->IF exp THEN BEGIN stmtSeq END ELSE BEGIN stmtSeq END
{
DPRINT("In Reduce:ifStmt->IF exp THEN BEGIN stmtSeq END ELSE BEGIN stmtSeq END\n");
DD = new ifInterStmt((expStmt*)D(2),(stmtSeqInter*)D(5),(stmtSeqInter*)D(9));
};
whileStmt->WHILE exp BEGIN stmtSeq END
{
DPRINT("In Reduce:whileStmt->WHILE exp BEGIN stmtSeq END\n");
DD = new whileStmtInter((expStmt*)D(2),(stmtSeqInter*)D(4));
};
assignStmt->ID ASSIGN exp
{
DPRINT("In Reduce:assignStmt->ID ASSIGN exp\n");
DD = new AssignInterStmt(D(1)->tokenstr,(expStmt*)D(3));
};
exp->simpleExp LT simpleExp
{
DPRINT("In Reduce:exp->simpleExp LT simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),LT,(simExpStmt*)D(3));
};
exp->simpleExp LE simpleExp
{
DPRINT("In Reduce:exp->simpleExp LE simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),LE,(simExpStmt*)D(3));
};
exp->simpleExp EQ simpleExp
{
DPRINT("In Reduce:exp->simpleExp EQ simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),EQ,(simExpStmt*)D(3));
};
exp->simpleExp GT simpleExp
{
DPRINT("In Reduce:exp->simpleExp GT simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),GT,(simExpStmt*)D(3));
};
exp->simpleExp GE simpleExp
{
DPRINT("In Reduce:exp->simpleExp GE simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),GE,(simExpStmt*)D(3));
};
exp->simpleExp NE simpleExp
{
DPRINT("In Reduce:exp->simpleExp NE simpleExp\n");
DD = new expStmt((simExpStmt*)D(1),NE,(simExpStmt*)D(3));
};
exp->simpleExp
{
DPRINT("In Reduce:exp->simpleExp\n");
DD = new expStmt(NULL,-1,(simExpStmt*)D(1));
};
simpleExp->simpleExp PLUS term
{
DPRINT("In Reduce:simpleExp->simpleExp PLUS term\n");
DD = new simExpStmt((simExpStmt*)D(1),PLUS,(termStmt*)D(3));
};
simpleExp->simpleExp MINUS term
{
DPRINT("In Reduce:simpleExp->simpleExp MINUS term\n");
DD = new simExpStmt((simExpStmt*)D(1),MINUS,(termStmt*)D(3));
};
simpleExp->term
{
DPRINT("In Reduce:simpleExp->term\n");
DD = new simExpStmt(NULL,-1,(termStmt*)D(1));
}
;
term->term TIMES factor
{
DPRINT("In Reduce:term->term TIMES factor\n");
DD = new termStmt((termStmt*)D(1),(factorStmt*)D(3));
};
term->factor
{
DPRINT("In Reduce:term->factor\n");
DD = new termStmt(NULL,(factorStmt*)D(1));
};
factor->LP exp RP
{
DPRINT("In Reduce:factor->LP exp RP\n");
factorStmt *p= new factorStmt;
p->Key.kind = factorStmt::factorKIND::expfactorKind;
p->Key.u.ExpInFactor = (expStmt*)D(2);
};
factor->ID
{
DPRINT("In Reduce:factor->ID\n");
factorStmt* p = new factorStmt;
p->Key.kind = factorStmt::factorKIND::idfactorKind;
p->Key.u.idstr = D(1)->tokenstr;
DD = p;
};
factor->NUMBER
{
DPRINT("In Reduce:factor->NUMBER\n");
factorStmt*p = new factorStmt;
p->Key.kind = factorStmt::factorKIND::numfactorKind;
p->value = atoi(D(1)->tokenstr);
DD = p;
};
生成AnsyTable.cpp文件后,需要自行添加gettoken函数及其它驱动函数。具体实现参见附录I