[antlr-interest] Re: Resolving ambiguity with dates and division operations

Matthew Ford Matthew.Ford at forward.com.au
Wed Apr 16 05:49:03 PDT 2003


Yes you are correct,
I remember now I did not have to handle  division.
So you are back to doing real work in the parser
just return the digits  (2002) and '/'  etc
and in the parser you will have to put them together when the parser gets to
the point where it is looking for a date.

date
 : i1:INT '/' i2:INT '/' i3:INT
{ // process i1, i2, i3 here to form a date in internal form
  // eg  String dataString = i1.getText()+"/"+i2.getText()+"/"+i3.getText();
}
 ;

You still have to decide when you expect a date and when you expect  number
/ number
Basically a date is a special form of constant, like an integer.
Constants can occure (in general) at the inner most level of expressions.

divExpression:unaryIntExpression
 ( (DIV^ | DOT_DIV^ | DOT_STAR_DOT^ | DOT_STAR^ | STAR_TILDA^ | STAR^ )
   unaryIntExpression  )*;

protected
unaryIntExpression
 : (PLUS^ | MINUS^)* primaryIntExpression
 ;

// the basic element of an expression
primaryExpression
 :   constant
 | LPAREN! expression RPAREN!
 ;



The question is how do you decide between a constant and a division
expression.
You use Syntactic Predicates

protected
divExpression_or_Date
 : (date_time) => date
 | (division) => div_expression


Note that the predicate rules (in this case, date_time or division) do not
have to be real rules the parse will finally use.
They only have to express the required sequence of tokes to decide between
dates and divisions.
The exact form depends on your language syntax.
Basically write down by hand how you yourself would decide between the two
case and then form the Syntactic Predicates
from that.

I know this is not a complete solution but I hope this helps.
matthew
----- Original Message -----
From: "oneway_111" <oneway_111 at yahoo.com>
To: <antlr-interest at yahoogroups.com>
Sent: Wednesday, April 16, 2003 9:05 PM
Subject: [antlr-interest] Re: Resolving ambiguity with dates and division
operations


> --- In antlr-interest at yahoogroups.com, "Matthew Ford" >
> ...
> > // a numeric literal date, time, int, float, hex or oct
> > DATE_TIME_INT_FLOAT
> >  : (DIGIT DIGIT DIGIT DIGIT '/') => DATE {_ttype = DATE;}
> >  | (DIGIT (DIGIT)? ':') => TIME {_ttype = TIME;}
> >  | (INT '.') => FLOAT {_ttype = FLOAT;}
> >  | INT {_ttype = INT;}
> >  | ('0' 'x')=> HEX {_ttype = HEX;}
> >  ;
> ...
>
> Thanks, that was helpful.
>
> I'm concerned though that
> (DIGIT DIGIT DIGIT DIGIT '/') => DATE {_ttype = DATE;}
> will not work when, say, you want 2002 divided by 4 (2002/4), will it?
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list