[antlr-interest] newline in Antlr3

Ilia Kantor ilia at obnovlenie.ru
Thu Aug 31 13:58:12 PDT 2006


I'm converting Antlr2 grammar to Antlr v3 (thanks, Terrence).

But somehow lexer misses line number... And newline() call from Antlr2 is not 
working.


------------- target -----
grammar a;

output ruleO { something
 like { bracketed; } test
}

rule ruleR {
        blabla
}

------------------  output ( line nums are wrong ) ------- 
Line 1 pos 0 type 12: 

Line 2 pos 0 type 9: grammar
Line 2 pos 7 type 12:  
Line 2 pos 8 type 9: a
Line 2 pos 9 type 10: ;
Line 2 pos 10 type 12: 

Line 3 pos 0 type 12: 

Line 4 pos 0 type 9: output
Line 4 pos 6 type 12:  
Line 4 pos 7 type 9: ruleO
Line 4 pos 12 type 12:  
Line 4 pos 13 type 4: {
Line 4 pos 14 type 6:  something
 like { bracketed; } test
}

rule ruleR {
	blabla
}

---------- lexer grammar -----------

lexer grammar MainLexer;

@header {
	package grammar.main;
}


@members {
	Integer bracketLevel = 0;
}

BRACKET_OPEN : '{' { bracketLevel++; };
BRACKET_CLOSE : '}' { bracketLevel--; };

ANY: {bracketLevel>0}?=> (
	(~('{' | '}') |
	'{' { bracketLevel++; } |
	'}' { bracketLevel--; }
	)+
	) 
    ;

GENERIC_ID : {bracketLevel==0}?=> ( LETTER | '_' ) (NAMECHAR)* ;

DC: {bracketLevel==0}?=> ';';

fragment NAMECHAR
    : LETTER | DIGIT | '.' | '-' | '_' | ':'
    ;

fragment DIGIT
    :    '0'..'9'
    ;

fragment LETTER
    : 'a'..'z'
    | 'A'..'Z'
    ;

WS  :  {bracketLevel==0}?=>
       (' '|'\r'|'\t'|'\u000C'|'\n' ) {channel=99;}
    ;


P.S a minor question: how to recieve token mnemo NAME, not numeric code (for 
debug) ?






More information about the antlr-interest mailing list