[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
3.7.3 Operator Precedence
Use the %left
, %right
, %nonassoc
, or
%precedence
declaration to
declare a token and specify its precedence and associativity, all at
once. These are called precedence declarations.
See section Operator Precedence, for general information on
operator precedence.
The syntax of a precedence declaration is nearly the same as that of
%token
: either
%left symbols…
or
%left <type> symbols…
And indeed any of these declarations serves the purposes of %token
.
But in addition, they specify the associativity and relative precedence for
all the symbols:
-
The associativity of an operator op determines how repeated uses
of the operator nest: whether ‘x op y op
z’ is parsed by grouping x with y first or by
grouping y with z first.
%left
specifies left-associativity (grouping x with y first) and%right
specifies right-associativity (grouping y with z first).%nonassoc
specifies no associativity, which means that ‘x op y op z’ is considered a syntax error.%precedence
gives only precedence to the symbols, and defines no associativity at all. Use this to define precedence only, and leave any potential conflict due to associativity enabled. - The precedence of an operator determines how it nests with other operators. All the tokens declared in a single precedence declaration have equal precedence and nest together according to their associativity. When two tokens declared in different precedence declarations associate, the one declared later has the higher precedence and is grouped first.
For backward compatibility, there is a confusing difference between the
argument lists of %token
and precedence declarations.
Only a %token
can associate a literal string with a token type name.
A precedence declaration always interprets a literal string as a reference to a
separate token.
For example:
%left OR "<=" // Does not declare an alias. %left OR 134 "<=" 135 // Declares 134 for OR and 135 for "<=".
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on December 1, 2013 using texi2html 5.0.