[antlr-interest] Using ANTLR's ~ to do match literals causes "no viable alternative" messages in generated Java but not in ANTLRWorks

Davy Landman davy.landman at gmail.com
Fri Feb 26 11:34:14 PST 2010


Applying $channel=hidden on a fragment does indeed cause a compiler
error, but that was not the error I was refering to.

I was refering that the ANTLR code example I provided without the WS
hidden channel in ANTLRWorks does works correctly and outputs no
error's at all to the console.

When the same grammar file is used in java (via Generate Code)
directly using the same code as the debugger from ANTLRWorks used
(__Test__.java) the java version outputs error to the console.

So my idea is that the debugger in ANTLRWorks connects to the process
via a network stream, and the "line 1:3 no viable alternative at
character ' '   " is not reported back over the network stream, but
reported in the java console in which the __Test__ application runs.
If this is the case, the debugger can give a false sense of working
code. Everything works nicely, but in your actual application you get
(uncatchable?) error messages.

Davy

On Fri, Feb 26, 2010 at 19:53, Ron Hunter-Duvar
<ron.hunter-duvar at oracle.com> wrote:
> I would have expected that setting $channel=HIDDEN in a fragment rule would
> have given an error (either a compile or run time), since fragments don't
> generate tokens, so there should be no token to set the channel on. Perhaps
> not strictly an Antlr bug, but it would be good if there were an error
> warning message for this during generation.
>
> Ron
>
>
> Davy Landman wrote:
>
> Hi ANTRL mailing list,
>
> So Ron's solution worked very nice. And I should have thought of it
> myself, but in my opinion the fact that these messages do not show in
> the ANTLRWorks debugger+console is confusing. Is there a reason behind
> it? Or is it perhaps a defect?
>
> Davy
>
> On Fri, Feb 26, 2010 at 09:36, Davy Landman <davy.landman at gmail.com> wrote:
>
>
> Hi Ron,
>
> Thanks, I've been applying the channel=HIDDEN on the WS_CHAR fragment
> level, and that messed up everything, but using this extra token works
> perfectly.
>
> Cheers,
> Davy
>
> On Fri, Feb 26, 2010 at 01:54, Ron Hunter-Duvar
> <ron.hunter-duvar at oracle.com> wrote:
>
>
> Hi Davy,
>
> You don't have a rule for consuming whitespace. Add something like this:
>
> WS :
>  WS_CHAR+ {$channel=HIDDEN;}
>  ;
>
> fragment WS_CHAR :
>  (' '|'\r'|'\t'|'\u000C'|'\n')
>  ;
>
> Ron
>
>
> Davy Landman wrote:
>
>
> Hello,
>
> I'm having some problems with my generated java parser/lexer, but I've
> tried to reduce the problem to a small subset.
>
> Let's assume the simple language which has identifiers and numbers.
> And the only rules are, a number contains out of only numbers, and a
> identifier can not contain `'?', ':', ' '` and can not begin with a
> number.
>
> I would create the following ANTLR specification of this language.
>
>    grammar simple;
>
>    prog:       expr* EOF;
>
>    expr
>        :       ID | INT;
>
>
>    ID  : ~('0'..'9'|SEPERATORS) ~(SEPERATORS)*;
>
>    fragment
>    SEPERATORS
>        :       ' ' | ':' | '?';
>
>    INT :       '0'..'9'+
>        ;
>
> And running this in ANTLRWorks debugging and parsing mode, no errors
> are reported to the console.
>
> But if I than generate the Parser and Lexer and try to consume it in a
> java program as such:
>
>    import org.antlr.runtime.ANTLRStringStream;
>    import org.antlr.runtime.CommonTokenStream;
>    import org.antlr.runtime.RecognitionException;
>
>
>    public class runner {
>
>        public static void main(String args[])
>        {
>                simpleLexer lex = new simpleLexer(new
> ANTLRStringStream("a33 44"));
>                CommonTokenStream tokens= new CommonTokenStream(lex);
>                simpleParser parser = new simpleParser(tokens);
>                try {
>                        parser.prog();
>                } catch (RecognitionException e) {
>                        e.printStackTrace();
>                }
>        }
>    }
>
> The parser works, and the prog() continues succesfully. But in the
> background the following error message is printed to the console.
>
>    line 1:3 no viable alternative at character ' '
>
> The same exact sequence causes no messages to the ANTLRWorks console,
> so I was wondering, am I abusing the ~ ? Or is there a difference
> between the ANTLRWorks debugger and default java runtime?
>
> So if anybody can help me how to get rid of these messages? Because in
> the original lexer and parser where this question is based, I get a
> nice looking AST but only have messages printed to the eclipse
> console.
>
> Versions used:
> ANTLRWorks 1.3.1
> ANTLR-3.2
> Linux x64
>
> Kind regards,
> Davy Landman
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
>
>
>
> --
> Ron Hunter-Duvar | Software Developer V | 403-272-6580
> Oracle Service Engineering
> Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5
>
> All opinions expressed here are mine, and do not necessarily represent
> those of my employer.
>
>
>
>
>
>
> --
> Ron Hunter-Duvar | Software Developer V | 403-272-6580
> Oracle Service Engineering
> Gulf Canada Square 401 - 9th Avenue S.W., Calgary, AB, Canada T2P 3C5
>
> All opinions expressed here are mine, and do not necessarily represent
> those of my employer.
>


More information about the antlr-interest mailing list