⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javaccgrm.html

📁 This a JavaCC documentation.
💻 HTML
📖 第 1 页 / 共 4 页
字号:
<HTML><!--Copyright 漏 2002 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,California 95054, U.S.A. All rights reserved.  Sun Microsystems, Inc. hasintellectual property rights relating to technology embodied in the productthat is described in this document. In particular, and without limitation,these intellectual property rights may include one or more of the U.S.patents listed at http://www.sun.com/patents and one or more additionalpatents or pending patent applications in the U.S. and in other countries.U.S. Government Rights - Commercial software. Government users are subjectto the Sun Microsystems, Inc. standard license agreement and applicableprovisions of the FAR and its supplements.  Use is subject to license terms.Sun,  Sun Microsystems,  the Sun logo and  Java are trademarks or registeredtrademarks of Sun Microsystems, Inc. in the U.S. and other countries.  Thisproduct is covered and controlled by U.S. Export Control laws and may besubject to the export or import laws in other countries.  Nuclear, missile,chemical biological weapons or nuclear maritime end uses or end users, whetherdirect or indirect, are strictly prohibited.  Export or reexport to countriessubject to U.S. embargo or to entities identified on U.S. export exclusionlists, including, but not limited to, the denied persons and speciallydesignated nationals lists is strictly prohibited.--><HEAD><title>JavaCC Grammar Files</title><!-- Changed by: Michael Van De Vanter, 14-Jan-2003 --></HEAD><BODY bgcolor="#FFFFFF" ><H1>JavaCC [tm]: Grammar Files</H1>This page contains the complete syntax of Java Compiler Compiler [tm]grammar files with detailed explanations of each construct.<P>Tokens in the grammar files follow the same conventions as for the Java programming language.Hence identifiers, strings, characters, etc. used in the grammars arethe same as Java identifiers, Java strings, Java characters, etc.<P><em>White space</em> in the grammar files also follows the same conventions asfor the Java programming language.  This includes the syntax for comments.  Most comments present inthe grammar files are generated into the generated parser/lexical analyzer.<P>Grammar files are preprocessed for Unicode escapes just as Java filesare (i.e., occurrences of strings such as <code>\uxxxx</code> - where <code>xxxx</code> is a hex value -are converted the the corresponding Unicode character before lexical analysis).<P><em>Exceptions to the above rules:</em>The Java operators "<code>&lt;&lt;</code>", "<code>&gt;&gt;</code>", "<code>&gt;&gt;&gt;</code>", "<code>&lt;&lt;=</code>","<code>&gt;&gt;=</code>", and "<code>&gt;&gt;&gt;=</code>" are left out of JavaCC's input token listin order to allow convenient nested use of token specifications.Finally, the following are the additional reserved words in the Java CompilerCompiler [tm] grammar files.<P><TABLE CELLPADDING="3"><TR><TD ALIGN=LEFT><strong>EOF</strong></TD><TD ALIGN=LEFT><strong><A HREF="#IGNORE_CASE">IGNORE_CASE</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#JAVACODE">JAVACODE</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#LOOKAHEAD">LOOKAHEAD</A></strong></TD></TR><TR><TD ALIGN=LEFT><strong><A HREF="#MORE">MORE</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#options">options</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#PARSER_BEGIN">PARSER_BEGIN</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#PARSER_END">PARSER_END</A></strong></TD></TR><TR><TD ALIGN=LEFT><strong><A HREF="#SKIP">SKIP</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#SPECIAL_TOKEN">SPECIAL_TOKEN</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#TOKEN">TOKEN</A></strong></TD><TD ALIGN=LEFT><strong><A HREF="#TOKEN_MGR_DECLS">TOKEN_MGR_DECLS</A></strong></TD></TR></TABLE><P>Any Java entities used in the grammar rules that follow appear italicizedwith the prefix <EM>java_</EM> (<EM>e.g.</EM>, <EM>java_compilation_unit</EM>).<P><HR><P><A NAME="PARSER_BEGIN"></A><A NAME="PARSER_END"></A><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod1">javacc_input</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE><A HREF="#prod2">javacc_options</A></TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE>"PARSER_BEGIN" "(" &lt;IDENTIFIER&gt; ")"</TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE><EM>java_compilation_unit</EM></TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE>"PARSER_END" "(" &lt;IDENTIFIER&gt; ")"</TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE>( <A HREF="#prod5">production</A> )*</TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE>&lt;EOF&gt;</TD></TR></TABLE><P>The grammar file starts with a list of options (which is optional).This is then followed by a Java compilation unit enclosed between"PARSER_BEGIN(name)" and "PARSER_END(name)".  After this is a listof grammar productions.  <A HREF="#prod2">Options</A> and<A HREF="#prod5">productions</A> are described later.<P>The <EM>name</EM> that follows "PARSER_BEGIN" and "PARSER_END" mustbe the same and this identifies the name of the generated parser.For example, if <EM>name</EM> is "MyParser", then the following filesare generated:<P><STRONG>MyParser.java:</STRONG>The generate parser.<BR><STRONG>MyParserTokenManager.java:</STRONG>The generated token manager (or scanner/lexical analyzer).<BR><STRONG>MyParserConstants.java:</STRONG>A bunch of useful constants.<P>Other files such as "Token.java", "ParseError.java", etc. are alsogenerated.  However, these files contain boilerplate code and arethe same for any grammar and may be reused across grammars.<P>Between the PARSER_BEGIN and PARSER_END constructs is a regularJava compilation unit (a compilation unit in Java lingo is the entirecontents of a Java file).  This may be any arbitraryJava compilation unit so long as it contains a class declarationwhose name is the same as the name of the generated parser ("MyParser"in the above example).  Hence, in general, this part of the grammarfile looks like:<P><PRE>    PARSER_BEGIN(parser_name)    . . .    class parser_name . . . {      . . .    }    . . .    PARSER_END(parser_name)</PRE><P>JavaCC does not perform detailed checks on the compilation unit, soit is possible for a grammar file to pass through JavaCC and generateJava files that produce errors when they are compiled.<P>If the compilation unit includes a package declaration, this isincluded in all the generated files.  If the compilation unit includesimports declarations, this is included in the generated parser andtoken manager files.<P>The generated parser file contains everything in the compilation unitand, in addition, contains the generated parser code that is included atthe end of the parser class.  For the above example, the generatedparser will look like:<P><PRE>    . . .    class parser_name . . . {      . . .      // generated parser is inserted here.    }    . . .</PRE><P>The generated parser includes a public method declaration correspondingto each non-terminal (see <A HREF="#prod9">javacode_production</A> and<A HREF="#prod11">bnf_production</A>) in the grammar file.  Parsing withrespect to a non-terminal is achieved by calling the method correspondingto that non-terminal.  Unlike yacc, there is no single start symbol inJavaCC - one can parse with respect to any non-terminal in the grammar.<P>The generated token manager provides one public method:<P><PRE>    Token getNextToken() throws ParseError;</PRE><P>For more details on how this method may be used, please read<A HREF="apiroutines.html">the description of the Java Compiler CompilerAPI</A>.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod2">javacc_options</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>[ "<A NAME="options">options</A>" "{" ( <A HREF="#prod6">option_binding</A> )* "}" ]</TD></TR></TABLE><P>The options if present, starts with the reserved word "options" followedby a list of one or more option bindings within braces.  Each optionbinding specifies the setting of one option.  The same option may not beset multiple times.<P>Options may be specified either here in the grammar file, or from<A HREF="commandline.html">the command line</A>.  If the option is setfrom <A HREF="commandline.html">the command line</A>, that takes precedence.<P>Option names are not case-sensitive.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod6">option_binding</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>"LOOKAHEAD" "=" <EM>java_integer_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"CHOICE_AMBIGUITY_CHECK" "=" <EM>java_integer_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"OTHER_AMBIGUITY_CHECK" "=" <EM>java_integer_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"STATIC" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"DEBUG_PARSER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"DEBUG_LOOKAHEAD" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"DEBUG_TOKEN_MANAGER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"OPTIMIZE_TOKEN_MANAGER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"ERROR_REPORTING" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"JAVA_UNICODE_ESCAPE" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"UNICODE_INPUT" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"IGNORE_CASE" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"USER_TOKEN_MANAGER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"USER_CHAR_STREAM" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"BUILD_PARSER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"BUILD_TOKEN_MANAGER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"TOKEN_MANAGER_USES_PARSER" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"SANITY_CHECK" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"FORCE_LA_CHECK" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"COMMON_TOKEN_ACTION" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"CACHE_TOKENS" "=" <EM>java_boolean_literal</EM> ";"</TD></TR><TR><TD></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"OUTPUT_DIRECTORY" "=" <EM>java_string_literal</EM> ";"</TD></TR></TABLE><UL><LI><STRONG><A NAME="LOOKAHEAD">LOOKAHEAD</A>:</STRONG>The number of tokens to look ahead before making adecision at a choice point during parsing.  The default value is 1.The smaller this number, the faster the parser.  This number may beoverridden for specific productions within the grammar as describedlater.  See the description of<A HREF="lookahead.html">the lookahead algorithm</A> for completedetails on how lookahead works.<LI><STRONG>CHOICE_AMBIGUITY_CHECK:</STRONG>This is an integer option whose default value is 2.This is the number of tokens considered in checking choices of theform "A | B | ..." for ambiguity.  For example, if there is a commontwo token prefix for both A and B, but no common three token prefix,(assume this option is set to 3) then JavaCC can tell you to use alookahead of 3 for disambiguation purposes.  And if A and B have acommon three token prefix, then JavaCC only tell you that you need tohave a lookahead of 3 <EM>or more</EM>.  Increasing this can give you morecomprehensive ambiguity information at the cost of more processingtime.  For large grammars such as the Java grammar, increasing this numberany further causes the checking to take too much time.<LI><STRONG>OTHER_AMBIGUITY_CHECK:</STRONG>This is an integer option whose default value is 1.This is the number of tokens considered in checking all other kinds ofchoices (i.e., of the forms "(A)*", "(A)+", and "(A)?") for ambiguity.This takes more time to do than the choice checking, and hence thedefault value is set to 1 rather than 2.<LI>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -