[antlr-interest] Help with a tree grammar: losing a list of statements

Jim Idle jimi at temporal-wave.com
Tue Dec 1 09:48:47 PST 2009


$s is the correct reference, but it is a collection of whatever the statement rule returns, so you may need to use an iterator in your string template. , or it might just be that your statement rule isn't setting up its st properly. 

 

stats={$s}

 

then

 

..., stats) ::= <<

...

<stats:{<it>}; separator="\n">

 

Your reference to $text is the text for the whole node, and so you get the while keyword too. So, check your  statement rule generates a good st, then use $s=statement and just pass in  $s.st to check that just one statement works, then go to s+= and see if that works, then add an iterator in the template and see if that works. Use small incremental changes to see where your problem lies, rather than just stabbing in the dark ;-)

 

Also do not forget that you do not HAVE to use rewrite rules, you can create the template in an action, then repeatedly set the stats attribute in your loop, which will create a collection of StringTemplate.

 

Jim

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Pauba, Kevin L
Sent: Tuesday, December 01, 2009 8:54 AM
To: antlr-interest
Subject: Re: [antlr-interest] Help with a tree grammar: losing a list of statements

 

Sorry to respond to my own post but I've made some (limited) progress.

 

I changed the rewriting tree grammar rule from:

 

        |       ^(KW_while ^(EXPR l=string condition=string r=string) ^(SLIST s+=statement+))

                -> template(lhs={$l.text}, operator={$condition.text}, rhs={$r.text}, stats={$s}) << ... >>

 

to:

 

        |       ^(KW_while ^(EXPR l=string condition=string r=string) ^(SLIST s+=statement+))

                -> template(lhs={$l.text}, operator={$condition.text}, rhs={$r.text}, stats={$text}) << ... >>

 

and now get more substituted for <stats> than I expected:

 

L:

        ; while $99 > 0

        test $99 > 0 L_END:

        while $99 > 0

                LET $99 - 1

                LET $98 + 1

        endwhile

 

        goto L:

L_END:

 

I don't want the "while $99 > 0" or "endwhile" in there, just the text of the tokens for the statements in SLIST.

 

Reading TDAR a little more, I discovered (again) "Deriving imaginary Nodes from Real Tokens" (pg 175) and modified the parser grammar from:

 

        |       KW_while l=string condition=string r=string statement+ KW_endwhile

                -> ^(KW_while ^(EXPR $l $condition $r) ^(SLIST statement+))

 

to:

 

        |       KW_while l=string condition=string r=string statement+ KW_endwhile

                -> ^(KW_while ^(EXPR $l $condition $r) ^(SLIST[$l,"statements"] statement+)) 

 

but I get the following error:

 

error(117): JCC.g:0:0: missing attribute access on rule scope: l

 

 (that l is a lower case L).

 

I welcome any hints or pointers to TDAR sections for a solution.

 

Thanks!

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Pauba, Kevin L
Sent: Monday, November 30, 2009 11:31 AM
To: antlr-interest
Subject: [antlr-interest] Help with a tree grammar: losing a list of statements

 

I have a parser and tree grammar written to add better flow control constructs to an assembly language-like DSL.

 

Here's the parser grammar rule of interest:

 

        |       KW_while l=string condition=string r=string statement+ KW_endwhile

                -> ^(KW_while ^(EXPR $l $condition $r) ^(SLIST statement+))

 

In ANTLRWorks it shows what I expect for an AST using the following text:

 

while $99 > 0

                let $99 - 1

                let $98 + 1

endwhile

 

The expected AST includes a tree with a root of KW_while and two children (the EXPR and the SLIST (the list of two statement trees).  I hope that makes sense.

 

I have a tree-walking grammar that uses "output=template" and "rewrite = true" with the following rule:

 

        |       ^(KW_while ^(EXPR l=string condition=string r=string) ^(SLIST s+=statement+))

                -> template(lhs={$l.text}, operator={$condition.text}, rhs={$r.text}, stats={$s})

                <<

L:

        ; while <lhs> <operator> <rhs>

        test <lhs> <operator> <rhs> L_END:

        <stats>

        goto L:

L_END:

                >>

 

I'm doing something wrong as the "<stats>" ends up being null and I get the following output:

 

L:

        ; while $99 > 0

        test $99 > 0 L_END:

        goto L:

L_END:

 

What is wrong with my rewrite grammar?

 



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20091201/c7d6b4f3/attachment.html 


More information about the antlr-interest mailing list