06.01.2015 Views

Manual

Manual

Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

10.1.4 C++ Parser Interface<br />

The output files ‘output.hh’ and ‘output.cc’ declare and define the parser class in the<br />

namespace yy. The class name defaults to parser, but may be changed using ‘%define<br />

"parser_class_name" "name"’. The interface of this class is detailed below. It can be<br />

extended using the %parse-param feature: its semantics is slightly changed since it describes<br />

an additional member of the parser class, and an additional argument for its constructor.<br />

semantic_value_type<br />

location_value_type<br />

The types for semantics value and locations.<br />

[Type of parser]<br />

[Type of parser]<br />

parser (type1 arg1, ...)<br />

[Method on parser]<br />

Build a new parser object. There are no arguments by default, unless ‘%parse-param<br />

{type1 arg1}’ was used.<br />

int parse ()<br />

[Method on parser]<br />

Run the syntactic analysis, and return 0 on success, 1 otherwise.<br />

std::ostream& debug_stream ()<br />

[Method on parser]<br />

void set_debug_stream (std::ostream& o )<br />

[Method on parser]<br />

Get or set the stream used for tracing the parsing. It defaults to std::cerr.<br />

debug_level_type debug_level ()<br />

[Method on parser]<br />

void set_debug_level (debug level l )<br />

[Method on parser]<br />

Get or set the tracing level. Currently its value is either 0, no trace, or nonzero, full<br />

tracing.<br />

void error (const location type& l, const std::string& m ) [Method on parser]<br />

The definition for this member function must be supplied by the user: the parser uses<br />

it to report a parser error occurring at l, described by m.<br />

10.1.5 C++ Scanner Interface<br />

The parser invokes the scanner by calling yylex. Contrary to C parsers, C++ parsers are<br />

always pure: there is no point in using the %pure-parser directive. Therefore the interface<br />

is as follows.<br />

int yylex (semantic value type& yylval, location type& [Method on parser]<br />

yylloc, type1 arg1, ...)<br />

Return the next token. Its type is the return value, its semantic value and location<br />

being yylval and yylloc. Invocations of ‘%lex-param {type1 arg1}’ yield additional<br />

arguments.<br />

10.2 A Complete C++ Example<br />

This section demonstrates the use of a C++ parser with a simple but complete example.<br />

This example should be available on your system, ready to compile, in the directory ../bison/examples/calc++.<br />

It focuses on the use of Bison, therefore the design of the various<br />

C++ classes is very naive: no accessors, no encapsulation of members etc. We will use a<br />

Lex scanner, and more precisely, a Flex scanner, to demonstrate the various interaction. A<br />

hand written scanner is actually easier to interface with.

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

Saved successfully!

Ooh no, something went wrong!