[antlr-interest] Parsing a time expression

Rick Mann rmann at latencyzero.com
Sat Apr 24 13:35:26 PDT 2010


On Apr 24, 2010, at 09:11:04, Cliff Hudson wrote:

> So you basically have two types of expressions, those that start with INT
> '/' and those that don't.  So this would look like:
> 
> time_expr
> 	:	INT '/' time_ltr
> 	|	time_rtl
> 	;
> 
> time_rtl
> 	: 	(INT ':' (INT ':')?)? INT '.' INT;
> 
> time_ltr
> 	: 	INT (':' INT (':' INT ('.' INT)?)?)?
> 	;
> 	
> Is this what you tried and it failed?

That seems to work, thank you. Not sure anymore what I had that was failing. I've modified it a bit to make it an AST generator, by putting '!' after the lexical tokens, and changed the INT '.' INT parts to be (INT | FLOAT), and it still parses in ANTLRWorks.

Now I'm trying to write the tree parser for it, and I've got this at one point, but I'd like to avoid writing $val = Integer.parseInt($INT.text) over and over. Is there any way to do that?

tree grammar TimeEval;

options
{
	tokenVocab=TimeInterval;
	ASTLabelType=CommonTree;
}

interval returns [float val]
	: INT intLTR;

intLTR returns [float val]
	:INT	{ $val += 24.0 * 3600.0f * Integer.parseInt($INT.text); }
	(INT	{ $val +=        3600.0f * Integer.parseInt($INT.text); }
	(INT	{ $val +=          60.0f * Integer.parseInt($INT.text); }
	(INT	{ $val +=                  Integer.parseInt($INT.text); }
	|FLOAT	{ $val +=                  Float.parseFloat($FLOAT.text); }
	)?)?)?
	;

TIA,
Rick

> 
> 
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Rick Mann
> Sent: Saturday, April 24, 2010 6:07 AM
> To: antlr-interest Interest
> Subject: [antlr-interest] Parsing a time expression
> 
> I posted a couple related questions earlier, but now I'm down to a more
> fundamental question.
> 
> I'm trying to use a complete lexer/parser/tree parser. I'd like to support
> two types of expressions representing time intervals. In the end, they
> evaluate to a value representing seconds. The two types look like this:
> 
> 1)	15/
> 2)	15/ 23
> 3)	15/ 23:12
> 4)	15/ 23:12:07
> 5)	15/ 23:12:07.2
> 
> and
> 
> 6)	7.2
> 7)	12:07.2
> 8)	23:12:07.2
> 
> The main difference is that if the expression starts with INT '/', then it's
> built up left-to-right with each value representing days, hours, minutes,
> and seconds, respectively. If there is no '/' in the expression, it's built
> up right-to-left, with seconds in the right-most position.
> 
> I'm having trouble conceptualizing what the grammar really should look like,
> and how the tree parser would look. When I try to write stuff out in the
> form of INT '/'! (INT (':'! INT)?)?, I get lots of "matches more than one
> alternative" warnings.
> 
> OTOH, one can think of these as arithmetic expressions. Considering example
> 5 above, it would be:
> 
> 	  24 * 3600 * 15
> 	+      3600 * 23
> 	+        60 * 12
> 	+              7.2
>        ------------------
>                 1379527.2
> 
> But I can't figure out how to build the tree that accounts for the position
> of each element to allow all the alternatives 1 - 5.
> 
> Thanks for any guidance.
> 
> -- 
> Rick
> 
> 
> 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