[antlr-interest] ANTLR 3.1: Abbreviated keywords & imported lexer

zosrothko zosrothko at orange.fr
Mon Aug 18 14:38:03 PDT 2008


Hi

I am trying to implement the implicit abbreviated keywords according to 
the Wiki FAQ on the Simple example along with the importing grammar 
feature, but whatever configuration I am testing, ANTLR answer is  
Simple.g:18:8: no lexer rule corresponding to token: VAR
Can someone explain me how should be declare the keyword VAR using the 
implicit strategy(i.e, without putting VAR : 'VAR' in the lexer)??

TIA
zos



Here the Simple parser extended
grammar Simple;
import CommonLexer;

tokens {
    VAR;
}
@header {
    package simple;
}
@lexer::header {
    package simple;
}
file : 'program' ID ';' {System.out.println("found program "+$ID.text);}
       decl+
     ;

decl : VAR ID ('=' expr)? ';'
       {System.out.println("found var "+$ID.text);}
     ;

expr : INT | FLOAT ;



Here the CommonLexer

lexer grammar CommonLexer;
@header {
    package simple;
}
@members {
    private static final Map<String, Integer> keywords;
    static {
        keywords = new TreeMap<String, Integer>();
        keywords.put("VAR", VAR);
    }
    private int CheckKeywordsTable(int type, string lexeme) {
        return type;
    }
}

ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
    { $type = CheckKeywordsTable(ID, getText()) };

INT : '0'..'9'+ ;

FLOAT:  INT '.' INT?
  | '.' INT
  ;

CHAR:   '\'' ( ESC | ~('\''|'\\') ) '\''
    ;

STRING
    :  '"' ( ESC | ~('\\'|'"') )* '"'
    ;

fragment
ESC :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    ;

COMMENT
    :   '/*' ( options {greedy=false;} : . )* '*/' {$channel=HIDDEN;}
    ;

LINE_COMMENT
    : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}
    ;

WS : (' '|'\t'|'\r'|'\n')+ {$channel=HIDDEN;} ;




More information about the antlr-interest mailing list