[antlr-interest] Problem parsing grammer for a label

Gary Brown gary at pi4tech.com
Sun Aug 1 12:57:36 PDT 2010


Hi

I have a stripped down grammer where I want to have a keyword, followed by
an ID, then ':' and then a block demarcated by '{' and '}'. The grammer is:

grammar ParserTest;

options {
    output=AST;
}

tokens {
    PLUS     = '+' ;
    MINUS    = '-' ;
    MULT    = '*' ;
    DIV    = '/' ;
    FULLSTOP = '.' ;
}

@header {
package test;
}

@lexer::header {
package test;
}

recurDef: 'recur'^ labelName ':'! blockDef ;
labelName: ID ;
blockDef: '{'! '}'! ;

ID : ('a'..'z'|'A'..'Z'|'_')('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ;
URL : (ID|':'|'?'|'/')+ ;
NUMBER    : (DIGIT)+ ;
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+     { $channel =
HIDDEN; } ;
fragment DIGIT    : '0'..'9' ;


When I used the generated parser against the following:

recur Transaction: {
}


I get the following error:

line 1:6 mismatched input 'Transaction:' expecting ID

Shouldn't the lexical analyser recognize the 'Transaction' part as an ID,
and then the colon as a separate character. If the ':'! is removed from the
rule, and the content changes to

recur Transaction{
}

it correctly detects the ID as being distinct from the following '{'
character. So why is the ':' being treated as a special case?

Any guidance would be appreciated.

Regards
Gary


More information about the antlr-interest mailing list