[antlr-interest] why <if(y==2 )> in Stringtemplate does not work?

YINGAnnie yimm8369 at hotmail.com
Sun Mar 8 17:32:10 PDT 2009



 

 

okie, finally, the problem has been sovled. Thank you very much.

But there is an other problem, the original grammar of multiplicativeExpression is like this:

 

multiplicativeExpression
    :   unaryExpression ( (STAR|DIV|MOD) unaryExpression)*   ->^(MULTIPLICATIVE_EXPRESSION unaryExpression+)
    ;

 

It works fine, however, I want to match the STAR|DIV|MOD ,therefore I changed the multiplicativeExpression as follows:
multiplicativeExpression
    :   unaryExpression ->^(MULTIPLICATIVE_EXPRESSION unaryExpression)

    |  unaryExpression ( (STAR|DIV|MOD)^ unaryExpression)+

    ;

 

But the error of lookahead occurs and it consume lots of memory. 

 

In tree grammar ,the related code is as follows:

  multiplicativeExpression
    :^(MULTIPLICATIVE_EXPRESSION unaryExpression)

    |^(DIV unaryExpression+)
    |^(MOD unaryExpression+)  
    |^(STAR u1=unaryExpression u2=unaryExpression)   
   

Therefore, Could I have different roots in the same rule? like MULTIPLICATIVE_EXPRESSION^ and (STAR|DIV|MOD)^ ?

 

Annie

 
> CC: antlr-interest at antlr.org
> From: parrt at cs.usfca.edu
> To: yimm8369 at hotmail.com
> Subject: Re: [antlr-interest] why <if(y==2 )> in Stringtemplate does not work?
> Date: Sun, 8 Mar 2009 15:45:22 -0700
> 
> does String==int make sense?
> Ter
> On Mar 8, 2009, at 3:41 PM, YINGAnnie wrote:
> 
> >
> > like this?
> >
> > multiplicativeExpression
> >
> > :^(DIV unaryExpression+)
> > |^(MOD unaryExpression+)
> > |^(STAR u1=unaryExpression u2=unaryExpression) ->{$u2.text==2}? 
> > assgin(x={$u1.text},y={$u2.text},z={"<<"})
> > ;
> >
> >
> > The errors are still there.
> >
> >
> > Ann
> >
> > > CC: antlr-interest at antlr.org
> > > From: parrt at cs.usfca.edu
> > > To: yimm8369 at hotmail.com
> > > Subject: Re: [antlr-interest] why <if(y==2 )> in Stringtemplate 
> > does not work?
> > > Date: Sun, 8 Mar 2009 15:36:01 -0700
> > >
> > > remove if from predicate. it's an expression.
> > > ter
> > > On Mar 8, 2009, at 3:27 PM, YINGAnnie wrote:
> > >
> > > >
> > > > But there's some errors as the red color shows:
> > > >
> > > > additiveExpression
> > > >
> > > > : ^(ADDITIVE_EXPRESSION multiplicativeExpression+)
> > > > (106): reference to undefined rule: multiplicativeExpression
> > > > ;
> > > >
> > > >
> > > > multiplicativeExpression
> > > >
> > > > :^(DIV unaryExpression+) (106): reference to undefined
> > > > rule: unaryExpression
> > > > |^(MOD unaryExpression+) (106): reference to undefined
> > > > rule: unaryExpression
> > > > |^(STAR u1=unaryExpre ssion u2=unaryExpression) -
> > > > >{if($u2.text==2)}? assgin(x={$u1.text},y={$u2.text},z={"<<"})
> > > > ; (100): syntax
> > > > error: antlr: expecting REWRITE, found ';'
> > > >
> > > > unaryExpression
> > > > : ^(UNARY_EXPRESSION unaryOp? postfixExpression) (100):
> > > > syntax error: antlr: expecting SEMI, found ')'
> > > > ;
> > > >
> > > > If I delete ->{if($u2.text==2)}?
> > > > assgin(x={$u1.text},y={$u2.text},z={"<<"}) , all the errors are
> > > > gone. I am not sure what is the problem.
> > > >
> > > > Thanks.
> > > >
> > > > Ann
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > > CC: antlr-interest at antlr.org
> > > > > From: parrt at cs.usfca.edu
> > > > > To: yimm8369 at hotmail.com
> > > > > Subject: Re: [antlr-interest] why <if(y==2 )> in Stringtemplate
> > > > ; does not work?
> > > > > Date: Fri, 6 Mar 2009 15:56:11 -0800
> > > > >
> > > > >
> > > > > On Mar 6, 2009, at 3:46 PM, YingAnnie wrote:
> > > > >
> > > > > >
> > > > > > So, what I expect is that when the code is "a=b*c", and 
> > c==2, then
> > > > > > change * to /.
> > > > > >
> > > > > > multiplicativeExpression
> > > > > > :^(STAR u1=unaryExpression u2=unaryExpression) ->
> > > > > > assgin(x={$u1.text},y={$u2.text},z={"/"})
> > > > > > ;
> > > > > >
> > > > > > group T;
> > > > > > assgin(x,y,z)::= <<
> > > > > > <if(y==2)><x><z><y><else><"error"><endif>
> > > > > >
> > > > > > If I can not test the value of attributes within ST,Then it 
> > has to
> > > > > > be done in the tree grammar. can I do something like this:
> > > ; > > >
> > > > > > multiplicativeExpression
> > > > > > :^(STAR u1=unaryExpression u2='2') ->
> > > > > > assgin(x={$u1.text},y={$u2.text},z={"/"})
> > > > > > ;
> > > > >
> > > > > Almost. you need predicated -> alternatives
> > > > >
> > > > > -> {..}? template one
> > > > > -> {...}? template two
> > > > > -> else template
> > > > >
> > > > > T
> > > >
> > > > 把MSN装进手机,更多聊天乐趣等你挖掘! 立刻下载!
> > > > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > > > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> > >
> >
> > 立刻下载 MSN 保护盾,保障 MSN 安全稳定! 现在就下载!
> 

_________________________________________________________________
梦幻K图,百变造型,让你的照片与众不同,快来MClub试试吧!
http://club.msn.cn/?form=3
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090309/0833d7a3/attachment.html 


More information about the antlr-interest mailing list