Next: , Previous: , Up: D Parsers   [Contents][Index]


10.2.4 D Parser Interface

The name of the generated parser class defaults to YYParser. The YY prefix may be changed using the ‘%define api.prefix’. Alternatively, use ‘%define api.parser.class {name}’ to give a custom name to the class. The interface of this class is detailed below.

By default, the parser class has public visibility. To add modifiers to the parser class, %define api.parser.public, api.parser.abstract and/or api.parser.final.

The superclass and the implemented interfaces of the parser class can be specified with the ‘%define api.parser.extends’ and ‘%define api.parser.implements’ directives.

The parser class defines an interface, Lexer (see D Scanner Interface). Other than this interface and the members described in the interface below, all the other members and fields are preceded with a yy or YY prefix to avoid clashes with user code.

The parser class can be extended using the %parse-param directive. Each occurrence of the directive will add a by default public field to the parser class, and an argument to its constructor, which initializes them automatically.

Constructor on YYParser: this(lex_param, …, parse_param, …)

Build a new parser object with embedded ‘%code lexer’. There are no parameters, unless %params and/or %parse-params and/or %lex-params are used.

Constructor on YYParser: this(Lexer lexer, parse_param, …)

Build a new parser object using the specified scanner. There are no additional parameters unless %params and/or %parse-params are used.

Method on YYParser: boolean parse()

Run the syntactic analysis, and return true on success, false otherwise.

Method on YYParser: boolean getErrorVerbose()
Method on YYParser: void setErrorVerbose(boolean verbose)

Get or set the option to produce verbose error messages. These are only available with ‘%define parse.error detailed’, which also turns on verbose error messages.

Method on YYParser: void yyerror(string msg)
Method on YYParser: void yyerror(Location loc, string msg)

Print an error message using the yyerror method of the scanner instance in use. The Location and Position parameters are available only if location tracking is active.

Method on YYParser: boolean recovering()

During the syntactic analysis, return true if recovering from a syntax error. See Error Recovery.

Method on YYParser: File getDebugStream()
Method on YYParser: void setDebugStream(File o)

Get or set the stream used for tracing the parsing. It defaults to stderr.

Method on YYParser: int getDebugLevel()
Method on YYParser: void setDebugLevel(int l)

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

Constant of YYParser: string bisonVersion
Constant of YYParser: string bisonSkeleton

Identify the Bison version and skeleton used to generate this parser.

The internationalization in D is very similar to the one in C. The D parser uses dgettext for translating Bison messages.

To enable internationalization, compile using ‘-version ENABLE_NLS -version YYENABLE_NLS’ and import bindtextdomain and textdomain from C:

extern(C) char* bindtextdomain(const char* domainname, const char* dirname);
extern(C) char* textdomain(const char* domainname);

The main function should load the translation catalogs, similarly to the c/bistromathic example:

int main()
{
  import core.stdc.locale;

  // Set up internationalization.
  setlocale(LC_ALL, "");
  // Use Bison's standard translation catalog for error messages
  // (the generated messages).
  bindtextdomain("bison-runtime", BISON_LOCALEDIR);
  // For the translation catalog of your own project, use the
  // name of your project.
  bindtextdomain("bison", LOCALEDIR);
  textdomain("bison");

  // usual main content
  ...
}

For user message translations, the user must implement the ‘string _(const char* msg)’ function. It is recommended to use gettext:

%code imports {
  static if (!is(typeof(_)))
  {
    version(ENABLE_NLS)
    {
      extern(C) char* gettext(const char*);
      string _(const char* s)
      {
        return to!string(gettext(s));
      }
    }
  }
  static if (!is(typeof(_)))
  {
    pragma(inline, true)
    string _(string msg) { return msg; }
  }
}

Next: D Parser Context Interface, Previous: D Location Values, Up: D Parsers   [Contents][Index]