如何在bison语法中正确格式化i%2 == 0?

时间:2015-03-16 22:52:56

标签: bison

我的语法包含:

expression : NUMBER { $$ = new Int($1); }
       | identifier { $$ = $<expr>1; }
       | INC identifier { $$ = new UnaryOperation($<expr>2, O_ADD_BEFORE); }
       | identifier INC { $$ = new UnaryOperation($<expr>1, O_ADD_AFTER); }
       | DEC identifier { $$ = new UnaryOperation($<expr>2, O_SUB_BEFORE); }
       | identifier DEC { $$ = new UnaryOperation($<expr>1, O_SUB_AFTER); }
       | PLUS expression { $$ = $<expr>1; }
       | MINUS expression { $$ = new UnaryOperation($<expr>2, O_UNARY_MINUS); }
       | LNEG expression { $$ = new UnaryOperation($<expr>2, O_LOG_NEG); }
       | BNEG expression { $$ = new UnaryOperation($<expr>2, O_BIT_NEG); }
       | expression PLUS expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_PLUS); }
       | expression MINUS expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_MINUS); }
       | expression MUL expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_MUL); }
       | expression DIV expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_DIV); }
       | expression SHIFTL expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_SHIFT_LEFT); }
       | expression SHIFTR expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_SHIFT_RIGHT); }
       | expression LESS expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_LESS); }
       | expression MORE expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_MORE); }
       | expression LESS_EQ expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_LESS_EQ); }
       | expression MORE_EQ expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_MORE_EQ); }
       | expression BAND expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_BIT_AND); }
       | expression BOR expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_BIT_OR); }
       | expression BXOR expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_BIT_XOR); }
       | expression EQ expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_EQUALS); }
       | expression NEQ expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_NEQUALS); }
       | expression OR expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_LOG_OR); }
       | expression AND expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_LOG_AND); }
       | expression MOD expression { $$ = new BinaryOperation($<expr>1, $<expr>3, O_MOD); }
       | LEFT_BRACE set_expr RIGHT_BRACE { $$ = $<expr>1; }
       | LEFT_BRACE identifier SET_MUL expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_MUL); }
       | LEFT_BRACE identifier SET_DIV expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_DIV); }
       | LEFT_BRACE identifier SET_MOD expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_MOD); }
       | LEFT_BRACE identifier SET_ADD expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_ADD); }
       | LEFT_BRACE identifier SET_SUB expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_SUB); }
       | LEFT_BRACE identifier SET_SHIFT_L expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_SHIFT_L); }
       | LEFT_BRACE identifier SET_SHIFT_R expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_SHIFT_R); }
       | LEFT_BRACE identifier SET_AND expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_AND); }
       | LEFT_BRACE identifier SET_XOR expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_XOR); }
       | LEFT_BRACE identifier SET_OR expression RIGHT_BRACE { $$ = new BinaryOperation($<expr>2, $<expr>4, O_SET_OR); }
       | LEFT_BRACE expression RIGHT_BRACE { $$ = $2; }
       ;

然而,当我将树编译成字节码时,表达式

i % 2 == 0

将以此操作顺序结束:

PUSH i
PUSH 2
PUSH 0
EQ
MOD

这是不正确的。如何强制解析器的树首先进行操作i%2然后执行i%2 == 0?

1 个答案:

答案 0 :(得分:2)

您应该向野牛宣布MOD的优先级高于EQ

%left EQ
%left MOD

这必须在您的代币申报后完成。