[antlr-interest] Parsing a time expression

Cliff Hudson cliff.s.hudson at gmail.com
Sat Apr 24 09:11:04 PDT 2010


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?


-----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