www.delorie.com/gnu/docs/bison/bison_57.html   search  
 
Buy the book!


Bison 1.875

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.6.3 Default Action for Locations

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.

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 is an array holding 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 processing a syntax error, the second parameter is an array holding 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 for simple LALR(1) parsers:

 
#define YYLLOC_DEFAULT(Current, Rhs, N)          \
  Current.first_line   = Rhs[1].first_line;      \
  Current.first_column = Rhs[1].first_column;    \
  Current.last_line    = Rhs[N].last_line;       \
  Current.last_column  = Rhs[N].last_column;

and like this for GLR parsers:

 
#define YYLLOC_DEFAULT(Current, Rhs, N)          \
  Current.first_line   = YYRHSLOC(Rhs,1).first_line;      \
  Current.first_column = YYRHSLOC(Rhs,1).first_column;    \
  Current.last_line    = YYRHSLOC(Rhs,N).last_line;       \
  Current.last_column  = YYRHSLOC(Rhs,N).last_column;

When defining YYLLOC_DEFAULT, you should consider that:


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003