sample program to use the lexer
java Main sample.input
output
token 0: type: 1, text: 2
token 1: type: 2, text: +
token 2: type: 1, text: 4
token 3: type: 2, text:
token 4: type: 2, text: +
token 5: type: 2, text:
token 6: type: 2, text: a
token 7: type: 1, text: 6
token 8: type: 2, text:
jflex grammar: simple.flex
%%
%{
private int comment_count = 0;
public static void trace(String msg) {
System.out.println(msg);
}
public Yytoken t(int type, String text) {
return new Yytoken(type, text);
}
%}
%line
%char
%full
//%debug
DIGIT=[0-9]
%%
{DIGIT} { return t(1, yytext());}
<<EOF>> {return t(-1, "eof"); }
. {
return t(2, yytext() );
}
Main.java
public class Main {
public static void main(String argv[]) throws Exception {
if (argv.length <1) {
System.out.println("Usage: HelloMain file");
return;
}
Yylex scanner = null;
scanner = new Yylex( new java.io.FileReader(argv[0]) );
for(int i=0; i<9; i++) {
System.out.println("token "+i+": "+scanner.yylex());
}
}
}