06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 10: C++ Language Interface 101<br />

10 C++ Language Interface<br />

10.1 C++ Parsers<br />

10.1.1 C++ Bison Interface<br />

The C++ parser LALR(1) skeleton is named ‘lalr1.cc’. To select it, you may either pass the<br />

option ‘--skeleton=lalr1.cc’ to Bison, or include the directive ‘%skeleton "lalr1.cc"’<br />

in the grammar preamble. When run, bison will create several entities in the ‘yy’ namespace.<br />

Use the ‘%name-prefix’ directive to change the namespace name, see Section 3.7.11<br />

[Decl Summary], page 59. The various classes are generated in the following files:<br />

‘position.hh’<br />

‘location.hh’<br />

The definition of the classes position and location, used for location tracking.<br />

See Section 10.1.3 [C++ Location Values], page 102.<br />

‘stack.hh’<br />

‘file.hh’<br />

‘file.cc’<br />

An auxiliary class stack used by the parser.<br />

(Assuming the extension of the input file was ‘.yy’.) The declaration and implementation<br />

of the C++ parser class. The basename and extension of these two<br />

files follow the same rules as with regular C parsers (see Chapter 9 [Invocation],<br />

page 97).<br />

The header is mandatory; you must either pass ‘-d’/‘--defines’ to bison, or<br />

use the ‘%defines’ directive.<br />

All these files are documented using Doxygen; run doxygen for a complete and accurate<br />

documentation.<br />

10.1.2 C++ Semantic Values<br />

The %union directive works as for C, see Section 3.7.4 [The Collection of Value Types],<br />

page 55. In particular it produces a genuine union 1 , which have a few specific features in<br />

C++.<br />

− The type YYSTYPE is defined but its use is discouraged: rather you should refer to the<br />

parser’s encapsulated type yy::parser::semantic_type.<br />

− Non POD (Plain Old Data) types cannot be used. C++ forbids any instance of classes<br />

with constructors in unions: only pointers to such objects are allowed.<br />

Because objects have to be stored via pointers, memory is not reclaimed automatically:<br />

using the %destructor directive is the only means to avoid leaks. See Section 3.7.7 [Freeing<br />

Discarded Symbols], page 57.<br />

1 In the future techniques to allow complex types within pseudo-unions (similar to Boost variants) might<br />

be implemented to alleviate these issues.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!