[antlr-interest] Multiple string template rule

Jim Idle jimi at temporal-wave.com
Tue Oct 25 13:58:15 PDT 2011


In this rule:

unaryExpr
      :        ((PLUS|MINUS)?) valueExpr
      |        NOT valueExpr -> template(a={$valueExpr.text}) "not <a>"
      ;

Your first alt is not producing a template. It is also likely that you do
not want left associativity with unary expressions but want:

unaryExpr
      :        PLUS u=unaryExpr -> template(a={$u.text}) "+ <a>"
      :        MINUS u=unaryExpr -> template(a={$u.text}) "- <a>"
      |        NOT u=unaryExpr -> template(a={$u.text}) "not <a>"
	  | v=valueExpr  { $st = $v.st; }
      ;


So, I would double check your alts and/or post the entire grammar.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Tim Poole
> Sent: Tuesday, October 25, 2011 1:37 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Multiple string template rule
>
> Yes, my other re-writing rules have a similar structure. This is an
> example of one that's working:
>
> unaryExpr
>      :        ((PLUS|MINUS)?) valueExpr
>      |        NOT valueExpr -> template(a={$valueExpr.text}) "not <a>"
>      ;
>
> And a conditional one:
>
> generalComp
>      :   gc=GENERAL_COMP
>      -> {($gc.text).equals("=")}? template() "=="
>      -> template(a={$gc.text}) "<a>"
>      ;
>
> On 25/10/2011 19:55, Jim Idle wrote:
> > Are all your rules either setting $st before they return or creating
> a
> > template with ->template(.... or similar? Producing the template is
> > not automatic.
> >
> > Jim
> >
> >> -----Original Message-----
> >> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> >> bounces at antlr.org] On Behalf Of Tim Poole
> >> Sent: Tuesday, October 25, 2011 10:00 AM
> >> To: antlr-interest at antlr.org
> >> Subject: Re: [antlr-interest] Multiple string template rule
> >>
> >> :$
> >>
> >> That's a bit embarrasing!
> >>
> >> OK, so I am actually trying to do a similar thing for both the OR
> >> statements and the AND statements.
> >>
> >> What I didn't explain was that the "<bf ...>" expression isn't
> >> contributing anything, so it looks like the "b+=andExpr" isn't
> >> catching anything from the token.
> >>
> >> There is an example rule including "( p+=parameterDef )*" but this
> is
> >> slightly different to my "(OR b+=andExpr)*".
> >>
> >> I suspect that I probably need something closer to "<bf:{ expr |
> \|\|
> >> <expr>  }>".
> >> I have tried something similar, but the lack of data in bf seems to
> >> be the limiting factor.
> >>
> >> I'm hoping it's possible, just that I don't understand how to do it,
> >> yet.
> >>
> >> Any thoughts gratefully received...
> >>
> >> I'm trying to convert:
> >>
> >> rule != "working" or mental_state == "distressed"
> >>
> >> into:
> >>
> >> rule != "working" || mental_state == "distressed"
> >>
> >> Obviously, this snippet of grammar doesn't cover the expressions
> >> (andExpr) on either side of the "or", but I hope it gives you a
> >> flavour of what I'm trying to achieve.
> >>
> >> orExpr
> >>        : a=andExpr (OR b+=andExpr)* ->   template(a={$a.text},
> bf={$b})
> >> "<a><bf; separator=\" || \">"
> >>        ;
> >>
> >> Regards,
> >>
> >> Tim.
> >>
> >> On 25/10/2011 17:02, Jim Idle wrote:
> >>> Probably because you are modifying the OR rule and not the AND
> rule.
> >>>
> >>> Jim
> >>>
> >>>> -----Original Message-----
> >>>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> >>>> bounces at antlr.org] On Behalf Of Tim Poole
> >>>> Sent: Tuesday, October 25, 2011 8:54 AM
> >>>> To: antlr-interest at antlr.org
> >>>> Subject: [antlr-interest] Multiple string template rule
> >>>>
> >>>> Hi all,
> >>>>
> >>>> I'm trying to use the += in the following rule, but I'm not sure
> if
> >>>> my use of it is correct.
> >>>>
> >>>> Does anyone have any thoughts?
> >>>>
> >>>> I'm trying to convert:
> >>>>
> >>>> rule != "working" and mental_state == "distressed"
> >>>>
> >>>> into:
> >>>>
> >>>> rule != "working"&&   mental_state == "distressed"
> >>>>
> >>>> Obviously, this snippet of grammar doesn't cover the expressions
> >>>> (andExpr) on either side of the "and", but I hope it gives you a
> >>>> flavour of what I'm trying to achieve.
> >>>>
> >>>> orExpr
> >>>>        : a=andExpr (OR b+=andExpr)* ->   template(a={$a.text},
> >> bf={$b})
> >>>> "<a><bf; separator=\"&&   \">"
> >>>>        ;
> >>>>
> >>>> Regards,
> >>>>
> >>>> Tim.
> >>>>
> >>>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >>>> Unsubscribe:
> >>>> http://www.antlr.org/mailman/options/antlr-interest/your-
> >>>> email-address
> >>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >>> Unsubscribe:
> >>> http://www.antlr.org/mailman/options/antlr-interest/your-email-
> >> address
> >>>
> >>> -----
> >>> No virus found in this message.
> >>> Checked by AVG - www.avg.com
> >>> Version: 2012.0.1834 / Virus Database: 2092/4572 - Release Date:
> >>> 10/24/11
> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> >> Unsubscribe:
> >> http://www.antlr.org/mailman/options/antlr-interest/your-
> >> email-address
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> > http://www.antlr.org/mailman/options/antlr-interest/your-email-
> address
> >
> >
> > -----
> > No virus found in this message.
> > Checked by AVG - www.avg.com
> > Version: 2012.0.1834 / Virus Database: 2092/4573 - Release Date:
> > 10/25/11
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list