Next: , Previous: , Up: C++ Location Values   [Contents][Index]


10.1.5.3 Exposing the Location Classes

When both %header and %locations are enabled, Bison generates an additional file: location.hh. If you don’t use locations outside of the parser, you may avoid its creation with ‘%define api.location.file none’.

However this file is useful if, for instance, your parser builds an abstract syntax tree decorated with locations: you may use Bison’s location type independently of Bison’s parser. You may name the file differently, e.g., ‘%define api.location.file "include/ast/location.hh"’: this name can have directory components, or even be absolute. The way the location file is included is controlled by api.location.include.

This way it is possible to have several parsers share the same location file.

For instance, in src/foo/parser.yy, generate the include/ast/loc.hh file:

// src/foo/parser.yy
%locations
%define api.namespace {foo}
%define api.location.file "include/ast/loc.hh"
%define api.location.include {<ast/loc.hh>}

and use it in src/bar/parser.yy:

// src/bar/parser.yy
%locations
%define api.namespace {bar}
%code requires {#include <ast/loc.hh>}
%define api.location.type {bar::location}

Absolute file names are supported; it is safe in your Makefile to pass the flag -Dapi.location.file='"$(top_srcdir)/include/ast/loc.hh"' to bison for src/foo/parser.yy. The generated file will not have references to this absolute path, thanks to ‘%define api.location.include {<ast/loc.hh>}’. Adding ‘-I $(top_srcdir)/include’ to your CPPFLAGS will suffice for the compiler to find ast/loc.hh.