[antlr-interest] Re: pretty printing with stringtemplate

Terence Parr parrt at cs.usfca.edu
Tue Jan 3 11:51:13 PST 2006


On Jan 3, 2006, at 12:54 AM, Arnulf Heller wrote:

> Hi there,
>
> have there been any thoughts to include such features directly into  
> the design of ANTLR?

Yes.  Prashant Deva and I are working on it and (per Mies' post, he  
has a nice solution too).

> Concerning pretty printing: What if ANTLR generates string  
> templates for every rule in the grammar? I'm not too familiar with  
> how stringtemplate works, but I think the return value of each rule  
> should be a instance of a string template.

That is how output=template mode works in ANTLR v3 :)  Of course you  
must fill in the attributes.

> So, if one has a rule like:
>
> ifrule : "if" LPAREN boolexpr RPAREN LCURLY stmts RCURLY;
>
> a proper string template might be (remove syntax errors):
>
> ifrule(boolexpr,stmts) ::= <<
> if ( $boolexpr$ )
> {
> 	$stmts$
> }
> >>

yes, and you'd write it like this in v3:

options {output=template;}

ifrule : "if" LPAREN boolexpr RPAREN LCURLY s=stmts RCURLY
	-> ifrule(boolexpr={$boolexpr.st}, stmts={$s.slist})
    ;

where stmts must return a list of templates:

stmts returns [List slist]
	:	(s+=stat)+ {$slist = $s;}
	;

stat	:	ifstat -> {$ifstat.st}
	|	ID '=' expr ';' -> assign(lhs={$ID.text}, rhs={$expr.st})
	;

or something like that.  I don't like some of that (too inconvenient)  
but it's ok for now until we learn more about usage patterns.

Terence


More information about the antlr-interest mailing list