我对下面的代码有问题,我处理了无关的代码。
我已经调试了代码,发现问题是我不能进入 switch(e-> u.op.oper)。
在函数Interpext()中,存在嵌套的开关语句。在那里,我可以看到字符串的输出,"好" 。因此,执行显然到了那里。
但是,我看不到" WOW" 输出。
因为"好" 已经打印了,所以我认为也应该打印" wow" 。
但是我看不到紧随的" wow" 输出,应该发生 从第二个嵌套语句 switch(e-> u.op.oper)。
typedef struct A_exp_* A_exp;
typedef enum {A_plus, A_minus, A_times, A_div} A_binop;
struct IntAndTable interpExp(A_exp e, Table_ t) {
Table_ tempT;
switch (e->kind) {
case A_idExp :
return (struct IntAndTable) {lookup(t, e->u.id), t};
case A_numExp :
printf("hin");
printf("%dn", e->u.num);
return (struct IntAndTable) {e->u.num, t};
case A_opExp :
printf("goodn");
switch (e->u.op.oper) {
printf("wowowowowown");
struct IntAndTable left = interpExp(e->u.op.left, t);
struct IntAndTable right = interpExp(e->u.op.right, left.t);
case A_plus :
return (struct IntAndTable) {left.i + right.i, right.t};
case A_minus :
return (struct IntAndTable) {left.i - right.i, right.t};
case A_times :
return (struct IntAndTable) {left.i * right.i, right.t};
case A_div :
return (struct IntAndTable) {left.i / right.i, right.t};
}
case A_eseqExp :
tempT = interpStm(e->u.eseq.stm, t);
return interpExp(e->u.eseq.exp, tempT);
}
}
struct A_exp_ {
enum {A_idExp, A_numExp, A_opExp, A_eseqExp} kind;
union {
string id;
int num;
struct {
A_exp left;
A_binop oper;
A_exp right;
} op;
struct {
A_stm stm;
A_exp exp;
} eseq;
} u;
};
您的内部switch
在所需的printf
之前没有case
,这会导致break
不执行的代码。
将Antti Haaapala归功于他的评论中的有用链接,
在Exmple(或其解释)中,明确指出像您这样的代码"无法达到"。