You can use the examples and documents provided by JavaCC to get started. The following is a brief summary:
Both lexical and syntax rules are written in a. jj file. This file should be written as follows:
1. Start:
[Java]
Options
{
JDK_VERSION = "1.5 ";
STATIC = false;
}
Options
{
JDK_VERSION = "1.5 ";
STATIC = false;
}
Put this in. JDK_VERSION is the Jdk version applicable to the generated code. It can be 1.5, 1.6, or 1.7. STATIC indicates whether to generate a STATIC parser class. For other options, see the document.
2. Main class
[Java]
PARSER_BEGIN (Sparql)
Package cn.edu. ruc. iir. sparql;
Import cn.edu. ruc. iir. query .*;
Import cn.edu. ruc. iir. query. model .*;
Import cn.edu. ruc. iir. query. util .*;
Import java. text .*;
Public class Sparql
{
Public static void main (String args []) throws ParseException
{
Sparql parser = new Sparql (System. in );
Int res = 0;
While (true)
{
System. out. print ("MyRDF> ");
Try
{
Res = parser. one_query ();
If (res = 0)
{}
Else if (res = 1)
{
Break;
}
Else
{
System. out. println ("Please correct your query .");
}
}
Catch (Exception e)
{
System. out. println ("NOK .");
System. out. println (e. getMessage ());
Parser. ReInit (System. in );
}
Catch (Error e)
{
System. out. println ("Oops .");
System. out. println (e. getMessage ());
Break;
}
}
}
}
PARSER_END (Sparql)
PARSER_BEGIN (Sparql)
Package cn.edu. ruc. iir. sparql;
Import cn.edu. ruc. iir. query .*;
Import cn.edu. ruc. iir. query. model .*;
Import cn.edu. ruc. iir. query. util .*;
Import java. text .*;
Public class Sparql
{
Public static void main (String args []) throws ParseException
{
Sparql parser = new Sparql (System. in );
Int res = 0;
While (true)
{
System. out. print ("MyRDF> ");
Try
{
Res = parser. one_query ();
If (res = 0)
{}
Else if (res = 1)
{
Break;
}
Else
{
System. out. println ("Please correct your query .");
}
}
Catch (Exception e)
{
System. out. println ("NOK .");
System. out. println (e. getMessage ());
Parser. ReInit (System. in );
}
Catch (Error e)
{
System. out. println ("Oops .");
System. out. println (e. getMessage ());
Break;
}
}
}
}
PARSER_END (Sparql)
Defines the main class of the syntax parser between PARSER_BEGIN and PARSER_END, which is the entrance of the entire parsing program, it mainly contains some referenced packages and classes and a main method (other methods are generated by JavaCC ). Since STATIC is set to false above, a parser object needs to be created here. A parser. one_query () is called once to parse a statement and obtain the parsing result.
3. Define lexical rules
[Java]
SKIP:
{
""
| "\ T"
| "\ R"
| "\ N"
}
TOKEN:/* OPERATORS */
{
<SELECT:
"Select"
| "SELECT">
| <WHERE:
"Where"
| "WHERE">
| <OBRACE: "{">
| <CBARCE: "}">
}
TOKEN:
{
<UNKNOWN :"? "<KNOWN>
| <KNOWN: (["<", ">", "a"-"z", "A"-"Z", "0"-"9 ",": ","/","_","-","\"","'","~ "," # "]) +>
| <MISSEDP: "[" "]">
| <EXIT:
"Exit"
| "EXIT">
}
SKIP:
{
""
| "\ T"
| "\ R"
| "\ N"
}
TOKEN:/* OPERATORS */
{
<SELECT:
"Select"
| "SELECT">
| <WHERE:
"Where"
| "WHERE">
| <OBRACE: "{">
| <CBARCE: "}">
}
TOKEN:
{
<UNKNOWN :"? "<KNOWN>
| <KNOWN: (["<", ">", "a"-"z", "A"-"Z", "0"-"9 ",": ","/","_","-","\"","'","~ "," # "]) +>
| <MISSEDP: "[" "]">
| <EXIT:
"Exit"
| "EXIT">
}
This part defines the rules of the lexical parser, SKIP defines the strings to be ignored, and TOKEN defines the strings to be recognized. Note: It is not to say that the SKIP rules in the input are removed before parsing. That is not scientific, and normal lexical parsing will not do that, in the process of processing input strings in sequence, the "big mouth" method is used to identify the substrings as long as possible. If the lexical rules are ambiguous, JavaCC will give warnings. Do not ignore these warnings.
4. Define syntax rules
The syntax unit of JavaCC is as follows:
[Java]
Token subject ():
{
Token token = null;
}
{
Token = <UNKNOWN>
{
Return token;
}
| Token = <KNOWN>
{
Token token1 = token;
}
(
"." Token = <KNOWN>
{
Token1.image + = "." + token. image;
}
)*
{
Return token1;
}
}
Token subject ():
{
Token token = null;
}
{
Token = <UNKNOWN>
{
Return token;
}
| Token = <KNOWN>
{
Token token1 = token;
}
(
"." Token = <KNOWN>
{
Token1.image + = "." + token. image;
}
)*
{
Return token1;
}
} Starts with a declaration, including the return value type, rule name, and a colon. For such a syntax rule, JavaCC generates a method with the same name in the syntax analyzer class. Then, write some variable declarations in a pair of curly braces. The specific content of the rule is written in the next pair of curly braces.
When a syntax unit has multiple rules, use | to separate them. Each rule is composed of a series of lexical or syntax units. Each lexical or syntax unit is followed by a pair of curly brackets to write the code to be processed.