[antlr-interest] Pls Help: Tree Grammar Bug? or Wrong usage? orUnderstanding Gap?

Kenneth Domino kenneth.domino at domemtech.com
Fri Aug 27 01:56:30 PDT 2010


> Tree Grammar Output =  {vs{vs} }

What's happening is that you are essentially synthesizing a complex string 
template
containing multiple <v>'s.  This gets evaluated eventually in main(),
which is why you see {vs {vs}}.  Instead, force the substitution
before returning a new template.  So your tree grammar will look like this:

tree grammar blockTree;

options {
  language = Java;
  output = template;
  tokenVocab = block;
  ASTLabelType = CommonTree;
}

@header {
//  package tg_bug;
}

rule: ^(BLOCK (vstring += v)* (sstring+=s)* ENDBLOCK) ->
      template(a={$BLOCK.text}, v={$vstring}, s={$sstring}, 
b={$ENDBLOCK.text})
<< <a><v><s><b> >>
;

v: 'v' -> template() "v";

s: 's' -> template() "s"
 | x=rule ->
      template(r={$x.st.toString()})
 << <r> >>
;

(I took the liberty to change the style of the template spec for "s", and I 
commented out
the 'package tg_bug' decl's to get this thing to compile and run for me. 
Packages-sigh.)
The key change is the template spec for the production "s->rule".  The 
"toString()" forces the
evaluation of the s.t. before creating a new one.

> However if I re-write the grammar as below, I don't have this problem.
>
> b->'{' '}'
>  | '{' v '}'
>  | '{' s '}'
>  | '{' v+ s+ '}'

Not sure which grammar you're referring to b/c there is a parse grammar and
a tree grammar.  Don't do this.  That's not the problem, and it looks ugly.

> RTFM

Reading never really helps. Always better to just debug the friggin' code 
and figure
out what it is doing.

Ken


 



More information about the antlr-interest mailing list