- Save the JavaCC file as, say, hw1.jj
- % javacc hw1.jj
- % javac *.java
- % java simple input_file_name
options {
IGNORE_CASE = false;
OPTIMIZE_TOKEN_MANAGER = true;
}
PARSER_BEGIN(simple)
import java.io.*;
public class simple {
public static void main(String[] args) throws FileNotFoundException
{
if ( args.length < 1 ) {
System.out.println("Input filename required as argument");
System.exit(1);
}
SimpleCharStream stream = new SimpleCharStream(
new FileInputStream(args[0]));
Token t = null;
simpleTokenManager tokenMgr = new simpleTokenManager(stream);
int line_no = 1;
do {
t = tokenMgr.getNextToken();
switch(t.kind) {
case ADD: System.out.println("ADD: " + t.image);
break;
case RELOP: System.out.println("RELOP: " + t.image);
break;
case NUMBER: System.out.println("NUMBER: " + t.image);
break;
case NAME: System.out.println("NAME: " + t.image + " line = " + line_no);
break;
case NL: line_no++;
break;
default:
if ( t.kind != EOF )
System.out.println("OTHER: " + t.image);
break;
}
} while (t.kind != EOF);
}
}
PARSER_END(simple)
SKIP:
{
"\t"
| "\r"
| " "
}
TOKEN:
{
<NL: "\n">
}
TOKEN:
{
<ADD: "+">
| <RELOP: "!=" | "==">
}
TOKEN:
{
<NUMBER: (["0"-"9"])+>
| <NAME: ["a"-"z","A"-"Z"] (["a"-"z","A"-"Z","0"-"9","_"])*>
}