Next: , Up: Tracking Locations   [Contents][Index]


3.5.1 Data Type of Locations

Defining a data type for locations is much simpler than for semantic values, since all tokens and groupings always use the same type. The location type is specified using ‘%define api.location.type’:

%define api.location.type {location_t}

This defines, in the C generated code, the YYLTYPE type name. When YYLTYPE is not defined, Bison uses a default structure type with four members:

typedef struct YYLTYPE
{
  int first_line;
  int first_column;
  int last_line;
  int last_column;
} YYLTYPE;

In C, you may also specify the type of locations by defining a macro called YYLTYPE, just as you can specify the semantic value type by defining a YYSTYPE macro (see Data Types of Semantic Values). However, rather than using macros, we recommend the api.value.type and api.location.type %define variables.

Default locations represent a range in the source file(s), but this is not a requirement. It could be a single point or just a line number, or even more complex structures.

When the default location type is used, Bison initializes all these fields to 1 for yylloc at the beginning of the parsing. To initialize yylloc with a custom location type (or to chose a different initialization), use the %initial-action directive. See Performing Actions before Parsing.