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

Mark Volkmann r.mark.volkmann at gmail.com
Sun Jan 6 19:26:11 PST 2008


On Jan 6, 2008 5:31 PM, Fırat Küçük <firatkucuk at gmail.com> wrote:
> yes i need it.

I decided to take a shot at figuring out a solution to your grammar.
It should be easy right. Terr already gave the hint that the problem
is that it was greedily grabbing the DOT for FLOAT instead of leaving
it for the separator between the number and the identifier. Piece of
cake? Well I've tried several things I thought would work to no avail.
Why in the world doesn't this work?

grammar Sample;

options {
 backtrack = true; // I shouldn't need this, but I don't think it can hurt.
}

start
 // Using a syntactic predicate to order alternatives.
 : (INTEGER DOT IDENTIFIER)=> INTEGER DOT IDENTIFIER
 | FLOAT DOT IDENTIFIER  ;

FLOAT: NUMBER DOT NUMBER;
INTEGER: NUMBER;
IDENTIFIER: LETTER+;
DOT: '.';
fragment NUMBER: DIGIT+;
fragment LETTER: 'a' .. 'z';
fragment DIGIT: '0' .. '9';

> 07.01.2008 tarihinde Mark Volkmann <r.mark.volkmann at gmail.com> yazmış:
> > On Jan 6, 2008 5:23 PM, Fırat Küçük <firatkucuk at gmail.com> wrote:
> > > hi,
> > > the problem is related to following grammar:
> > >
> > > grammar Sample;
> > >
> > > start: (FLOAT | INTEGER) DOT IDENTIFIER;
> > >
> > > FLOAT: NUMBER DOT NUMBER;
> > > INTEGER: NUMBER;
> > > IDENTIFIER: LETTER+;
> > > DOT: '.';
> > > fragment NUMBER: DIGIT+;
> > > fragment LETTER: 'a' .. 'z';
> > > fragment DIGIT: '0' .. '9';
> > >
> > > let us try:
> > >
> > > 3.hello
> > >
> > > result:
> > > line 1:2 required (...)+ loop did not match anything at character 'h'
> > > line 1:3 mismatched input 'ello' expecting set null
> >
> > I think it's trying to match a FLOAT which requires a number after the
> > DOT. It should work if you change the start rule to this.
> >
> > start: INTEGER DOT IDENTIFIER;
> >
> > Do you really need to be able to parse something like this?
> > 3.14.hello

-- 
R. Mark Volkmann
Object Computing, Inc.


More information about the antlr-interest mailing list