[antlr-interest] distinguishing between DIV and regular expression

Anakreon Mendis anakreonmejdi at yahoo.gr
Wed Apr 5 02:42:42 PDT 2006


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