[antlr-interest] Mismatched Token with String and Newline

Robert.Klaus at innovations.de Robert.Klaus at innovations.de
Fri Oct 26 02:25:05 PDT 2007


Hi,

I have a problem with lexing Strings and I'm thinking I have done
something wrong. When entering the grammar below in ANTLRWorks and use
the interpreter with the input "\n" it works fine. When I enter a line
break after the "\n" I get a MismatchedTokenException.

In a unit test, I parsed this input:
String input = "\"\\n\"\r\n";

And ANTLR responded with:
line 1:1 no viable alternative at character '\'
line 2:0 mismatched character '<EOF>' expecting '"'

I compared this to the Java.g in the examples and it seem to be quite
similar, so I'm quite clueless now.

Can anybody help me find the mistake?

Thanks in advance,

Robert



grammar testMe;

options
{
	language = 'Java'
}

parse
:
foo EOF
;

foo	:
	STRING	
	;



STRING
:
'"' (CHARACTER | ~('\\'|'"'))* '"'
	;
	
fragment CHARACTER
	:
		
		('\\' ('n'|'t'|'r'|'\\'|'"'))		
		| ('\\u' HEX_CHAR HEX_CHAR HEX_CHAR HEX_CHAR)
	;
	
fragment HEX_CHAR
	:
		DIGIT | 'A'..'F'
	;
	
fragment DIGIT 
	:	
		'0'..'9'
	;
	
NEWLINE :	'\r'? '\n' ;
WS 	:	(' ' | '\t' | '\f')+ ;

-------------------------------------------------------------
Robert Klaus
Diplom Informatiker
Product Development
-------------------------------------------------------------
Innovations Softwaretechnologie GmbH
Ziegelei 7
88090 Immenstaad
Germany
Phone: +49(0)7545-202-339
robert.klaus at innovations.de
www.visual-rules.com
-------------------------------------------------------------
Executives: 
Achim Berger, Thomas Cotic, Walter Pitz
Register Court Ulm HRB 631622
-------------------------------------------------------------


More information about the antlr-interest mailing list