[antlr-interest] AST Question

Bart Kiers bkiers at gmail.com
Fri May 20 01:55:11 PDT 2011


Hi

Your rule:

targetsExpr
  :  category ('CAND' targetsExpr)* -> ^('CAND' category targetsExpr*)
  ;


is incorrect. You're always using `CAND` in your rewrite rule but that rule
could just match `category` only.

You'll probably want to do:

targetsExpr
  :  category ('CAND'^ targetsExpr)*
  ;

(Note the ^ after CAND which makes it the root)

Regards,

Bart.


On Fri, May 20, 2011 at 10:11 AM, massimiliano.masi at gmail.com <
massimiliano.masi at gmail.com> wrote:

> Hello All,
>
> I'm more or less a newbie using antlr. I have a small issue on creating
> the AST, using rewrite rules. I'm so sorry if this is a FAQ or similar! :)
>
> I have the following productions (it's like an algebra with
> 3 operators with different priorities):
>
>
> targetsExpr  : (category) ('CAND' targetsExpr)*
>   ->^('CAND' category targetsExpr*)
>  ;
> category: (matchEl) ('OR' category)*
>   -> ^('OR' matchEl category* )
>  ;
> matchEl : factor ('AND' factor)* ->^('AND' factor*)
>  ;
>
> factor
>  : matchId OPAR targetValue COMMA targetName CPAR ->^('FAC' matchId
> targetValue targetName)
>
>
> The problem is that the AST created contains productions as:
>
> CAND ->OR -> OR-> AND (FAC, FAC).
>
> The second OR is created because the ``category'' production is passed
> multiple times.
>
> Is there a way to not create these kind of  rules?
>
> You can see a sample of the AST created in
> http://www.mascanc.net/~max/policy.pdf.
>
>
>
> --
> Massimiliano Masi
>
> http://www.mascanc.net/~max
>
> 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