The second example is a simple improvement.
This one does not abend when the input does not
have the word test in it. It gives due
warning instead.
options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 2;
FORCE_LA_CHECK=true;
}
PARSER_BEGIN(Simple2)
public class Simple2 {
/** Main entry point. */
public static void main(String args[]) throws ParseException {
Simple2 parser = new Simple2(System.in);
parser.Input();
}
}
PARSER_END(Simple2)
SKIP : {
" "
"\t"
"\n"
"\r"
}
TOKEN: { < TEST: "test">}
TOKEN: {<CR_LF: "\r\n"> }
TOKEN : { <OTHER : ~[] > }
void Input() :
{
System.out.println("Start");
Token t1=null;}
{
(
(<OTHER>)*
(t1=<TEST> {System.out.println("This line has test in it.");})?
(<OTHER>)*
<CR_LF> {if (t1==null) System.out.println("Sorry no test in this line!");}
)*
<EOF >
}