[antlr-interest] Different StringTemplate outputs depending on results of called parser rule

Cremerius Ralf (DGS-EC/ECC3) Ralf.Cremerius at de.bosch.com
Mon Nov 8 07:06:52 PST 2010


Hello,

Working with ANTLR v3 + StringTemplate I often come across the following situation:

One parser rule calls another rule and depending on the evaluation of that called rule, I'd like to return different values using StringTemplate. As far as I know, each rule alternative in ANTLR may only return one kind of StringTemplate output. So if I want to return two kinds of outputs, I need two parser rule alternatives. I then come up with parser rules of the following style (using a semantic predicate), but this won't work:

R:      // Alternative A
        called_rule
        { (comeConditionDependingOn_Called_rule) }?
        -> stringtemplate_output_A(...)
        |  // Alternative B
        { (!(comeConditionDependingOn_Called_rule)) }?
        -> stringtemplate_output_B(...)

=> What is a smarter way of achieving the different desired outputs?

Thanks in advance for any feedback & best regards,
   Ralf Cremerius





More information about the antlr-interest mailing list