[antlr-interest] Can't create AST Node?

Anakreon anakreonmejdi at yahoo.gr
Thu Jul 8 16:11:33 PDT 2004


Tiller, Michael (M.M.) wrote:
> 
>>-----Original Message-----
>>From: Monty Zukowski [mailto:monty at codetransform.com]
>>Subject: Re: [antlr-interest] Can't create AST Node?
>>
>>On Jul 8, 2004, at 10:52 AM, Tiller, Michael ((M.M.)) wrote:
>>
> I'm trying to do some very specific things but protecting for others as
> well.  The main goal of my work is in code metrics.  So I'm interested
> in analyzing source code.
I have implemented a source to source translator from ASP to PHP.
The implementation includes a TreeParser (actually 2 TreeParser one for JavaScript
and one for VbScript). I'm posting this because the program does analyze the code
in order to find the equivalent subtree for a subtree of JavaScript or VbScript.

The implementation language is Java. In a week the project will be sent to sourceforge
and perhaps it would be useful to you as an example.

The TreeParser is very useful. What I do, is to execute some code when a node is matched.
For example consider this:Response.write "Hello"
The tree would be:
.[DOT]
  Response[IDENTIFIER]
  write[METHOD_CALL]
    ARGLIST_VALUES
      Hello [DSTRING]
When DOT is matched, it search for a class or an Object with name Response.
It will find an instance of Object AspClass (this is implementation specific)
Then it checks if class Response has a member called write and finds an instance
of class GenericMethod (implementation specific).
>>From tree:
.[DOT]
  Response[IDENTIFIER]
  write[METHOD_CALL]
the node write[METHOD_CALL] (instance of ObjectAST) is created, so the tree  is now:
write[METHOD_CALL] {ObjectAST}
  ARGLIST_VALUES
    Hello [DSTRING]
When this subtree is matched, the code checks if the GenericMethod
(temporally stored in the ObjectAST object) would accept 1 argument of type
DSTRING. It does in this case.
Then it asks from the GenericMethod to create an equivalent tree for PHP which
is:
print[METHOD_CALL] 
  ARGLIST_VALUES
    Hello [DSTRING]

The real work is done by the GenericMethod instance and the tree was used
to find out which method is write (found by the code executed for (. Response write) )
and also to find the arguments of the method.

I hope the example gives some hints on how to use the TreeParser.

In my case the product of the TreeParser is an other AST tree which represents
PHP code. The final tree is fed to an other TreeParser which produces the PHP source
code. For the last TreeParser a TreeWalker could be used, by I found more convenient
to use a TreeParser.

When the sources will be uploaded at sourceforge, I'll make an anouncement to
the  list, in case someone is interested.

Anakreon. 


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
    antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list