Next: Java Action Features, Previous: Java Parser Interface, Up: Java Parsers
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.
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".
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
.
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".