[antlr-interest] Grouping nodes in the AST

Christopher Hunt huntc at internode.on.net
Wed Dec 22 14:59:13 PST 2010


Hi there,

I'm putting the final touches to my parser of Zoneinfo (1) files and haven't been able to solve the last part of the puzzle: how to group a set of AST nodes such that I can ensure the order by which nodes are processed in the tree grammar. The parser grammar I have should explain better:

line
	: (ruleLine | zoneLine | linkLine | NEWLINE!)*
	;

ruleLine
	: 'Rule' ruleName ruleFrom ruleTo ruleType ruleIn ruleOn ruleAt ruleSave ruleLetters NEWLINE!
	;

zoneLine
	: 'Zone' zoneName zoneGmtOffset zoneRules zoneFormat 
	(zoneUntil (NEWLINE+) zoneGmtOffset zoneRules zoneFormat)* 
	NEWLINE
	-> 'Zone' zoneName zoneGmtOffset zoneRules zoneFormat 
	(zoneUntil 'Zone' zoneName zoneGmtOffset zoneRules zoneFormat)* 
	;

linkLine
	: 'Link' linkFrom linkTo NEWLINE!
	;

In essence, I need to group all of the rule lines together, and all of the zone lines together so that all rules are processed by my tree grammar first, then all zones are processed and finally all links are processed. This is because zones reference rules and links reference zones.

Zoneinfo source files can have rules, zones and links appear in any order hence my need to ensure that the AST outputs rules first then zones then links.

I was thinking that referencing a previous rule as described in Terence's book on p174 was the way to go, but I've not been able to make that work.

All help appreciated.

Kind regards,
Christopher

(1) http://en.wikipedia.org/wiki/Tz_database


More information about the antlr-interest mailing list