[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [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.
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-param
s 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 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 ther methods in the interface.
Use
%define lex_throws
to specify any uncaught exceptions. Default isjava.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 position_type "class-name".
- Method on Lexer: Object getLVal ()
Return the semantical value of the last token that yylex returned.
The return type can be changed using
%define stype "class-name".
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |