[antlr-interest] antlr-interest Digest, Vol 51, Issue 26

Bill Cox bill at qswtools.com
Thu Feb 26 15:20:12 PST 2009


Re: [antlr-interest] request for comments on language
On Thu, Feb 26, 2009 at 12:00 PM,  <antlr-interest-request at antlr.org> wrote:
> Send antlr-interest mailing list submissions to
>        antlr-interest at antlr.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://www.antlr.org/mailman/listinfo/antlr-interest
> or, via email, send a message with subject or body 'help' to
>        antlr-interest-request at antlr.org
>
> You can reach the person managing the list at
>        antlr-interest-owner at antlr.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of antlr-interest digest..."
>
>
> Today's Topics:
>
>   1. Re: if..else in StringTemplate (Terence Parr)
>   2. Re: request for comments on language implementation (Andy Tripp)
>   3. Re: request for comments on language implementation (Terence Parr)
>   4. Re: request for comments on language implementation
>      (Michael Bedward)
>   5. Changed output path in ANTLRWorks: Cannot find    tokens file
>      <unset-dir> / X.tokens (Joern Gebhardt)
>   6. Re: request for comments on language implementation
>      (Andreas Meyer)
>   7. Re: request for comments on language implementation (Andy Tripp)
>   8. Manipulating text in the lexer (Sam Barnett-Cormack)
>   9. Re: request for comments on language implementation
>      (Andreas Meyer)
>  10. Error reporting with DebugListener attached (Indhu Bharathi)
>  11. Re: Changed output path in ANTLRWorks: Cannot find        tokens
>      file <unset-dir> / X.tokens (Joern Gebhardt)
>  12. Manipulating text in the lexer (Sam Barnett-Cormack)
>  13. common tokens (Robert Soule)
>  14. Backtracking vs Lookahead (Andreas Meyer)
>  15. Re: Manipulating text in the lexer (Indhu Bharathi)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Wed, 25 Feb 2009 13:29:23 -0800
> From: Terence Parr <parrt at cs.usfca.edu>
> Subject: Re: [antlr-interest] if..else in StringTemplate
> To: YingAnnie <yimm8369 at hotmail.com>
> Cc: antlr-interest at antlr.org
> Message-ID: <956F67BC-F769-48A7-B8C5-8B49D8E545EC at cs.usfca.edu>
> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes
>
>
> On Feb 24, 2009, at 6:48 PM, YingAnnie wrote:
>
>>
>>
>>
>> Hi everyone,
>>
>> I want to use if else in the StringTemplateGroup,if (x ="org") print
>> 1234 else print 5678, but <if(x=="org")> doesnot work.
>
> Correct. ST does not allow you to do any logic computations in the
> view. You must do the computation in the model and pass the result to
> the template.
>
>  this philosophy is explained in a number of places on the website
> and the wiki.
>
> Ter
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 25 Feb 2009 17:02:52 -0500
> From: Andy Tripp <antlr at jazillian.com>
> Subject: Re: [antlr-interest] request for comments on language
>        implementation
> To: Andreas Meyer <andreas.meyer at smartshift.de>
> Cc: antlr-interest at antlr.org
> Message-ID: <49A5C00C.1000704 at jazillian.com>
> Content-Type: text/plain; charset=ISO-8859-1; format=flowed
>
> Hi Andreas,
>
> Andreas Meyer wrote:
>> Hi!
>>
>> Your mail was addressed to Michael, but I hope it's ok to answer
>> nonetheless:
>>
>> I would consider hand-written code to walk an AST harmful. Maybe there
>> are cases where it is useful, but walking a dynamically typed tree like
>> this looks like a maintenance nightmare to me.
>
> I've found it much easier to do "hand-written" AST transformations.
> For example, to find cases like "x+0" and replace with just "x", you'd
> have something like:
>
> List<AST> pluses = root.findAncestorsOfType(MyTokenTypes.PLUS);
> for (AST ast: plusses) {
>        if (ast.getChildCount() == 2
>            && ast.getSecondChild().getType() == MyTokenTypes.INTEGER
>            && ast.getSecondChild().getText().equals("0")) {
>                ast.replaceMyself(ast.getFirstChild());  // replace "x+0" with just "x"
>        }
> }
>
> Isn't this simpler than the ANTLR treewalker equivalent?
> Especially since whoever takes over this code when you leave will likely
> already know Java but not know ANTLR?
>
> It's maintainable because later, when you want to also replace "x*1" with just "x", and a few
> other similar patterns, you can make this into a generic function:
>
> void replaceIdentity(MyTokenType type, String value, AST root) {
>        List<AST> asts = root.findAncestorsOfType(type);
>        for (AST ast: plusses) {
>                if (ast.getChildCount() == 2
>                    && ast.getSecondChild().getType() == MyTokenTypes.INTEGER
>                    && ast.getSecondChild().getText().equals("0")) {
>                        ast.replaceMyself(ast.getFirstChild());  // replace "x OP VALUE" with just "x"
>                }
>        }
> }

I also like this style (in the generic form) very much.  I find it
very readable/
maintainable.  Is there anything in your tree building process that guarantees
that the literal zero is the second child?  It seems to me that for
full generality
you would also need to check the first child for zero.

Also, is the AST guaranteed to be a binary tree?  In one code generator that I
built, the arguments to an 'add' operator node were strung in a list,
from widest to narrowest.  Similarly for the multiply node.


More information about the antlr-interest mailing list