create a grammar file
hello.jj
PARSER_BEGIN(Hello)
package egtry.hello;
import java.io.StringReader;
public class Hello {
public static void main(String[] args) throws Exception {
StringReader in=new StringReader("count 10");
Hello hello=new Hello(in);
Hello.words();
}
}
PARSER_END(Hello)
SKIP: { " " |"\t" |"\n" |"\r" }
TOKEN: { <WORD: (["a"-"z"])+ > }
TOKEN: { <INT: (["0"-"9"])+ > }
void words():
{
int id=0;
}
{
<WORD>
{ id++;
System.out.println(id+" word");
}
<INT>
{
id++;
System.out.println(id+"id");
}
<EOF>
}
Compile hello.jj and generate java codes
java -classpath "\java\javacc-5.0\bin\lib\javacc.jar" javacc hello.jj
Run the Hello.java
Generated the Hello.java source
Hello.java
package egtry.hello; //copied from the grammar file
import java.io.StringReader; //copied from the grammar file
public class Hello implements HelloConstants {
//copied from the grammar file
public static void main(String[] args) throws Exception {
StringReader in=new StringReader("count 10");
Hello hello=new Hello(in);
Hello.words();
}
static final public void words() throws ParseException {
int id=0; //copied from the rule init block
jj_consume_token(WORD);
id++; //copied from production action
System.out.println(id+" word");
jj_consume_token(INT);
id++;
System.out.println(id+"id");
jj_consume_token(0); //EOF
}
....
}