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

📄 simplereadme.html

📁 This a JavaCC documentation.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<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 README for SimpleExamples</title><!-- Changed by: Michael Van De Vanter, 14-Jan-2003 --></HEAD><BODY bgcolor="#FFFFFF" ><H1>JavaCC [tm]: README for SimpleExamples</H1><P><PRE>This directory contains five examples to get you started using JavaCC [tm].Each example is contained in a single grammar file and is listedbelow:	Simple1.jj	Simple2.jj	Simple3.jj	Simple4.jj	NL_Xlator.jjOnce you have tried out and understood each of these examples, youshould take a look at more complex examples in other sub-directoriesunder the examples directory.  But even with just these examples, youshould be able to get started on reasonably complex grammars.---------------------------------------------------------------------Summary Instructions:If you are a parser and lexical analyzer expert and can understand theexamples by just reading them, the following instructions show you howto get started with JavaCC.  The instructions below are with respectto Simple1.jj, but you can build any parser using the same set ofcommands.1. Run javacc on the grammar input file to generate a bunch of Java   files that implement the parser and lexical analyzer (or token   manager):	javacc Simple1.jj2. Now compile the resulting Java programs:	javac *.java3. The parser is now ready to use.  To run the parser, type:	java Simple1The Simple1 parser and others in this directory are designed to takeinput from standard input.  Simple1 recognizes matching bracesfollowed by zero or more line terminators and then an end of file.Examples of legal strings in this grammar are:  "{}", "{{{{{}}}}}", etc.Examples of illegal strings are:  "{{{{", "{}{}", "{}}", "{{}{}}", "{ }", "{x}", etc.Try typing various different inputs to Simple1.  Remember &lt;control-d&gt;may be used to indicate the end of file (this is on the UNIX platform).Here are some sample runs:	% java Simple1	{{}}&lt;return&gt;	&lt;control-d&gt;	%	% java Simple1	{x&lt;return&gt;	Lexical error at line 1, column 2.  Encountered: "x"	TokenMgrError: Lexical error at line 1, column 2.  Encountered: "x" (120), after : ""	        at Simple1TokenManager.getNextToken(Simple1TokenManager.java:146)	        at Simple1.getToken(Simple1.java:140)	        at Simple1.MatchedBraces(Simple1.java:51)	        at Simple1.Input(Simple1.java:10)	        at Simple1.main(Simple1.java:6)	%	% java Simple1	{}}&lt;return&gt;	ParseException: Encountered "}" at line 1, column 3.	Was expecting one of:	    &lt;EOF&gt; 	    "\n" ...	    "\r" ...	        at Simple1.generateParseException(Simple1.java:184)	        at Simple1.jj_consume_token(Simple1.java:126)	        at Simple1.Input(Simple1.java:32)	        at Simple1.main(Simple1.java:6)	%---------------------------------------------------------------------DETAILED DESCRIPTION OF Simple1.jj:This is a simple JavaCC grammar that recognizes a set of left bracesfollowed by the same number of right braces and finally followed byzero or more line terminators and finally an end of file.  Examples oflegal strings in this grammar are:  "{}", "{{{{{}}}}}", etc.Examples of illegal strings are:  "{{{{", "{}{}", "{}}", "{{}{}}", etc.This grammar file starts with settings for all the options offered byJavaCC.  In this case the option settings are their default values.Hence these option settings were really not necessary.  One could aswell have completely omitted the options section, or omitted one ormore of the individual option settings.  The details of the individualoptions is described in the JavaCC documentation in the web pages.Following this is a Java compilation unit enclosed between"PARSER_BEGIN(name)" and "PARSER_END(name)".  This compilation unitcan be of arbitrary complexity.  The only constraint on thiscompilation unit is that it must define a class called "name" - thesame as the arguments to PARSER_BEGIN and PARSER_END.  This is thename that is used as the prefix for the Java files generated by theparser generator.  The parser code that is generated is insertedimmediately before the closing brace of the class called "name".In the above example, the class in which the parser is generatedcontains a main program.  This main program creates an instance of theparser object (an object of type Simple1) by using a constructor thattakes one argument of type java.io.InputStream ("System.in" in thiscase).The main program then makes a call to the non-terminal in the grammarthat it would like to parse - "Input" in this case.  All non-terminalshave equal status in a JavaCC generated parser, and hence one mayparse with respect to any grammar non-terminal.Following this is a list of productions.  In this example, there aretwo productions that define the non-terminals, "Input" and"MatchedBraces," respectively.  In JavaCC grammars, non-terminals arewritten and implemented (by JavaCC) as Java methods.  When thenon-terminal is used on the left-hand side of a production, it isconsidered to be declared and its syntax follows the Java syntax.  Onthe right-hand side, its use is similar to a Java method call.Each production defines its left-hand side non-terminal followed by acolon.  This is followed by a bunch of declarations and statementswithin braces (in both cases in the above example, there are nodeclarations and hence this appears as "{}") which are generated ascommon declarations and statements into the generated method.  This isthen followed by a set of expansions also enclosed within braces.Lexical tokens (regular expressions) in a JavaCC input grammar areeither simple strings ("{", "}", "\n", and "\r" in the above example),or a more complex regular expression.  In our example above, there isone such regular expression "&lt;EOF&gt;" which is matched by the end offile.  All complex regular expressions are enclosed within angularbrackets.The first production above says that the non-terminal "Input" expandsto the non-terminal "MethodBraces" followed by zero or more lineterminators ("\n" or "\r") and then the end of file.The second production above says that the non-terminal "MatchedBraces"expands to the token "{" followed by an optional nested expansion of"MatchedBraces" followed by the token "}".  Square brackets [...]in a JavaCC input file indicate that the ... is optional.[...] may also be written as (...)?.  These two forms are equivalent.Other structures that may appear in expansions are:   e1 | e2 | e3 | ... : A choice of e1, e2, e3, etc.   ( e )+             : One or more occurrences of e   ( e )*             : Zero or more occurrences of eNote that these may be nested within each other, so we can havesomething like:   (( e1 | e2 )* [ e3 ] ) | e4To build this parser, simply run JavaCC on this file and compile theresulting Java files:

⌨️ 快捷键说明

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