[antlr-interest] Is my brain crooked?

Tim Gleason tgleason at gmail.com
Sat Jun 16 12:06:46 PDT 2007


I bought the ANTLR book with the idea that I would have some fun
writing a few toy languages -- and maybe even learn a thing or two.
But it seems /every/ grammar I try to write fails due to the LL(*)
restriction.  My brain just seems unable to grok a grammar that ANTLR
likes :(  I've written several recursive descent parsers in the past
and never had these kinds of problems.

Anyway, in re-writing my grammar over an over until I get it right, I
seem to have something that sorta works, but now I can't figure out
how to write the tree generator.

A fragment of my original grammar looks like this:

math_expr
 :  times_expr                               -> times_expr
 |  times_expr times_op math_expr -> ^(BIN_EXPR times_op times_expr math_expr)
 ;

times_op
       :        ('*'|'/');

That, of course, gave me errors.  I've rewritten the rule, like:

math_expr
 : times_expr ('*' times_expr)*
 ;

What I want is a tree if there is a '*', and no tree if not.  I know
that I can add the '^' at the '*' -- but that will give me '*' at the
root of my tree.  What I really want is a tree that looks like:
(BIN_OP {operation} {left_expression} {right_expression})

Is there an easy way to do this in my case?

Thanks for your help!
tim


More information about the antlr-interest mailing list