[antlr-interest] V2 -> V3 rules

Kevin J. Cummings cummings at kjchome.homeip.net
Tue Mar 8 11:13:18 PST 2011


On 03/08/2011 01:45 PM, g4 at novadsp.com wrote:
> Thanks Jim
> 
> In order to get the right tree I ended up with this:
> 
> rhs
> 	: a=rhs_alt (OR b=rhs)*
> 	;
> 
> 
> Sorry to labor the point here but what I keep failing to see is how you 
> spec a rewrite rule for this kind of expression. I know the default gets 
> me what I need but that's not quite the same as knowing :)

Sorry, I'm not Jim.  B^)

When I write tree grammars (I've only written a couple), I think of what
the trees will look like, and write the grammars to match the trees that
could possibly be walked as input.  If it helps you to think of what the
trees will look like, consider this:  Most parsed trees are a list of
root nodes.  What can those root nodes have for children.  Create rules
for those cases and for the cases of when those children are themselves
trees (and not single nodes).  When there are multiple possibilities,
either handle them with alternations or with multiple rules to be able
to match all possible cases.

Pretty simple when you think about it, but its not exactly the same as
trying to parse the source code into an AST (at least its not how *I*
think about it).

> I do not want to use the list idiom Ter mentions in the book. Are 
> sub-rules the only way to do it?

-- 
Kevin J. Cummings
kjchome at verizon.net
cummings at kjchome.homeip.net
cummings at kjc386.framingham.ma.us
Registered Linux User #1232 (http://counter.li.org)


More information about the antlr-interest mailing list