Next: , Previous: Java Location Values, Up: Java Parsers


10.2.4 Java Parser Interface

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

By default, the parser class has package visibility. A declaration ‘%define public’ will change to public visibility. Remember that, according to the Java language specification, the name of the .java file should match the name of the class in this case. Similarly, you can use abstract, final and strictfp with the %define declaration to add other modifiers to the parser class. A single ‘%define annotations {annotations}’ directive can be used to add any number of annotations to the parser class.

The Java package name of the parser class can be specified using the ‘%define package’ directive. The superclass and the implemented interfaces of the parser class can be specified with the %define extends and ‘%define implements’ directives.

The parser class defines an inner class, Location, that is used for location tracking (see Java Location Values), and a inner interface, Lexer (see Java Scanner Interface). Other than these inner class/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 protected final field to the parser class, and an argument to its constructor, which initialize them automatically.

— Constructor on YYParser: YYParser (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.

Use %code init for code added to the start of the constructor body. This is especially useful to initialize superclasses. Use ‘%define init_throws’ to specify any uncaught exceptions.

— Constructor on YYParser: YYParser (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.

If the scanner is defined by %code lexer, this constructor is declared protected and is called automatically with a scanner created with the correct %params and/or %lex-params.

Use %code init for code added to the start of the constructor body. This is especially useful to initialize superclasses. Use ‘%define init_throws’ to specify any uncaught exceptions.

— 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 verbose’, which also turns on verbose error messages.

— Method on YYParser: void yyerror (String msg)
— Method on YYParser: void yyerror (Position pos, 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: java.io.PrintStream getDebugStream ()
— Method on YYParser: void setDebugStream (java.io.printStream o)

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

— 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.