I'm trying to make a bnf and have such a problem with priority:
When I describe rules for prefix or unary not expressions like that
unary_not_expression ::= ("~"|"!") expression
psi tree for expression a = !b^c looks like it were a = !(b^c)
Changing order in rule
expression ::= xor_expression | unary_not_expression | … | primary_expression
doesn't help. By the way, there is the same problem here https://github.com/JetBrains/Grammar-Kit/blob/master/HOWTO.md in part 2.4 Compact expression parsing with priorities. Although there is such list of priorities for an example in this tutorial
// Expression root: expr
// Operator priority table:
// 0: BINARY(assign_expr)
// 1: BINARY(plus_expr) BINARY(minus_expr)
// 2: BINARY(mul_expr) BINARY(div_expr)
// 3: PREFIX(unary_plus_expr) PREFIX(unary_min_expr)
// 4: N_ARY(exp_expr)
// 5: POSTFIX(ref_expr)
// 6: ATOM(simple_ref_expr) ATOM(literal_expr) PREFIX(paren_expr)
in fact in makes psi tree like expression were a = !(b^c).
My solution was to write a rule like that
unary_not_expression ::= ("~"|"!") primary_expression
where rule for primary_expression is
primary_expression ::= parenthesized_expression | identifier | literal
But I don't like this solution, because primary_expression is listed in rule for expression and the whole bnf doesn't look good.
Is there any other solution for that problem?