Debian Codegraph @

documentation/manual/algorithm/condep.yo

raw
1 Often the precedence of an operator depends on the context. This sounds
2 outlandish at first, but it is really very common. For example, a minus sign
3 typically has a very high precedence as a unary operator, and a somewhat lower
4 precedence (lower than multiplication) as a binary operator.
5
6 The b() precedence directives, %left, %right and %nonassoc, can only be used
7 once for a given token; so a token has only one precedence declared in this
8 way. For context-dependent precedence, you need to use an additional
9 mechanism: the %prec modifier for rules.
10
11 The %prec modifier declares the precedence of a particular (non-empty) rule by
12 specifying a terminal symbol whose precedence should be used for that
13 rule. It's not necessary for that symbol to appear otherwise in the rule. The
14 modifier's syntax is:
15 verb(%prec terminal-symbol)
16
17 and it is written after the components of the rule. Its effect is to assign
18 the rule the precedence of terminal-symbol, overriding the precedence that
19 would be deduced for it in the ordinary way. The altered rule precedence then
20 affects how conflicts involving that rule are resolved (see section Operator
21 Precedence).
22
23 Here is how %prec solves the problem of unary minus. First, declare a
24 precedence for a fictitious terminal symbol named UMINUS. There are no tokens
25 of this type, but the symbol serves to stand for its precedence:
26 verb(
27 ...
28 %left '+' '-'
29 %left '*'
30 %left UMINUS
31 )
32
33 Now the precedence of UMINUS can be used in specific rules:
34 verb(
35 exp:
36 ...
37 | exp '-' exp
38 ...
39 | '-' exp %prec UMINUS
40 )