Previous: Actions and Locations, Up: Tracking Locations [Contents][Index]
Actually, actions are not the best place to compute locations. Since
locations are much more general than semantic values, there is room in
the output parser to redefine the default action to take for each
rule. The YYLLOC_DEFAULT
macro is invoked each time a rule is
matched, before the associated action is run. It is also invoked
while processing a syntax error, to compute the error’s location.
Before reporting an unresolvable syntactic ambiguity, a GLR
parser invokes YYLLOC_DEFAULT
recursively to compute the location
of that ambiguity.
Most of the time, this macro is general enough to suppress location dedicated code from semantic actions.
The YYLLOC_DEFAULT
macro takes three parameters. The first one is
the location of the grouping (the result of the computation). When a
rule is matched, the second parameter identifies locations of
all right hand side elements of the rule being matched, and the third
parameter is the size of the rule’s right hand side.
When a GLR parser reports an ambiguity, which of multiple candidate
right hand sides it passes to YYLLOC_DEFAULT
is undefined.
When processing a syntax error, the second parameter identifies locations
of the symbols that were discarded during error processing, and the third
parameter is the number of discarded symbols.
By default, YYLLOC_DEFAULT
is defined this way:
# define YYLLOC_DEFAULT(Cur, Rhs, N) \ do \ if (N) \ { \ (Cur).first_line = YYRHSLOC(Rhs, 1).first_line; \ (Cur).first_column = YYRHSLOC(Rhs, 1).first_column; \ (Cur).last_line = YYRHSLOC(Rhs, N).last_line; \ (Cur).last_column = YYRHSLOC(Rhs, N).last_column; \ } \ else \ { \ (Cur).first_line = (Cur).last_line = \ YYRHSLOC(Rhs, 0).last_line; \ (Cur).first_column = (Cur).last_column = \ YYRHSLOC(Rhs, 0).last_column; \ } \ while (0)
where YYRHSLOC (rhs, k)
is the location of the kth symbol
in rhs when k is positive, and the location of the symbol
just before the reduction when k and n are both zero.
When defining YYLLOC_DEFAULT
, you should consider that:
YYLLOC_DEFAULT
.
Previous: Actions and Locations, Up: Tracking Locations [Contents][Index]