JAVACC jjtree notation and the basic grammar and application of JJ
2012-2-7 Read 2279 comments 0
/***********************************************************/> the test jjt,jj file I used is from the javacc5.0 version >dir_hier/ Javacc-5.0/javacc-5.0/examples/jjtreeexamples simpleexamples/***********************************************************//*********************************** /0.JAVACC (the Java Compiler Compiler) Overview >abstract SUMMARY:JAVACC is generally used to write rules for writing in a language, such as the syntax of Java rules variables must start with a letter or the end of a number, and so on, this is a writing specification, the writing specification is unique to the language, he must be satisfied, then he is how to judge the character of the script to conform to the rules, it needs to be validated, JAVACC is A tool used to define the character channeling rules for a particular input format, as well as a validator that validates the character channeling of this particular format. Defines the character channeling input format, and verifies that the input character channeling is the most basic function of JAVACC. JAVACC syntax rules describe the way a file is written in one of two ways is. jj file, the other is. jjt file, the two files are not very different in writing, but Jjt file is more easy than JJ file expression syntax rules, JJ file optional parameters and JJT are very different, This is their biggest difference. /***********************************************************//************************************************* /1.JAVACC Grammar Rule writing Operation codebase > How to write conditional structures in production expressions (term = < terms > | term = < STAR > | term = < Prefixterm > |term = < Wildterm > | term = < number >) > The generated code corresponds to the analysis: because in JAVACC (<A> | <B>) is expressed as the meaning of arbitrarily selecting one of the two rules. Switch (tokenkind) {case term:/** dosomthing...*/break; Case STAR:/** dosomthing...*/break; void Ifexp (): {int k=0;/** java local variable declaration */}{{/* writes arbitrary Java code in {} and is in object-oriented way */} ({if (k==2) {break;} System.out.println ("k=" +k); k++; }) *}> how to write loop structure in production expressions ({if (k==2) {break;} System.out.println ("k=" +k); k++; }->) *> generates the corresponding code while (true) {if (k==2) {break; } System.out.println ("k=" +k); k++; }> calls other production expressions in the production expression corresponding to method calls in Java (): {stringbuffer sb = new StringBuffer (); Token t = null; } {t = a/** here corresponds to the generated Java code T=jj_consume_token (a); * because it equals to consume a mark apply this token of consumption to the T variable */} void A (): {}{}>JAVACC in the lexical state describes <*>token:{} <default>token:{<a: " 2001 to 2002 ">:D aterange | <b: "Anything";} <daterange>token:{} parsing:<*>,<default>,<daterange> is the benefit of lexical state definition lexical states, If you are in a production expression There's just a character channeling match to the 2001 to 2002 this character channeling then he's going to immediately define the next pattern character channeling to be matched to daterange the token> defined in the lexical state has a loop with the conditional structure defined by the way Java The cc grammar file is quite perfect. /***********************************************************//************************************************* /2.JJT Grammar Rules Description File Writing method >JJT summary Jjt can clearly express the idea of grammatical analysis, and he put each production expression as a node to represent, the execution of these nodes in an effective organization into a syntax analysis tree; Options>build_node_files (default:true) creates a sample implementation for Simplenode and other nodes used in the syntax. >multi (Default:false) creates a multi-modal parse tree. This option defaults to False to generate a single-mode parse tree. >node_default_void (default:false) When this option is set to true, no one node is defined for each non-wrapper production, and instead is empty. >node_factory (Default:false) uses a factory method to create a node in the following way: public static node jjtcreate (int id) >node_package (default: "" ) is placed into the package in the Build node class. The default is the package for the parser. >node_prefix (default: "AST") AST means abstract node in multi-mode, prefix is used to construct node class name from node marker. The default prefix is "AST" >node_scope_hook (default:false) Inserts a user-defined parsing method at the node scope entry and exit. The method that needs to be called before the node's life begins and ends becomes a hook. See also: node-scoped hooks. >node_uses_parser (Default:false) Whether the current parser object is also in the properties of the node object Jjtree uses a selection to pass the parsed object to the constructor. For example: public static Node MynoDe.jjtcreate (myparser p, int id); Mynode (myparser p, int id); >static (default:true) generates code for the static parser. The option defaults to True. This must be consistent by using the equivalent JAVACC option. The value of the option is published in the source code of the JAVACC. >visitor (Default:false) inserts the Jjtaccept () method in the node class, producing a visitor implementation for each node type used in the syntax. >visitor_exception (default: "") if this option is set, it will use the form of the Jjtaccept () and visit () methods. Note: This option will be removed in a later version of Jjtree. If it does not affect you please do not use it. >jjtree_output_directory (default:use value of output_directory) by default, the output directory JJTREE generated is specified in the Global output_directory settings. The explicit setting of this option allows the user to detach the parser from the tree file. /***********************************************************//************************************************* /3. Parse tree node >JAVACC each production expression as a simple node (simplenode) in the case of the default MULTI (Default:false), JAVACC has provided a simple implementation of this Simplenode class. >multi (Default:true) will provide a default simple implementation class for each node in the form of [node_prefix_ production expression name]. > defines the way a node is defined explicitly: a node created with the specified number of child nodes void Additiveexpression () #void: {} {(Multiplicativeexpression () (("+" | "-") multiplicativeexpression ()) *) #Add (3)}> parse tree reference result: Add integer integer integer> #void不会为这个生产表达式生成对应的节点 > #Add表示在循环执行过程中生成的所有节点中的前三个节点作为 The son node named Add. by condition definition: void Additiveexpression () #void: {} {(Multiplicativeexpression () ("+" | "-") multiplicativeexpression ()) #Add (>3)}> #Add (>1) Another notation #add (jjtree.arity () > 1) Generated source code: Jjtree . Closenodescope (jjtn001, jjtree.nodearity () > 1); When the number of nodes created during the execution loop is greater than 1, all nodes are added as child nodes of the Add node and add nodes to the stack, and if the condition does not meet then all nodes created will remain in the stack by default in the child node of the previous node > parse tree Reference result: Start/ * * Because the number of nodes created during the execution of the loop does not reach the specified number so it is start*/Integer integer>notes: (...) #N (A ()) above the expression logic is unclear, you must explicitly use the conditional formula: (...) #N (t Rue) (A ()) > Specifies the name of the node for the resulting type void Productexp () #MyNode: {} {//doanything} #MyNode为这个生产表达式所对应的节点的名称 > Special application void P3 (): {} {P 4 () (P5 ()) + #ListOfP5s P6 ()} #name =jjtree.closenodescope (jjtn001, True); #name (3) =jjtree.closenodescope (jjtn001, 3); Name (>3) =jjtree.closenodescope (jjtn001, jjtree.nodearity () > 1);> what is Jjthisaststart Start (): {} {Expression ( ) ";" {return jjtthis;}} Analysis: Jjthis represents a reference to the current nodeAddress, as if Aststart start () This production expression corresponds to the start node then jjtthis points to the object, within which the implementation code is: Aststart jjtn000 = new Aststart (Jjtstart); > What is node_scope_hook= "true" (the default is true to indicate that there is a hook) this name is very interesting, the hook is exactly when a node is created to invoke the specified hook method, the method to be called when the node is added to the stack The hook can do some preparatory work at open, end can do some follow-up work sample code: Static final public void Additiveexpression () throws ParseException {Astadd JJ tn001 = new Astadd (jjtadd);//Create node Boolean jjtc001 = true; Jjtree.opennodescope (jjtn001); This is the hook try {//do something ...} finally {if (jjtc001) {Jjtree.closenodescope (jjtn001, 3);//This is the hook. }}}/***********************************************************/4. JJT file-generated file parsing SimpleQueryParser.jjtSimpleQueryParser.jjJJTSimpleQueryParserState.javaNode.javaParseException.javaSimpleChar Stream.javaSimpleNode.javaSimpleQueryParser.javaSimpleQueryParserConstants.javaSimpleQueryParserTokenManager.javaSimpleQu ERYPARSERTREECONSTANTS.JAVATOKEN.JAVATOKENMGRERROR.JAVA>JJT files can generate the. jj File > The name Simplequeryparser is repeated in many class names, through Parser_begiN (simplequeryparser) ... specified by Parser_end (simplequeryparser). >jjtsimplequeryparserstate.java used to record the organization of a node this class has a separate description, and in Jjtsimplequeryparserstate.java, he is a class that is designed in the form of stacks. >node.java is an interface jjt in which the node must implement him, at the time of option multi (default:false) The JAVACC compiler has helped us simply do the implementation of this class for Simplenode.java. >parseexception.java exception thrown when parsing a non-conforming character >simplequeryparserconstants.java This class is used primarily for association definitions token> Simplequeryparsertreeconstants.java a node that is primarily used for association definitions if the production expression is not named then the default is to use the name of the production expression as the name of the node > Simplequeryparsertokenmanager.java This is the most important class he used to produce the token object in Getnexttoken () embodies so little > Token.java This object is used as the smallest tick unit to encapsulate Simplequeryparsertokenmanager generated tokens >simplequeryparser.java the classes together to provide syntax Analysis Services, He is the consumer token object, in Jj_consume_token (Token_kind) can reflect this, passing in a token type returns a token object of this type > Tokenmgrerror.java encountered an error during the production of the token object >simplecharstream.java used to encapsulate the character channeling of the input >simplequeryparsertokenmanager.java/ * * Method for producing token*/Simplequeryparsertokenmanager.getnexttoken ()/** Production token *//** The Simplequeryparser of consumer tokens uses these tokens to generate specific logical objects */Simplequeryparser.java Simplequeryparser.jj_consuMe_token (int kind);/** consumes one token*/SIMPLEQUERYPARSER.JJ_NTK ()/** According to the specified type if the current JJ_NTK variable is-1 Indicates that the next token of the current token pair is not found and needs to go to tokenmanage and take a token out of the next*//**************************************** as the current token. /5.JJ File Parsing >options> Static=false; meaning that all production expressions correspond to Java code when not static/***********************************************************/
Javacc jjtree notation and the basic syntax and application of JJ notation