[antlr-interest] Parsing question

Jim Idle jimi at temporal-wave.com
Thu Aug 2 09:21:49 PDT 2012


OK - your example was not clear enough. You do need a fragment there.

However it sounds like you are trying to get the lexer to handle negative
numbers and that is usually the wrong way - you want to handle that in the
parser's expression tree. However, I might be tempted to handle the date
literal in the lexer rather than the parser as you will otherwise create a
lot of conflicts.


MINUS : '-';
fragment DATE :;
INTEGER : '0'..'9'+
          (('-' '0'..'9'+ '-' '0'..'9')=>('-' '0'..'9'+ '-' '0'..'9'+) {
$type = DATE; })?
;

Are you sure that your language allows date strings that are not quote
delimited? There is an obvious conflict with the subtract operator unless
there are separate expression trees based on context.

Jim

> -----Original Message-----
> From: Vinay Pandit [mailto:vpandit at quantivo.com]
> Sent: Wednesday, August 01, 2012 11:14 PM
> To: Jim Idle; antlr-interest at antlr.org
> Subject: RE: [antlr-interest] Parsing question
>
> Thanks for the reply. That did not work either.
>
> Regards
> Vinay
>
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jim Idle
> Sent: Wednesday, August 01, 2012 10:48 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Parsing question
>
> That should be:
>
> fragment
> DIGIT ....
>
> And you don't need separate parser rules for yearValue and the other
> two - they are the same thing, just use UNSIGNED_INTEGER directly.
>
> Jim
>
> > -----Original Message-----
> > From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> > bounces at antlr.org] On Behalf Of Vinay Pandit
> > Sent: Wednesday, August 01, 2012 9:44 PM
> > To: antlr-interest at antlr.org
> > Subject: [antlr-interest] Parsing question
> >
> > I am trying to parse a date time literal in ANTLR and I am having
> > issues with the grammar.
> >
> > Here are the rules defined in the parser
> >
> > dateValue : ( yearValue MINUS monthValue MINUS dayValue);
> >
> > yearValue : datetimeValue ;
> >
> > monthValue : datetimeValue;
> >
> > dayValue : datetimeValue;
> >
> > datetimeValue : UNSIGNED_INTEGER;
> >
> > The Lexer has
> >
> > MINUS         : '-' ;
> > DIGIT : ('0'..'9');
> > UNSIGNED_INTEGER : (DIGIT) +;
> >
> >
> > When I parse a date like 2012-01-01 for the dateValue rule, the
> parser
> > throws an exception.
> >
> > com. qexpr.ParseException: line 1:4 - mismatched input '-01'
> expecting
> > MINUS
> >                at
> >
> com.quantivo.qexpr.AbstractQParser.reportError(AbstractQParser.java:77)
> >                at
> > com.quantivo.qexpr.SQLGrammar.dateValue(SQLGrammar.java:4730)
> >                at
> >
> com.quantivo.qexpr.model.SQLGrammarTest.testDateValue(SQLGrammarTest.j
> > a
> > va:25)
> > ...
> >
> > Looking at the error message it is obvious that I am not getting the
> > Minus token. Instead the internal token that I get is an INTEGER
> > (signed). I tried the greedy=false option, but that did not seem to
> > help either. I am running out of ideas as to why the input does not
> > match. Obviously I am doing something wrong, but I am not sure what!
> >
> > Regards
> > Vinay
> >
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-
> interest/your-
> > email-address
>
> 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