[antlr-interest] AntlrWorks 1.4.2 intepreter fails but generated Antlr3.3 C# parser works

Stefan Misch stefan.misch at gmx.de
Sat Jan 1 08:11:37 PST 2011


Hi,

I'm using my holdidays to get more in touch with ANTLR. As I now also have
the book I'm working through the samples in chapter 3 using C# as target
language. I noticed a difference between AntlrWorks 1.4.2 interpreter and
the code it generated.

Using the first grammar sample Expr.g without any actions the interpreter
can parse the simple expression "5-3" and build a graph. If I augment the
grammar with actions the interpreter fails with
"MismatchedTokenException(12!=4)" - token 12 is minus '-', 4 is NEWLINE. He
seems to be unhappy with the slightly altered rule syntax for "expr" in the
augmented version. If I use the same syntax for describing rule "expr" in
the simpler non-augmented grammar the AntlrWorks interpreter fails, too.

The interpreter works with this rule for "expr":

expr:	multExpr (('+'|'-') multExpr)*
	;

but fails with this:

expr:	multExpr 
	(	'+' multExpr
	|	'-' multExpr
	)*
	;

Please note that it can interprete "5+3" with both ways, just "5-3" fails.
Please also note that the ExprParser.cs file generated for the augmented
grammar with actions works, i.e. the correct result is printed, so the
possible bug is limited to AntlrWorks.

Thanks,
Stefan

Here is my complete grammar for Expr.g:

grammar Expr;

options {
	language=CSharp3;
	}

public prog
	:	statement+
	;

statement
	:	expr NEWLINE
	|	assignment NEWLINE
	|	NEWLINE
	;
	
assignment
	:	ID '=' expr
	;
	
expr
	:	multExpr (('+'|'-') multExpr)*
	;
	
multExpr
	:	atom (('*'|'/') atom)*
	;

atom
	:	INT
	|	ID
	|	LPAREN expr RPAREN
	;
	
INT		:	('0'..'9')+;
ID  		:	('a'..'z'|'A'..'Z'|'_')
('a'..'z'|'A'..'Z'|'0'..'9'|'_')*;
LPAREN	:	'(';
RPAREN	:	')';
NEWLINE	:	'\r'? '\n';
WS  		:	(' '|'\t')+ {Skip();};




More information about the antlr-interest mailing list