Problem solved using syntactic lookahead, although not really sure why it makes such a difference....
I changed the line:
(LOOKAHEAD(2) (<AND> { sb.append(dict.getAND(dict.getDATE())); }
| <OR> { sb.append(dict.getOR(dict.getDATE())); } ) { sb.append(DateComparison(dict)); }
)* { return sb.toString(); }
to read:
(LOOKAHEAD(2,(< AND >|< OR >) DateComparison(dict)) (<AND> { sb.append(dict.getAND(dict.getDATE())); }
| <OR> { sb.append(dict.getOR(dict.getDATE())); } ) { sb.append(DateComparison(dict)); }
)* { return sb.toString(); }
and it now seems to work..
rhysf wrote:
Hi,
I am having a problem with lookaheads not seeming to be executed. The options in my jj file are as follows:
options{
STATIC=true ;
IGNORE_CASE=true ;
USER_CHAR_STREAM=true ;
JDK_VERSION="1.5";
OUTPUT_DIRECTORY= "../blah/";
CHOICE_AMBIGUITY_CHECK=3;
OTHER_AMBIGUITY_CHECK=2;
DEBUG_LOOKAHEAD=true;
}
Here is a snippet from the jj file (Statement is the entry point):
String Statement(Dictionary dict):
{
StringBuffer sb = new StringBuffer();
sb.append(dict.getPREAMBLE());
}
{
{ sb.append(Clause(dict)); }
(( <AND> { sb.append(dict.getAND()); }
| <OR> { sb.append(dict.getOR()); } ) { sb.append(Clause(dict)); })*
{ return sb.append(dict.getPOSTAMBLE()).toString(); }
}
String Clause(Dictionary dict):
{
StringBuffer sb = new StringBuffer();
}
{
<DATE_FIELD> { sb.append(dict.getDATE()); } {sb.append(DateClause(dict)); } { return sb.toString(); }
| ... etc
}
String DateClause(Dictionary dict):
{
Token timeperiod;
StringBuffer sb = new StringBuffer();
}
{
...some other choices.....
|
{
sb.append(DateComparison(dict));
}
(LOOKAHEAD(2) (<AND> { sb.append(dict.getAND(dict.getDATE())); }
| <OR> { sb.append(dict.getOR(dict.getDATE())); } ) { sb.append(DateComparison(dict)); }
)* { return sb.toString(); }
The problem is that the DateClause will consume the <AND> token even if the next token is not a DateComparison, and then naturally fail. Debug output does not show anything looking ahead at any point during execution, and the generated code has no lookahead in it either. Is there some option I don't know about that switches off lookahead functionality?