[antlr-interest] Using JFlex with Antlr

FranklinChen at cmu.edu FranklinChen at cmu.edu
Thu Oct 4 11:41:28 PDT 2007


> Now, I am still interested in how to couple JFlex with ANTLR. Anyone
> who can share information about this?
> 
> -Andreas

I am a happy user of JFlex with ANTLR 3.  What you need to do is to
make the interface of the JFlex-generated scanner compatible with what
ANTLR expects.  Here is how I do it in a file MyLexer.flex:

import org.antlr.runtime.*;

// To get ANTLR token types generated for parser.
import static mypackage.MyParser.*;

%%

%public
%class MyLexer
%implements TokenSource
%type Token
%unicode
%line
%column

%{
    public Token nextToken() {
        try {
            return yylex();
        }
        catch (java.io.IOException e) {
            System.err.println("shouldn't happen: " + e.getMessage());
            return Token.EOF_TOKEN;
        }
    }

    /** Turn 1-based to 0-based. */
    public void setLine(int line) {
        this.yyline = line-1;
    }

    public void setColumn(int column) {
        this.yycolumn = column;
    }

    public int getLine() {
        return this.yyline+1;
    }

    public int getColumn() {
        return this.yycolumn;
    }

    private Token newToken(int type, String text) {
        CommonToken token = new CommonToken(type, text);
        token.setLine(getLine());
        token.setCharPositionInLine(getColumn());
        return token;
    }

    private Token newToken(int type) {
        CommonToken token = new CommonToken(type, yytext());
        token.setLine(getLine());
        token.setCharPositionInLine(getColumn());
        return token;
    }
%}

%%

// ...actions... use newToken to return token

<<EOF>> { return newToken(EOF); }


Then in the driver in Java, just do the following:

MyLexer lexer = new MyLexer(reader);
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
MyParser parser = new MyParser(tokenStream);

-- 
Franklin


More information about the antlr-interest mailing list