[antlr-interest] This should be simple, but I can't find the right syntax (or is it a bug?)

Stefan Mätje (d) Stefan.Maetje at esd-electronics.com
Fri Nov 4 03:49:09 PDT 2011


Hi,

I'm having a similar rule in my tree grammar that is simply collecting the 
output of other rules into a common StringTemplate. Therefore I think your
rule is correct so far. (The "problem" template is in a *.stg file.)

problem:
    ^('PROBLEM'	(decls+=mod_dcl|decls+=specification)*)
    -> problem(mDcl={$decls})
    ;

/** Declarations allowed on MODULE level */
mod_dcl
	:	v=var_dcl	{ $st = $v.st; }
	|	f=func_dcl	{ $st = $f.st; }
	|	p=proc_dcl	{ $st = $p.st; }
	;

But I'm using the Java output language and this compiles well. Being curious 
I've set my output language to CSharp3 and I've seen the same behaviour in the
C# source as you describe. Maybe indeed a bug in the C# code generation stuff.

Perhaps you could try to set your output language to Java to see if the 
generated source fits your expectations.

Regards,
	Stefan

Am 04.11.2011 01:59:03 schrieb(en) Voelkel, Andy:
> Hi,
> 
> I'm trying to collect all the StringTemplates created by all instances of 
> one
> rule (borg) into a StringTemplate for a containing rule (prog). But I can't
> seem to find the right syntax:
> 
> options {
>         output = template;
>         language = CSharp3;
>       tokenVocab=Tree1;
>         ASTLabelType = CommonTree;
> }
> 
> public prog
> : (b+=borg)+ -> template(vals = {$b})
> <<
> { // prog
>   <vals; separator = ",">
> }
> >> ;
> 
> The syntax I tried above is one many approaches I've tried - none seem to
> work. Antlr3 runs without error but the code won't compile. I hope the 
> intent
> is clear, as it is a pretty simple example. I've looked back and forth in
> Terence's book trying to find something like this, and in the examples, but
> so far I've struck out. It seems like a very common thing to try to do!
> 
> One strange thing is that when I look at the generated code, the list for b
> (list_b) is declared as List<CommonTree>. But Terence's books says is should
> be List<StringTemplate> when the output type is template. Later in the
> generated code, when it tries to add to the list, it tries to create a
> List<StringTemplate>, which seems right, but causes a compiler error. It
> almost seems like a bug, but I probably just don't understand.
> 
> Can anyone help me with this?
> 
> - Andy
> 



More information about the antlr-interest mailing list