Next: , Previous: , Up: C++ Parsers   [Contents][Index]

10.1.3 C++ Parser Interface

The output files file.hh and declare and define the parser class in the namespace yy. The class name defaults to parser, but may be changed using ‘%define api.parser.class {name}’. The interface of this class is detailed below. It can be extended using the %parse-param feature: its semantics is slightly changed since it describes an additional member of the parser class, and an additional argument for its constructor.

Type of parser: token

A structure that contains (only) the token_kind_type enumeration, which defines the tokens. To refer to the token FOO, use yy::parser::token::FOO. The scanner can use ‘typedef yy::parser::token token;’ to “import” the token enumeration (see Calc++ Scanner).

Type of parser: token_kind_type

An enumeration of the token kinds. Its enumerators are forged from the token names, with a possible token prefix (see api.token.prefix):

/// Token kinds.
struct token
  enum token_kind_type
    YYEMPTY = -2,              // No token.
    YYEOF = 0,                 // "end of file"
    YYerror = 256,             // error
    YYUNDEF = 257,             // "invalid token"
    PLUS = 258,                // "+"
    MINUS = 259,               // "-"
    VAR = 271,                 // "variable"
    NEG = 272                  // NEG

/// Token kind, as returned by yylex.
typedef token::token_kind_type token_kind_type;
Type of parser: value_type

The types for semantic values. See C++ Semantic Values.

Type of parser: location_type

The type of locations, if location tracking is enabled. See C++ Location Values.

Type of parser: syntax_error

This class derives from std::runtime_error. Throw instances of it from the scanner or from the actions to raise parse errors. This is equivalent with first invoking error to report the location and message of the syntax error, and then to invoke YYERROR to enter the error-recovery mode. But contrary to YYERROR which can only be invoked from user actions (i.e., written in the action itself), the exception can be thrown from functions invoked from the user action.

Constructor on parser: parser ()
Constructor on parser: parser (type1 arg1, ...)

Build a new parser object. There are no arguments, unless ‘%parse-param {type1 arg1}’ was used.

Constructor on syntax_error: syntax_error (const location_type& l, const std::string& m)
Constructor on syntax_error: syntax_error (const std::string& m)

Instantiate a syntax-error exception.

Method on parser: int operator() ()
Method on parser: int parse ()

Run the syntactic analysis, and return 0 on success, 1 otherwise. Both routines are equivalent, operator() being more C++ish.

The whole function is wrapped in a try/catch block, so that when an exception is thrown, the %destructors are called to release the lookahead symbol, and the symbols pushed on the stack.

Exception related code in the generated parser is protected by CPP guards (#if) and disabled when exceptions are not supported (i.e., passing -fno-exceptions to the C++ compiler).

Method on parser: std::ostream& debug_stream ()
Method on parser: void set_debug_stream (std::ostream& o)

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

Method on parser: debug_level_type debug_level ()
Method on parser: void set_debug_level (debug_level_type l)

Get or set the tracing level (an integral). Currently its value is either 0, no trace, or nonzero, full tracing.

Method on parser: void error (const location_type& l, const std::string& m)
Method on parser: void error (const std::string& m)

The definition for this member function must be supplied by the user: the parser uses it to report a parser error occurring at l, described by m. If location tracking is not enabled, the second signature is used.

Next: C++ Semantic Values, Previous: C++ Bison Interface, Up: C++ Parsers   [Contents][Index]