[antlr-interest] Lexer problem - previous token semantic predicate

arjode at uni-koblenz.de arjode at uni-koblenz.de
Fri Apr 4 07:34:02 PDT 2008


> I have a problem in forcing the lexer to emit the right token type,
> based on previous token type encountered.

I've encounter the same problem. My solution is to remember the last
(significant) emited token in the lexer and check for this (this was for
embedded RegEx).

Heres my code:

@lexer::members {

private Token lastSignificantToken;

public void emit(Token token) {
    if(token.getChannel() == Token.DEFAULT_CHANNEL) {
        lastSignificantToken = token;
    }
    super.emit(token);
}

private boolean isLastToken(int... type) {
    Token last = getLastSignificantToken();
    if(last == null)
        return false;
    for(int t : type) {
        if(t == last.getType())
            return true;
    }
    return false;
}

private boolean isLastToken(String... type) {
    Token last = getLastSignificantToken();
    if(last == null)
        return false;
    for(String t : type) {
        if(t.equals(last.getText()))
            return true;
    }
    return false;
}

private Token getLastSignificantToken() {
    return lastSignificantToken;
}
}


RegEx
 : {isLastToken("=", "(", ",")}?=>'/' ( RegEx_ESCAPE_SEQUENCE |
~('\\'|'\''|'\n'|'\r'|'/') )* '/' ('g'|'i'|'m'|'s'|'x')* ;
fragment RegEx_ESCAPE_SEQUENCE : '\\' ('/') ;


Hope that helps.



More information about the antlr-interest mailing list