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

Kyle Ferrio kferrio at gmail.com
Sun Jan 2 09:40:39 PST 2011


Now this may really surprise you: If you have more than two alternatives, as
in

    rule: thing ( '*' thing | '/' thing | '%' thing | '#' thing ) *     //
not a great rule, but you get the idea

then you will find that the first, third and fourth alternatives are
interpreted correctly, and only the second alternative generates an error.
Strange, no?

This information does not constitute a solution.  Rather, this simply
underscores the fact that there are moving parts in the interpreter which
await fixing by someone familiar with the interpreter internals.  Meanwhile,
we users are grateful that the generated parsers work and that even the
interpreter can be tricked into working -- as you discovered -- by wrapping
the alternatives in extra parentheses as a workaround.

Kyle


On Sun, Jan 2, 2011 at 8:11 AM, Stefan Misch <stefan.misch at gmx.de> wrote:

>  Yes, only the first alternative of the rule works...
>
>  ------------------------------
> *Von:* Kyle Ferrio [mailto:kferrio at gmail.com]
> *Gesendet:* Sonntag, 2. Januar 2011 00:47
> *An:* Stefan Misch
> *Cc:* antlr-interest at antlr.org
> *Betreff:* Re: [antlr-interest] AntlrWorks 1.4.2 intepreter fails but
> generated Antlr3.3 C# parser works
>
>
> You may find that if you reverse the order of the '+' and '-' alternatives
> in the failing version of your rule, you may find that the minus case works
> and the plus case fails, i.e. the opposite of what you are seeing now.  If
> this hypothesis is correct, then you are probably seeing a quirky known
> issue in the antlr interpreter.  (I'm sorry, I do not recall the bug
> number.)  The first version of your rule works, apparently because the
> alternatives are "protected" by parens.  I hope this helps.
>
> Kyle
>
> On Sat, Jan 1, 2011 at 9:11 AM, Stefan Misch <stefan.misch at gmx.de> wrote:
>
>> 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();};
>>
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>


More information about the antlr-interest mailing list