[antlr-interest] Noob Question

Nik Molnar nikmd23 at gmail.com
Tue Jan 12 17:52:48 PST 2010


Hello all,

I am rather new to ANTLR and seem to be running into a small issue I can't
figure out.

I'm writing a very simple grammar based on many tutorials online, the
calculator.

This grammar generates C# code that compiles perfectly, and works for the
most part in ANTLRWorks Interpreter, Debugger and in a sample app I made in
.NET to call the generated Parser/Lexer.

The problem I run into is what I put in invalid syntax, expecting an error.
Output like so:

Valid Syntax: "3+3" => Works in interpreter, debugger and compiled .net
code.
Invalid Syntax: "3+/3" => Gives error in interpreter, debugger and compiled
.net code, as expected.
Invalid Syntax: "3_3" => The interpreter shows nothing, the debugger cannot
connect and the .net code hangs for a while then throws an out of memory
exception.

I'm sure I'm doing something wrong in my grammar but don't know what.

I've included it below. Please help me!

Thanks,

grammar Test;

/*options
{
language = 'CSharp2';
}*/

expression
    : amExpression;

amExpression
    :mdExpression ((PLUS|DASH) mdExpression)*
    ;

mdExpression
    :INT ((STAR|SLASH) INT)*
    ;

DASH
    :'-'
    ;

SLASH
    :'/'
    ;

WS
    : (' '
    | '\t'
    | '\n'
    | '\r')*
    { $channel = HIDDEN; }
    ;

STAR
    : '*'
    ;

PLUS
    : '+'
    ;

fragment DIGIT
    : '0'..'9'
    ;

INT
    : (DIGIT)+
    ;


More information about the antlr-interest mailing list