2017-09-18 54 views
-4

我对下面的代码有问题,我处理了不相关的代码。嵌套开关语句

我已经调试了代码,发现问题是我无法进入开关(e-> u.op.oper)

在函数interpExp()中,存在嵌套的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("hi\n"); 
      printf("%d\n", e->u.num); 
      return (struct IntAndTable) {e->u.num, t}; 
     case A_opExp : 
      printf("good\n"); 
      switch (e->u.op.oper) { 
       printf("wowowowowow\n"); 
       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; 
}; 
+1

您的内部'switch'在所需的printf之前没有'case'。 – Yunnosch

+0

@AnttiHaapala对不起,我没有检查这个问题的标题,我刚才修改了它 –

+0

@Ynnosch不切换语句运行代码以外的情况? –

回答

2

你的内switch具有所需printf, 这导致break从未被执行之前的代码之前没有case

贷安蒂Haaapala为成C标准的有用链接在他的评论,
在exmple (或它的解释),明确指出,像你这样的代码“不能达到”。