一个复杂一点的例子

 一个复杂一点的例子

下面是一个复杂一点的支持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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值