[antlr-interest] Parsing a time expression

Rick Mann rmann at latencyzero.com
Sat Apr 24 06:06:47 PDT 2010


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



More information about the antlr-interest mailing list