[antlr-interest] The right way to invoke templates with nullarguments?

Diehl, Matthew J matthew.j.diehl at intel.com
Thu Jun 21 18:02:39 PDT 2007


>(have to have a cumbersome conditional in there).

I've decided (I'm new though) that the best way to setup your
'cumbersome conditional' is whenever there's a conditional part, set it
equal to something (s2 like you did below), and then in the
StringTemplate section, define it as:

stat2={s2!=null?$s2.st:false}

Instead of false, you can also probably try null.  This allows you so
that when you write the group file, <if(stat1)>other stuff<stat1><endif>
can be used as well.  When I was setting it as:
stat2={s2!=null?$s2.st:""} it would always return true for
<if(stat1)><endif> since it was not null or false, and I would have to
make another variable called hasStat2 that was a boolean....  Anyway, if
there're any better ideas, I'd like to know, but this has been working
for me, and it's not too much of a work around.

Matt


-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of Terence Parr
Sent: Thursday, June 21, 2007 5:26 PM
To: antlr-interest
Subject: Re: [antlr-interest] The right way to invoke templates with
nullarguments?


On Jun 21, 2007, at 8:57 PM, Tim Clark wrote:

> Hi
>
> My application is outputting 'assembler code' for a stack machine.
> Corresponding to this tree parser rule:
>
> -------------A------------------
> ifStatement
>     :
>     ^(IF_STAT e=expr s1=statement s2=statement?)
>      -> ifStat(
>              expr={$e.st},
>              stat1={$s1.st},
>              stat2={$s2.st},
>              L1={myHelper.nextLabel ()},
>              L2={myHelper.nextLabel()}
>          )
> -------------------------------
>

Hi. the problem is that s2 may be null since it's optional. I've not  
got a good way to say $s2.st in the {...} w/o getting an error (have  
to have a cumbersome conditional in there).  I've got a bug report in  
about this.

Ter


More information about the antlr-interest mailing list