如何在野牛语法中正确格式化 i % 2 == 0?
How to correctly format i % 2 == 0 in bison grammar?
我的语法包含:
expression : NUMBER { $$ = new Int(); }
| 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 { $$ = ; }
;
然而当我将我的树编译成字节码时,表达式
i % 2 == 0
将以这个操作顺序结束:
PUSH i
PUSH 2
PUSH 0
EQ
MOD
这是不正确的。我如何强制解析器树先进行操作 i % 2 然后 i % 2 == 0?
你应该向 bison 声明 MOD
的优先级高于 EQ
:
%left EQ
%left MOD
这必须在您的令牌声明之后完成。
我的语法包含:
expression : NUMBER { $$ = new Int(); }
| 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 { $$ = ; }
;
然而当我将我的树编译成字节码时,表达式
i % 2 == 0
将以这个操作顺序结束:
PUSH i
PUSH 2
PUSH 0
EQ
MOD
这是不正确的。我如何强制解析器树先进行操作 i % 2 然后 i % 2 == 0?
你应该向 bison 声明 MOD
的优先级高于 EQ
:
%left EQ
%left MOD
这必须在您的令牌声明之后完成。