📄 javaccgrm.html
字号:
<TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod10">regular_expr_production</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>[ <A HREF="#newprod1">lexical_state_list</A> ]</TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE><A HREF="#prod17">regexpr_kind</A> [ "[" "IGNORE_CASE" "]" ] ":"</TD></TR><TR><TD></TD><TD></TD><TD ALIGN=LEFT VALIGN=BASELINE>"{" <A HREF="#prod18">regexpr_spec</A> ( "|" <A HREF="#prod18">regexpr_spec</A> )* "}"</TD></TR></TABLE><P>A regular expression production is used to define lexical entitiesthat get processed by the generated token manager. A detailed descriptionof how the token manager works is provided in<A HREF="tokenmanager.html">this minitutorial (click here)</A>. Thispage describes the syntactic aspects of specifying lexical entities,while <A HREF="tokenmanager.html">the minitutorial</A> describes howthese syntactic constructs tie in with how the token manager actuallyworks.<P>A regular expression production starts with a specification of thelexical states for which it applies (the<A HREF="#newprod1">lexical state list</A>).There is a standard lexical state called "DEFAULT". If the<A HREF="#newprod1">lexical state list</A> is omitted, the regularexpression production applies to the lexical state "DEFAULT".<P>Following this is a description of what kind of regular expressionproduction this is (<A HREF="#prod17">see below for what this means</A>).<P>After this is an optional "[IGNORE_CASE]". If this is present, theregular expression production is case insensitive - it has the sameeffect as the<A HREF="#prod6">IGNORE_CASE</A>option, except that in this case it applies locally to this regularexpression production.<P>This is then followed by a list of regular expression specificationsthat describe in more detail the lexical entities of this regularexpression production.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod12">token_manager_decls</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>"<A NAME="TOKEN_MGR_DECLS">TOKEN_MGR_DECLS</A>" ":" <EM>java_block</EM></TD></TR></TABLE><P>The token manager declarations starts with the reserved word"TOKEN_MGR_DECLS" followed by a ":" and then a set of Java declarationsand statements (the Java block). These declarations and statements arewritten into the generated token manager and are accessible from within<A HREF="#prod18">lexical actions</A>. See<A HREF="tokenmanager.html">the minitutorial on the token manager</A>for more details.<P>There can only be one token manager declaration in a JavaCC grammar file.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="newprod1">lexical_state_list</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>"<" "*" ">"</TD></TR><TR><TD></TD><TD>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"<" <EM>java_identifier</EM> ( "," <EM>java_identifier</EM> )* ">"</TD></TR></TABLE><P>The lexical state list describes the set of lexical states for whichthe corresponding <A HREF="#prod10">regular expression production</A>applies. If this is written as "<*>", the regular expression productionapplies to all lexical states. Otherwise, it applies to all the lexicalstates in the identifier list within the angular brackets.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod17">regexpr_kind</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>"TOKEN"</TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"SPECIAL_TOKEN"</TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"SKIP"</TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"MORE"</TD></TR></TABLE><P>This specifies the kind of<A HREF="#prod10">regular expression production</A>.There are four kinds:<P><UL><LI><STRONG><A NAME="TOKEN">TOKEN</A></STRONG>:The regular expressions in this regular expression production describe<EM>tokens</EM> in the grammar. The token manager creates a<A HREF="apiroutines.html">Token</A> object for each match of sucha regular expression and returns it to the parser.<P><LI><STRONG><A NAME="SPECIAL_TOKEN">SPECIAL_TOKEN</A></STRONG>:The regular expressions in this regular expression production describe<EM>special tokens</EM>. Special tokens are like tokens, except thatthey do not have significance during parsing - that is the BNF productionsignore them. Special tokens are, however, still passed on to the parserso that parser actions can access them. Special tokens are passedto the parser by linking them to neighboring real tokens using thefield "specialToken" in the <A HREF="apiroutines.html">Token</A>class. Special tokens are useful in the processing of lexical entitiessuch as comments which have no significance to parsing, but stillare an important part of the input file. See<A HREF="tokenmanager.html">the minitutorial on the token manager</A>for more details of special token handling.<P><LI><STRONG><A NAME="SKIP">SKIP</A></STRONG>:Matches to regular expressions in this regular expression productionare simply skipped (ignored) by the token manager.<P><LI><STRONG><A NAME="MORE">MORE</A></STRONG>:Sometimes it is useful to gradually build up a token to be passed onto the parser. Matches to this kind of regular expression are storedin a buffer until the next TOKEN or SPECIAL_TOKEN match. Then allthe matches in the buffer and the final TOKEN/SPECIAL_TOKEN matchare concatenated together to form one TOKEN/SPECIAL_TOKEN that ispassed on to the parser. If a match to a SKIP regular expressionfollows a sequence of MORE matches, the contents of the buffer isdiscarded.</UL><P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod18">regexpr_spec</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE><A HREF="#prod19">regular_expression</A> [ <EM>java_block</EM> ] [ ":" <EM>java_identifier</EM> ]</TD></TR></TABLE><P>The regular expression specification begins the actual descriptionof the lexical entities that are part of this<A HREF="#prod10">regular expression production</A>.Each regular expression production may contain any number ofregular expression specifications.<P>Each regular expression specification contains a regular expressionfollowed by a Java block (the lexical action) which is optional.This is then followed by an identifier of a lexical state (whichis also optional). Whenever this regular expression is matched,the lexical action (if any) gets executed, followed by any<A HREF="#prod6">common token actions</A>. Then the action dependingon the<A HREF="#prod17">regular expression production kind</A>is taken. Finally, if a lexical state is specified, the tokenmanager moves to that lexical state for further processing (thetoken manager starts initially in the state "DEFAULT").<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod16">expansion_choices</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE><A HREF="#prod20">expansion</A> ( "|" <A HREF="#prod20">expansion</A> )*</TD></TR></TABLE><P>Expansion choices are written as a list of one or more expansionsseparated by "|"s. The set of legal parses allowed by an expansionchoice is a legal parse of any one of the contained expansions.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod20">expansion</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE>( <A HREF="#prod22">expansion_unit</A> )*</TD></TR></TABLE><P>An expansion is written as a sequence of expansion units.A concatenation of legalparses of the expansion units is a legal parse of the expansion.<P>For example, the expansion "{" decls() "}" consists of three expansionunits - "{", decls(), and "}". A match for the expansion is a concatenationof the matches for the individual expansion units - in this case, that wouldbe any string that begins with a "{", ends with a "}", and contains a matchfor decls() in between.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod22">expansion_unit</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD><TD ALIGN=LEFT VALIGN=BASELINE><A HREF="#prod21">local_lookahead</A></TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE><EM>java_block</EM></TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"(" <A HREF="#prod16">expansion_choices</A> ")" [ "+" | "*" | "?" ]</TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>"[" <A HREF="#prod16">expansion_choices</A> "]"</TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>[ <EM>java_assignment_lhs</EM> "=" ] <A HREF="#prod19">regular_expression</A></TD></TR><TR><TD ALIGN=RIGHT VALIGN=BASELINE></TD><TD ALIGN=CENTER VALIGN=BASELINE>|</TD><TD ALIGN=LEFT VALIGN=BASELINE>[ <EM>java_assignment_lhs</EM> "=" ] <EM>java_identifier</EM> "(" <EM>java_expression_list</EM> ")"</TD></TR></TABLE><P>An expansion unit can be a <A HREF="#prod21">local LOOKAHEAD specification</A>.This instructs thegenerated parser on how to make choices at choice points. For detailson how LOOKAHEAD specifications work and how to write LOOKAHEAD specifications,<A HREF="lookahead.html">click here to visit the minitutorial on LOOKAHEAD</A>.<P>An expansion unit can be a set of Java declarations and code enclosedwithin braces (the Java block). These are also called <EM>parseractions</EM>. This is generated into the method parsing thenon-terminal at the appropriate location. This block is executedwhenever the parsing process crosses this point successfully.When JavaCC processes the Java block, it does not perform any detailedsyntax or semantic checking. Hence it is possible that the Java compilerwill find errors in your actions that have been processed by JavaCC.<EM>Actions are not executed during<A HREF="lookahead.html">lookahead evaluation</A>.</EM><P>An expansion unit can be a parenthesized set of one or more<A HREF="#prod16">expansion choices</A>. In which case, a legal parse of the expansionunit is any legal parse of the nested expansion choices.The parenthesized set of expansion choices can be suffixed (optionally) by:<UL><LI><STRONG>"+":</STRONG>Then any legal parse of the expansion unit is one or morerepetitions of a legal parse of the parenthesized set ofexpansion choices.<LI><STRONG>"*":</STRONG>Then any legal parse of the expansion unit is zero or morerepetitions of a legal parse of the parenthesized set ofexpansion choices.<LI><STRONG>"?":</STRONG>Then a legal parse of the expansion unit is either theempty token sequence or any legal parse of the nested expansion choices.An alternate syntax for this construct is to enclose theexpansion choices within brackets "[...]".</UL><P>An expansion unit can be a <A HREF="#prod19">regular expression</A>. Then a legal parseof the expansion unit is any token that matches this regularexpression. When a regular expression is matched, it creates anobject of type <A HREF="apiroutines.html">Token</A>. This objectcan be accessed by assigning it to a variable by prefixing theregular expression with "variable =". In general, you may have anyvalid Java assignment left-hand side to the left of the "=".<EM>This assignment is not performed during<A HREF="lookahead.html">lookahead evaluation</A>.</EM><P>An expansion unit can be a non-terminal (the last choice in the syntaxabove). In which case, it takesthe form of a method call with the non-terminal name used as thename of the method. A successful parse of the non-terminal causesthe parameters placed in the method call to be operated on and avalue returned (in case the non-terminal was not declared to beof type "void"). The return value can be assigned (optionally) toa variable by prefixing the regular expression with "variable =".In general, you may have anyvalid Java assignment left-hand side to the left of the "=".<EM>This assignment is not performed during<A HREF="lookahead.html">lookahead evaluation</A>.</EM>Non-terminals may not be used in an expansion in a manner that introducesleft-recursion. JavaCC checks this for you.<P><HR><P><TABLE><TR><TD ALIGN=RIGHT VALIGN=BASELINE><A NAME="prod21">local_lookahead</A></TD><TD ALIGN=CENTER VALIGN=BASELINE>::=</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -