一个复杂一点的例子

 一个复杂一点的例子

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

当然,让我们来看一个复杂例子,这个例子中我们将使用`switch`语句处理一些用户输入的菜单选项,比如在一个简单的命令行计算器程序中。 ```c #include <stdio.h> #include <string.h> int add(int a, int b) { return a + b; } int subtract(int a, int b) { return a - b; } int multiply(int a, int b) { return a * b; } int divide(int a, int b) { if (b != 0) return a / b; else return "Error: Division by zero"; } // 分母为零的情况处理 int main() { char command[10]; int num1, num2; printf("欢迎来到简单计算器!\n" "请输入操作:\n" "1. 加法 (+)\n" "2. 减法 (-)\n" "3. 乘法 (*)\n" "4. 除法 (/)\n" "\nEnter your choice: "); scanf("%s", command); // 获取用户输入 switch (command[0]) { case '1': printf("请输入两个数字: "); scanf("%d %d", &num1, &num2); printf("结果: %d\n", add(num1, num2)); break; case '2': printf("请输入两个数字: "); scanf("%d %d", &num1, &num2); printf("结果: %d\n", subtract(num1, num2)); break; case '3': printf("请输入两个数字: "); scanf("%d %d", &num1, &num2); printf("结果: %d\n", multiply(num1, num2)); break; case '4': printf("请输入两个数字: "); scanf("%d %d", &num1, &num2); printf("%s\n", divide(num1, num2)); // 输出函数返回的结果 break; default: printf("无效的选择! 请输入1到4之间的数字。\n"); } return 0; } ``` 在这个例子中,用户可以选择加、减、乘、除四种运算,并输入对应的操作数。`switch`会根据用户的输入执行相应的计算方法。如果输入不在预设的范围内,就会显示一条错误信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值