[antlr-interest] ANTLR 3.1: Abbreviated keywords & imported lexer
Terence Parr
parrt at cs.usfca.edu
Mon Aug 18 16:08:55 PDT 2008
i'd just leave them as separate parser / lexer. no need to use import
to combine them here.
ter
On Aug 18, 2008, at 2:38 PM, zosrothko wrote:
> 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;} ;
>
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
More information about the antlr-interest
mailing list