我正在使用 Jison 开发一种语言,我遇到的一个问题是运算符优先级。我希望比较运算符成为第一个要评估的运算符,例如,1 + 2 < 3 - 3 * 4
将变为(1+2) < (3 - (3 * 4))
我现在的规则:
expression
: expression OP_COMPARISON expression
{ $$ = { type: "Comparison", operator: $2, args: [$1, $3], location: locationFromObject(@$) } }
| literal
{ $$ = $literal; }
| NAME
{ $$ = { type: "Variable", variable: $1, location: locationFromObject(@$) }; }
| field
{ $$ = { type: "Field", field: $field, location: locationFromObject(@$) }; }
| '(' ':' typename ')' expression
{ $$ = { type: "Cast", cast_type: $typename, expression: $expression, location: locationFromObject(@$) }; }
| function_call
{ $$ = $function_call; $$.type = "FunctionCall"; }
| method_call
{ $$ = $method_call; $$.type = "FunctionCall"; }
| '(' expression ')'
{ $$ = { type: "Grouped", expression: $expression, location: locationFromObject(@$) }; }
| OP_PREPOSTFIX expression
{ $$ = { type: "Prefix", operator: $1, arg: $expression, location: locationFromObject(@$) }; }
| expression OP_PREPOSTFIX
{ $$ = { type: "Postfix", operator: $2, arg: $expression, location: locationFromObject(@$) }; }
| expression OP_ARITHMETIC expression
{
if($1.type == "Arithmetic"){
$$ = $1;
$$.args.push($3);
$$.operators.push($2);
$$.location = locationFromObject(@$);
}else{
$$ = { type: "Arithmetic", operators: [$2], args: [$1, $3], location: locationFromObject(@$) };
}
}
| expression OP_LOGICAL expression
{
if($1.type == "Logical"){
$$ = $1;
$$.args.push($3);
$$.operators.push($2);
$$.location = locationFromObject(@$);
}else{
$$ = { type: "Logical", operators: [$2], args: [$1, $3], location: locationFromObject(@$) };
}
}
| '!' expression
{ $$ = {type: "LogicalNot", arg: $expression, location: locationFromObject(@$) }; }
;
任何帮助将不胜感激