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


10.3.6 Java Scanner Interface

There are two possible ways to interface a Bison-generated Java parser with a scanner: the scanner may be defined by %code lexer, or defined elsewhere. In either case, the scanner has to implement the Lexer inner interface of the parser class. This interface also contains constants for all user-defined token names and the predefined YYEOF token.

In the first case, the body of the scanner class is placed in %code lexer blocks. If you want to pass parameters from the parser constructor to the scanner constructor, specify them with %lex-param; they are passed before %parse-params to the constructor.

In the second case, the scanner has to implement the Lexer interface, which is defined within the parser class (e.g., YYParser.Lexer). The constructor of the parser object will then accept an object implementing the interface; %lex-param is not used in this case.

In both cases, the scanner has to implement the following methods.

Method on Lexer: void yyerror (Location loc, String msg)

This method is defined by the user to emit an error message. The first parameter is omitted if location tracking is not active. Its type can be changed using %define api.location.type {class-name}.

Method on Lexer: int yylex ()

Return the next token. Its type is the return value, its semantic value and location are saved and returned by the their methods in the interface. Not needed for push-only parsers.

Use ‘%define lex_throws’ to specify any uncaught exceptions. Default is java.io.IOException.

Method on Lexer: Position getStartPos ()
Method on Lexer: Position getEndPos ()

Return respectively the first position of the last token that yylex returned, and the first position beyond it. These methods are not needed unless location tracking and pull parsing are active.

They should return new objects for each call, to avoid that all the symbol share the same Position boundaries.

The return type can be changed using %define api.position.type {class-name}.

Method on Lexer: Object getLVal ()

Return the semantic value of the last token that yylex returned. Not needed for push-only parsers.

The return type can be changed using ‘%define api.value.type {class-name}’.

Method on Lexer: void reportSyntaxError (YYParser.Context ctx)

If you invoke ‘%define parse.error custom’ (see The Bison Declarations Section), then the parser no longer passes syntax error messages to yyerror, rather it delegates that task to the user by calling the reportSyntaxError function.

Whether it uses yyerror is up to the user.

Here is an example of a reporting function (see Java Parser Context Interface).

public void reportSyntaxError(YYParser.Context ctx) {
  System.err.print(ctx.getLocation() + ": syntax error");
  // Report the expected tokens.
  {
    final int TOKENMAX = 5;
    YYParser.SymbolKind[] arg = new YYParser.SymbolKind[TOKENMAX];
    int n = ctx.getExpectedTokens(arg, TOKENMAX);
    for (int i = 0; i < n; ++i)
      System.err.print((i == 0 ? ": expected " : " or ")
                       + arg[i].getName());
  }
  // Report the unexpected token which triggered the error.
  {
    YYParser.SymbolKind lookahead = ctx.getToken();
    if (lookahead != null)
      System.err.print(" before " + lookahead.getName());
  }
  System.err.println("");
}

This implementation is inappropriate for internationalization, see the c/bistromathic example for a better alternative.


Next: Special Features for Use in Java Actions, Previous: Java Parser Context Interface, Up: Java Parsers   [Contents][Index]