[antlr-interest] distinguishing between DIV and regular expression

O.E. Dragon dragonoe at mcmaster.ca
Wed Apr 5 05:43:11 PDT 2006


In this case, just increasing k to k=2 would probably do the trick.

You could also try a semantic predicate like this:

REG_EXP : { LA(2) == COMMON_REG }? '/' COMMON_REG etc.

The trick you used should work, but should probably look like this:

> DIV_OR_REG:
>         '/' (
>               { $setType(DIV); }
>               |
>               COMMON_REG   (COMMON_REG | INNER_REG)*
>               '/'
>               ('g' | 'm' | 'i')* { $setType(REG_EXP); }
>         );

And instead of declaring DIV and REG_EXP as protected rules you should
simply add them to you parser's tokens{} section.

-Olivier

On 05 Apr 2006 12:42:42 +0300
 Anakreon Mendis <anakreonmejdi at yahoo.gr> wrote:
> I have a little problem with a lexer and I hope you could help.
> 
> The problem is the two tokens start with the same simbol.
> I have
> DIV : '/';
> and regex:
> REG_EXP :
> '/' COMMON_REG   (COMMON_REG | INNER_REG)* '/' ('g' | 'm' | 'i')*
> ;
> 
> When the code:
> a = b/c;
> the lexer thinks that '/' starts a regular expression.
> This is wrong ofcourse and the parsing fails.
> 
> Any suggestions how to resolve this problem?
> 
> This is a failed attemt I've done:
> DIV_OR_REG:
>         '/' (
>             (' ' | '\t') { _ttype = DIV; $setText("/");}
>             | COMMON_REG   (COMMON_REG | INNER_REG)*
>             '/'
>             ('g' | 'm' | 'i')* { _ttype = REG_EXP;}
>             | { _ttype = DIV; }
>         );
> 
> 
> protected DIV:;
> protected REG_EXP:;
> 
> One thought I had is to create a lexer for regex
> and control would be delivered to it whenever a '/'
> character is found. If that lexer failed to parse
> the input, then the '/' would be set to type DIV.
> 
> I hope there is a easier solution.
> 
> Anakreon
> -- 
> Three words describe our society:homo homini lupus
> 



More information about the antlr-interest mailing list