[antlr-interest] Problem parsing grammer for a label

John B. Brodie jbb at acm.org
Sun Aug 1 13:19:09 PDT 2010


Greetings!

On Sun, 2010-08-01 at 20:57 +0100, Gary Brown wrote:
> 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?

the input string `Transaction:` is a URL under your rules.

try `Transaction :`

it is probably just this stripped down example example, but since URL is
not used, just remove that rule...

> 
> Any guidance would be appreciated.
> 

Hope this helps...
   -jbb




More information about the antlr-interest mailing list