[antlr-interest] antlr beginner confused over ambiguities
Sam Barnett-Cormack
s.barnett-cormack at lancaster.ac.uk
Wed Feb 25 08:35:32 PST 2009
Rick Schumeyer wrote:
> This works, but it gives me one token containing "12:34:56". I still
> need to parse the hh,mm,ss values. What is the best way to do that?
>
> It seems that I want the parser to be able to tell the lexer "normally
> you can consider INT:INT:INT as a STRING. But right now, return tokens
> for each piece." Isn't that what a semantic predicate does? Or am I
> really missing something?
Well, you *could* do something funky with multiple-token emitting and so
on, I suppose, but my immediate reaction would be "hh:mm:ss is a
fixed-length, fixed-delimeter format. I can do that with a split
function in the target language". It all depends on what you want to
consider a "token". From a parse point of view, is a time like that an
indivisible entity, or three INTs with a delimeter?
Sam
> On Tue, Feb 24, 2009 at 3:32 PM, Gavin Lambert <antlr at mirality.co.nz
> <mailto:antlr at mirality.co.nz>> wrote:
>
> At 08:52 25/02/2009, Rick Schumeyer wrote:
>
> If the input string is "12:34:45", then of course antlr returns
> a STRING token. I understand why it does that (I think) but I'm
> not really sure how to fix that. I'm fuzzy on the difference
> between a semantic and syntactic predicate, and whether one of
> those is what I need.
>
> grammar STest;
>
> line : timestamp;
>
> timestamp
> : INT ':' INT ':' INT
> ;
>
> INT : ('0'..'9')+;
>
> STRING : ('!'..'+' | '-'..'~')+; /* anything but ws or
> comma */
>
>
> fragment TIMESTAMP: INT ':' INT ':' INT ;
>
> INT : ('0'..'9')+ ;
>
> STRING
> : (TIMESTAMP) => TIMESTAMP { $type = TIMESTAMP; }
>
> | ('!'..'+' | '-'..'~')+; /* anything but ws or comma */
> ;
>
>
>
> ------------------------------------------------------------------------
>
>
> 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