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


10.2.5 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 contain constants for all user-defined token names and the predefined EOF 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.

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 is active.

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.

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


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