[antlr-interest] MismatchedTokenException in loop

Indhu Bharathi indhu.b at s7software.com
Mon Oct 12 05:35:36 PDT 2009


You can get what you want by changing ‘msg_impl’ rule to 

 

msg_impl

                : ( (ID COLON)=> ID COLON)+ stmnt* 

                ;

 

This way the parser with look ahead to see if there is a COLON before it
matches the text with ‘ID COLON’.

 

Though the above solution will fix your problem, I’m not sure if this is an
elegant way of fixing this problem. I would vote for parser checking only
the syntax and the symantics handled by tree walkers. You can establish the
relation between messages while tree walking.

 

Cheers,

 

Indhu Bharathi

 <http://www.s7software.com/> S7 Software Solutions

 

 

From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Arne Schröder
Sent: Monday, October 12, 2009 3:17 PM
To: ANTLR-Mailing-List
Subject: [antlr-interest] MismatchedTokenException in loop

 

Hi,

I am trying to parse the following input:

[
   MSG1:
   MSG2: cmd1 cmd2
   MSG3: cmd3
]

which means on MSG1 and MSG2 commands cmd1 and cmd2 should be executed, on
MSG3 the command cmd3 should be executed.

I tried the following grammar:

grammar msg_test;    
msg_list: LSQUARE msg_impl* RSQUARE ;
msg_impl: (ID COLON)+ stmnt* ;
stmnt   : ID | INT ;
LSQUARE : '[' ;    
RSQUARE : ']' ;
COLON   : ':' ;
ID      : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
INT     : '0'..'9'+;
WS      : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;};

But this results in the following warning:
[10:59:35] warning(200): msg_test.g:3:20: Decision can match input such as
"ID" using multiple alternatives: 1, 2

When I run the parser I get a MismatchedTokenException on cmd2. 

When changing msg_impl by removing + to 

msg_impl: (ID COLON) stmnt* ;

the parser works fine (no warnings, the input is being parsed), but the
"connection" between MSG1 and MSG2 is lost and I cannot create an AST-Node
containing all messages that should be handled the same way.

It seems as if the COLON is not used in the stmnt* lookahead part to break
out of the loop. Why? Can I force it somehow?

Thanks for help and best regards
Christian

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091012/0355b6b2/attachment.html 


More information about the antlr-interest mailing list