坚持在yacc中打印令牌

时间:2016-10-19 08:28:51

标签: assembly compiler-construction yacc lex systems-programming

我正在编写一个简单的编译器来执行MOV和ADD指令。这是我的yacc代码的一部分......

{ "k1" : "v1" }

这里是类型

的指令
            %{
              char *name
              extern char *yytext
            %}

        %token MOV ADD    /*name of instructions returned from lexer*/
        %token REG ACC OP  /*REG implies Register names such as R0-R7, ACC implies name of accumulator such as A-F, OP implies operand such as 0-9*/
        %type <val> mov_inst add_instr
        %type <string> reg REG
        %type <index>  acc ACC

       %%
        program: instr  {printf("The value of %s is:%d\n", name, $1);}
        instr: add_instr;
        add_instr: ADD acc OP  {$$ = mem[$2] = mem[$2] + $3;}
           | ADD acc reg  {$$ = mem[$2] = mem[$2] + regmem[regIdx];}
           ;
       acc: ACC {$$ = mem[$1]; name = strdup(yytext);}
       reg: REG {$$ = strdup(yytext); name = $1;}
          ;

类似于

               ADD B 5; result should be printed as  
           "The value of B is 5" which is working fine. 

但我得到的是

              ADD B R1 -> it should be 
              "The value of B is 8" (say R1=3);

它打印的名称R1而不是B.我不知道为什么!
我需要在结果

中使用R取代R1
               "The value of R1 is 8" 

任何建议!!!

0 个答案:

没有答案