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

Davy Landman davy.landman at gmail.com
Fri Feb 26 23:46:09 PST 2010


Hi Kyle,

I've tried to make it clear in my emails I was not discussing the
interpreter part of ANTLRWorks, because messages warn you about the
differences, and as soon as you enable advanced stuff you'll notice
it.

I was talking about the Debugger tab in ANTLRWorks, which compiles the
grammar to a debug parser and lexer and creates a small java program
to run (__Test__.java) ANTLRWorks than offers stepping through the
lexing and parsing process. In this part of the program, you would
expect the same behavior compared to the release version of your
grammar file. So perhaps, it is a difference between this debug and
release version?

Davy

On Fri, Feb 26, 2010 at 22:52,  <kferrio at gmail.com> wrote:
> Terence has in the past acknowledged that the interpreter in antlrworks has some issues.  Now that Terence has publicly accepted stewardship of antlrworks perhaps he would appreciate a concise summary as a bug report.
>
> FYI. This is one reason I use the eclipse plugin even though I am not a java programmer.  The plugin is not perfect either but I think its better for some tasks.
>
> Kyle
> Sent from my Verizon Wireless BlackBerry
>
> -----Original Message-----
> From: Davy Landman <davy.landman at gmail.com>
> Date: Fri, 26 Feb 2010 19:41:53
> To: Ron Hunter-Duvar<ron.hunter-duvar at oracle.com>
> Cc: <antlr-interest at antlr.org>
> Subject: Re: [antlr-interest] Using ANTLR's ~ to do match literals causes
>        "no viable alternative" messages in generated Java but not in
>        ANTLRWorks
>
> 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.
>>>
>>>
>>
>
> 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