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

📄 simplereadme.html

📁 This a JavaCC documentation.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
	javacc Simple1.jj	javac *.javaNow you should be able to run the generated parser.  Make sure thatthe current directory is in your CLASSPATH and type:	java Simple1Now type a sequence of matching braces followed by a return and an endof file (CTRL-D on UNIX machines).  If this is a problem on yourmachine, you can create a file and pipe it as input to the generatedparser in this manner (piping also does not work on all machines - ifthis is a problem, just replace "System.in" in the grammar file with'new FileInputStream("testfile")' and place your input inside thisfile):	java Simple1 &lt; myfileAlso try entering illegal sequences such as mismatched braces, spaces,and carriage returns between braces as well as other characters andtake a look at the error messages produced by the parser.---------------------------------------------------------------------DETAILED DESCRIPTION OF Simple2.jj:Simple2.jj is a minor modification to Simple1.jj to allow white spacecharacters to be interspersed among the braces.  So then input suchas:	"{{  }\n}\n\n"will now be legal.Take a look at Simple2.jj.  The first thing you will note is that wehave omitted the options section.  This does not change anything sincethe options in Simple1.jj were all assigned their default values.The other difference between this file and Simple1.jj is that thisfile contains a lexical specification - the region that starts with"SKIP".  Within this region are 4 regular expressions - space, tab,newline, and return.  This says that matches of these regularexpressions are to be ignored (and not considered for parsing).  Hencewhenever any of these 4 characters are encountered, they are justthrown away.In addition to SKIP, JavaCC has three other lexical specificationregions.  These are:. TOKEN:         This is used to specify lexical tokens (see next example). SPECIAL_TOKEN: This is used to specify lexical tokens that are to be                 ignored during parsing.  In this sense, SPECIAL_TOKEN is                 the same as SKIP.  However, these tokens can be recovered                 within parser actions to be handled appropriately.. MORE:          This specifies a partial token.  A complete token is                 made up of a sequence of MORE's followed by a TOKEN                 or SPECIAL_TOKEN.Please take a look at some of the more complex grammars such as theJava grammars for examples of usage of these lexical specificationregions.You may build Simple2 and invoke the generated parser with input fromthe keyboard as standard input.You can also try generating the parser with the various debug optionsturned on and see what the output looks like.  To do this type:	javacc -debug_parser Simple2.jj	javac Simple2*.java	java Simple2Then type:	javacc -debug_token_manager Simple2.jj	javac Simple2*.java	java Simple2Note that token manager debugging produces a lot of diagnosticinformation and it is typically used to look at debug traces a singletoken at a time.---------------------------------------------------------------------DETAILED DESCRIPTION OF Simple3.jj:Simple3.jj is the third and final version of our matching bracedetector.  This example illustrates the use of the TOKEN region forspecifying lexical tokens.  In this case, "{" and "}" are defined astokens and given names LBRACE and RBRACE respectively.  These labelscan then be used within angular brackets (as in the example) to referto this token.  Typically such token specifications are used forcomplex tokens such as identifiers and literals.  Tokens that aresimple strings are left as is (in the previous examples).This example also illustrates the use of actions in the grammarproductions.  The actions inserted in this example count the number ofmatching braces.  Note the use of the declaration region to declarevariables "count" and "nested_count".  Also note how the non-terminal"MatchedBraces" returns its value as a function return value.---------------------------------------------------------------------DETAILED DESCRIPTION OF NL_Xlator.jj:This example goes into the details of writing regular expressions inJavaCC grammar files.  It also illustrates a slightly more complex setof actions that translate the expressions described by the grammarinto English.The new concept in the above example is the use of more complexregular expressions.  The regular expression:  &lt; ID: ["a"-"z","A"-"Z","_"] ( ["a"-"z","A"-"Z","_","0"-"9"] )* &gt;creates a new regular expression whose name is ID.  This can bereferred anywhere else in the grammar simply as &lt;ID&gt;.  What follows insquare brackets are a set of allowable characters - in this case it isany of the lower or upper case letters or the underscore.  This isfollowed by 0 or more occurrences of any of the lower or upper caseletters, digits, or the underscore.Other constructs that may appear in regular expressions are:  ( ... )+	: One or more occurrences of ...  ( ... )?	: An optional occurrence of ... (Note that in the case                  of lexical tokens, (...)? and [...] are not equivalent)  ( r1 | r2 | ... ) : Any one of r1, r2, ...A construct of the form [...] is a pattern that is matched by thecharacters specified in ... .  These characters can be individualcharacters or character ranges.  A "~" before this construct is apattern that matches any character not specified in ... .  Therefore:  ["a"-"z"] matches all lower case letters  ~[] matches any character  ~["\n","\r"] matches any character except the new line charactersWhen a regular expression is used in an expansion, it takes a value oftype "Token".  This is generated into the generated parser directoryas "Token.java".  In the above example, we have defined a variable oftype "Token" and assigned the value of the regular expression to it.---------------------------------------------------------------------DETAILED DESCRIPTION OF IdList.jj:This example illustrates an important attribute of the SKIPspecification.  The main point to note is that the regular expressionsin the SKIP specification are only ignored *between tokens* and not*within tokens*.  This grammar accepts any sequence of identifierswith white space in between.A legal input for this grammar is:"abc xyz123 A B C \t\n aaa"This is because any number of the SKIP regular expressions are allowedin between consecutive &lt;Id&gt;'s.  However, the following is not a legalinput:"xyz 123"This is because the space character after "xyz" is in the SKIPcategory and therefore causes one token to end and another to begin.This requires "123" to be a separate token and hence does not matchthe grammar.If spaces were OK within &lt;Id&gt;'s, then all one has to do is to replacethe definition of Id to:TOKEN :{  &lt; Id: ["a"-"z","A"-"Z"] ( (" ")* ["a"-"z","A"-"Z","0"-"9"] )* &gt;}Note that having a space character within a TOKEN specification doesnot mean that the space character cannot be used in the SKIPspecification.  All this means is that any space character thatappears in the context where it can be placed within an identifierwill participate in the match for &lt;Id&gt;, whereas all other spacecharacters will be ignored.  The details of the matching algorithm aredescribed in the JavaCC documentation in the web pages.As a corollary, one must define as tokens anything within whichcharacters such as white space characters must not be present.  In theabove example, if &lt;Id&gt; was defined as a grammar production rather thana lexical token as shown below this paragraph, then "xyz 123" wouldhave been recognized as a legitimate &lt;Id&gt; (wrongly).void Id() :{}{  &lt;["a"-"z","A"-"Z"]&gt; ( &lt;["a"-"z","A"-"Z","0"-"9"]&gt; )*}Note that in the above definition of non-terminal Id, it is made up ofa sequence of single character tokens (note the location of &lt;...&gt;s),and hence white space is allowed between these characters.---------------------------------------------------------------------</PRE></BODY></HTML>

⌨️ 快捷键说明

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