[antlr-interest] String template conundrum

Bill Andersen andersen at highfleet.com
Mon May 3 15:21:20 PDT 2010


Hi folks

I have a grammar I'm working on for a pattern-matching language with string template output that is meant to generate java code to do the matching.  Here's an example (partial) production:

pattern
	: LP op=ID left=pattern right=pattern RP ->
	  matchBinOp(name0={ps.genName();}, op={$op}, 
	    name1={$left.st.getAttribute("name0")}, 
	    name2={$right.st.getAttribute("name0"})
	 // ???
	;

[ Note ps.genName() is just a name generator method in some support code for the parser ]

I want the output Java code to look like this

// Generated by matchBinOp template
boolean match1(...) {
   ... match2(...)
   ... match3(...)
}

// Generated by template for $left
boolean match2(...) { ... }

// Generated by template for $right
boolean match3(...) { ... } 

So the question is, how do I get all three methods in the output (the above production only generates the first method)?  Both $left and $right are (recursive) calls to the same production but each can (recursively) generate its own methods.  I want to get from that to a linear list of method in the class code.

I have an (inelegant) workaround that involves adding parameters to matchBinOp (and similar templates) to stitch the code together, eg.

	  matchBinOp(name0={ps.genName();}, op={$op}, 
	    name1={$left.st.getAttribute("name0")}, 
	    name2={$right.st.getAttribute("name0"})
	    code1={$left.st}
	    code2={$left.st})

but I don't like this solution and am looking for something better.

Thanks for any help

	.bill

Bill Andersen
Highfleet, Inc. (www.highfleet.com)
3600 O'Donnell Street, Suite 600
Baltimore, MD 21224
Office: +1.410.675.1201
Cell: +1.443.858.6444
Fax: +1.410.675.1204







More information about the antlr-interest mailing list