Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

No Format
e : parse_expr[0] ; // rewrite e to this rule, which invokes the precedence engine

/** "Precedence climbing" engine
parse_expr[int p]
   :   (primary->primary)
       (   {prec[input.LA(1)]>=p}?=>     bop r=e[nextp(p)] -> ^(bop $e $r)
       |   {postprec[input.LA(1)]>=p}?=> suffix[$e.tree]   -> {$suffix.tree}
       )*
   ;

where rule suffix is described below (basically we remove e references from the left edge.

If you specify a rule with option parser=precedence, ANTLR does the new magical thing:

...

Within the special rule, ANTLR looks for binary operators and the suffix operators. It also looks one level deep into rules referenced from the special rule. In this case, ANTLR identifies '*' '+' '-' '=' as binary operators and '.' '[' '(' as suffix operators. All other alternatives are lumped together into a new rule:

No Format

parse_expr_alts
    :   primary
    |   '(' type ')' e // cast
    ;

The suffix rule has left recursive references to e sell those must be removed. The parse_expr rule invokes a suffix after having matched e already:

No Format

suffix
    :  '[' e ']'
    |  '(' e (',' e)* ')'
    ;