[antlr-interest] a simple (not for me :)) grammar problem

Mark Volkmann r.mark.volkmann at gmail.com
Mon Jan 7 14:15:23 PST 2008


On Jan 7, 2008 2:35 PM, Fırat Küçük <firatkucuk at gmail.com> wrote:
> no,
> this is what i did.
>
> this grammar parses "3     .    4    . hello".

The solution I emailed out doesn't parse that because it doesn't skip
whitespace. Well, I should be more clear. This is the output I get.

     [java] line 1:1 no viable alternative at character ' '
     [java] matched!
     [java] line 1:2 no viable alternative at character ' '
     [java] line 1:4 no viable alternative at character ' '
     [java] line 1:5 no viable alternative at character ' '
     [java] line 1:8 no viable alternative at character ' '
     [java] line 1:9 no viable alternative at character ' '
     [java] line 1:11 no viable alternative at character ' '
     [java] line 1:12 no viable alternative at character ' '

So you see I get the message "matched!", but I also get all the "no
viable alternative" messages. What we need is a way to make the parser
stop when it gets one of those. I think this is addressed in section
10.4. I'll try that and let you know what happens.

> so as gavin said.
>
>
> "It's not a solution if it doesn't work :)"
>
>
>
>  2008/1/7, Mark Volkmann <r.mark.volkmann at gmail.com>:
>
> > On Jan 7, 2008 6:24 AM, Gavin Lambert <antlr at mirality.co.nz> wrote:
> > > At 21:20 7/01/2008, =?ISO-8859-9?Q?F=FDrat_K=FC=E7=FCk?= wrote:
> > >  >
> > >  >this is my simple solution:
> > >
> > > It's not a solution if it doesn't work :)
> > >
> > > Try doing what I suggested.  You really should handle the floats
> > > in the lexer, since you don't have to worry about whitespace
> > > weirdness that way.  And if you do it the way I said, it should
> > > work.
> >
> > I think this is what you want or at least really close.
> >
> > grammar Sample;
> >
> > start
> >   options { backtrack = true; }
> >   : (floatValue | integerValue) DOT IDENTIFIER
> >     { System.out.println("matched!"); }
> >   ;
> >
> > floatValue: NUMBER DOT NUMBER;
> > integerValue: NUMBER;
> >
> > DOT: '.';
> > IDENTIFIER: LETTER+;
> > NUMBER: DIGIT+;
> > fragment LETTER: 'a'..'z';
> > fragment DIGIT: '0'..'9';
> >
> > NEWLINE: '\r'? '\n' { skip(); };
> >
> > --
> > R. Mark Volkmann
> > Object Computing, Inc.
> >
>
>
>
>
> --
> Öğr. Gör. Fırat Küçük
> ADAMYO Distance Learning
> SAKARYA University / TURKEY



-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list