[antlr-interest] Help controlling parser decisions

Gavin Lambert antlr at mirality.co.nz
Wed Jul 25 14:28:46 PDT 2007


At 04:52 26/07/2007, Ted Villalba wrote:
>The major difference between our grammars is yours does not have 
>any lexer rules for the operator NEAR, so there is no conflict. 
>Adding the BOOL_OP lexer rule back in breaks that example.
[...]
>query :  tag '=' keyBOOL terms+
>       ;
>
>terms  : WCHAR+
>        ;
>
>tag    : WCHAR
>        ;
>
>keyBOOL: near
>        ;
>
>near:   {input.LT(1).getText().toLowerCase().equals("near")}? 
>WCHAR
>        ;
>
>BOOL_OP :  'NEAR'; //comment this out to get working
>WS      : (' '|'\t'|'\r'|'\n')+ {skip();};
>WCHAR   : ~('='|'('| ')'|'"'|' '|'\t'|'\n'|'\r'|'#')+;

I think you need to make your parser more lenient.

1. Rename 'BOOL_OP' to 'NEAR', and don't add any other keywords to 
it -- give those their own separate lexer rules.

2. Create a parser rule 'bool_op' that accepts NEAR.

3. Remove 'terms' because it's pointless (you've already got 
'multiple characters' at the lexing level, and 'multiple terms' at 
the 'query' level).

4. Remove the 'near' rule and the 'keyBOOL' rule (since you've got 
'bool_op' now).

5. Wherever a bool_op can be used in a non-keyword context, add it 
as an alternative.  Presumably, this means changing 'tag' to 
"WCHAR | bool_op".

You'll still end up with a NEAR token output, not a WCHAR, but it 
should match now.



More information about the antlr-interest mailing list