[antlr-interest] Structuring back-end of translator

Tom Nurkkala tnurkkala at cse.taylor.edu
Fri Aug 14 09:10:33 PDT 2009


I am writing a translator that takes high-level queries and generates  
code (e.g., Python, LLVM) that can find records within hierarchical  
data.  For example, hierarchical employee data might take the  
following format:

company {
	name: acme corp, llc
	url: ac.me
	division {
		name: r&d
		employee {
			id : 1551
			person {
				ssn: 123-12-1234
				name {
					first: Fred
					last: Ziffle
				}
				role: programmer
			}
		}
		employee {
			id : 1555
			person {
				ssn: 111-11-1111
				name {
					first: Zelda
					last: Ziffle
				}
				role: manager
			}
		}
		... more employees ...
	}
	division {
		name: it
		employee { ... }
		... more employees ...
	}
}

I'd like to be able to query this data structure with something like  
the following:

	select employee where role = programmer

The translator would take this query and return a program that would  
list all the employee records having a role of programmer.  A simple  
translation of this query would be something like:

	within company
		for each division
			for each employee
				look up role
				if role = 'programmer'
					output entire employee record

Assume that the translator can infer that an employee is within a  
division is within a company, and that a role is an attribute of an  
employee (e.g., these might all be unique attributes within the  
hierarchy).

I have a parser that translates the query into a Common AST, and a  
tree walker that traverses the resulting AST.  I would like to be able  
to use String Template to generate the translation.  However, there is  
a fair amount of processing that needs to be done (e.g., finding the  
connection between company and role in order to infer the necessary  
nested loops) and then this information needs to somehow drive an ST  
group.

What's the best way to structure the tree walker and ST group?  Can I  
leverage template invocation within the ST group to handle the  
generation of multiply nested "for each" clauses in the output above?   
Or do I need to do the heavy lifting in custom output code that  
invokes simpler templates?  Or do I turn to a custom AST (e.g.,  
heterogeneous AST pattern)?  Or something else entirely?

thanks,
tom.



More information about the antlr-interest mailing list