Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Dynamic Languages Toolkit (DLTK) » Tips about building AST
Tips about building AST [message #37503] Mon, 25 May 2009 15:45 Go to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOO
Messages: 28
Registered: July 2009
Junior Member
Hi,

I'm trying to build a DLTK compliant AST and I'm facing an issue
building the parser. I've noticed that there is a "getChilds()" method
on every ASTNode, but you cant add any child node. Reading the code of
the Python IDE example, I discovered that ModuleDeclaration objects
accept child nodes with the "addStatement()" method. So, there is My
question.

Is it possible to have several ModuleDeclaration objects in one single
AST? If no, what is the proper way to link ASTNode objects together ?

Regards
Re: Tips about building AST [message #37540 is a reply to message #37503] Tue, 26 May 2009 03:43 Go to previous messageGo to next message
Alex Panchenko is currently offline Alex Panchenko
Messages: 342
Registered: July 2009
Senior Member
Hi Kevin,

ModuleDeclaration is just a class to return AST of the source module, so
it should be only one instance at the top of tree.

The natural way to navigate over AST is traverse() method, getChild() is
just a helper method and ASTNode's implementation uses traverse() to
collect it's direct children.

So, your classes should look like:

public class RubyIfStatement extends ASTNode {
private ASTNode fCondition;
private ASTNode fThenStatement;
private ASTNode fElseStatement;

public RubyIfStatement(ASTNode condition, ASTNode thenStatement,
ASTNode elseStatement) {
this.fCondition = condition;
this.fThenStatement = thenStatement;
this.fElseStatement = elseStatement;
}

public void traverse(ASTVisitor pVisitor) throws Exception {
if (pVisitor.visit(this)) {
if (fCondition != null) {
fCondition.traverse(pVisitor);
}
if (fThenStatement != null) {
fThenStatement.traverse(pVisitor);
}
if (fElseStatement != null) {
fElseStatement.traverse(pVisitor);
}
pVisitor.endvisit(this);
}
}
.....
}

Regards,
Alex

Kevin KIN-FOO wrote:
> Hi,
>
> I'm trying to build a DLTK compliant AST and I'm facing an issue
> building the parser. I've noticed that there is a "getChilds()" method
> on every ASTNode, but you cant add any child node. Reading the code of
> the Python IDE example, I discovered that ModuleDeclaration objects
> accept child nodes with the "addStatement()" method. So, there is My
> question.
>
> Is it possible to have several ModuleDeclaration objects in one single
> AST? If no, what is the proper way to link ASTNode objects together ?
>
> Regards
Re: Tips about building AST [message #37571 is a reply to message #37540] Wed, 27 May 2009 16:03 Go to previous messageGo to next message
Kevin KIN-FOO is currently offline Kevin KIN-FOO
Messages: 28
Registered: July 2009
Junior Member
Hi Alex,

Your answer is very quick and helpful. I'm still working on AST
generation and I'm wondering if it's possible to use an AST from another
language as reference.

I mean, I have an AST representing the code in another language that is
gifted for AST manipulation, can I do a proxy through it with a
ModuleDeclaration Object ? Is it a good idea ? Did anyone tried it before?

Regards

Alex Panchenko a écrit :
> Hi Kevin,
>
> ModuleDeclaration is just a class to return AST of the source module, so
> it should be only one instance at the top of tree.
>
> The natural way to navigate over AST is traverse() method, getChild() is
> just a helper method and ASTNode's implementation uses traverse() to
> collect it's direct children.
>
> So, your classes should look like:
>
> public class RubyIfStatement extends ASTNode {
> private ASTNode fCondition;
> private ASTNode fThenStatement;
> private ASTNode fElseStatement;
>
> public RubyIfStatement(ASTNode condition, ASTNode thenStatement,
> ASTNode elseStatement) {
> this.fCondition = condition;
> this.fThenStatement = thenStatement;
> this.fElseStatement = elseStatement;
> }
>
> public void traverse(ASTVisitor pVisitor) throws Exception {
> if (pVisitor.visit(this)) {
> if (fCondition != null) {
> fCondition.traverse(pVisitor);
> }
> if (fThenStatement != null) {
> fThenStatement.traverse(pVisitor);
> }
> if (fElseStatement != null) {
> fElseStatement.traverse(pVisitor);
> }
> pVisitor.endvisit(this);
> }
> }
> ....
> }
>
> Regards,
> Alex
>
> Kevin KIN-FOO wrote:
>> Hi,
>>
>> I'm trying to build a DLTK compliant AST and I'm facing an issue
>> building the parser. I've noticed that there is a "getChilds()" method
>> on every ASTNode, but you cant add any child node. Reading the code of
>> the Python IDE example, I discovered that ModuleDeclaration objects
>> accept child nodes with the "addStatement()" method. So, there is My
>> question.
>>
>> Is it possible to have several ModuleDeclaration objects in one single
>> AST? If no, what is the proper way to link ASTNode objects together ?
>>
>> Regards
>
Re: Tips about building AST [message #37733 is a reply to message #37571] Thu, 28 May 2009 08:35 Go to previous message
Alex Panchenko is currently offline Alex Panchenko
Messages: 342
Registered: July 2009
Senior Member
Hi Kevin,

In current Ruby implementation DLTK AST is created from JRuby AST.
It allows reusing of already created parser, so you can gain some time
during development.

Regards,
Alex

Kevin KIN-FOO wrote:
> Hi Alex,
>
> Your answer is very quick and helpful. I'm still working on AST
> generation and I'm wondering if it's possible to use an AST from another
> language as reference.
>
> I mean, I have an AST representing the code in another language that is
> gifted for AST manipulation, can I do a proxy through it with a
> ModuleDeclaration Object ? Is it a good idea ? Did anyone tried it before?
>
> Regards
>
> Alex Panchenko a écrit :
>> Hi Kevin,
>>
>> ModuleDeclaration is just a class to return AST of the source module, so
>> it should be only one instance at the top of tree.
>>
>> The natural way to navigate over AST is traverse() method, getChild() is
>> just a helper method and ASTNode's implementation uses traverse() to
>> collect it's direct children.
>>
>> So, your classes should look like:
>>
>> public class RubyIfStatement extends ASTNode {
>> private ASTNode fCondition;
>> private ASTNode fThenStatement;
>> private ASTNode fElseStatement;
>>
>> public RubyIfStatement(ASTNode condition, ASTNode thenStatement,
>> ASTNode elseStatement) {
>> this.fCondition = condition;
>> this.fThenStatement = thenStatement;
>> this.fElseStatement = elseStatement;
>> }
>>
>> public void traverse(ASTVisitor pVisitor) throws Exception {
>> if (pVisitor.visit(this)) {
>> if (fCondition != null) {
>> fCondition.traverse(pVisitor);
>> }
>> if (fThenStatement != null) {
>> fThenStatement.traverse(pVisitor);
>> }
>> if (fElseStatement != null) {
>> fElseStatement.traverse(pVisitor);
>> }
>> pVisitor.endvisit(this);
>> }
>> }
>> ....
>> }
>>
>> Regards,
>> Alex
>>
>> Kevin KIN-FOO wrote:
>>> Hi,
>>>
>>> I'm trying to build a DLTK compliant AST and I'm facing an issue
>>> building the parser. I've noticed that there is a "getChilds()" method
>>> on every ASTNode, but you cant add any child node. Reading the code of
>>> the Python IDE example, I discovered that ModuleDeclaration objects
>>> accept child nodes with the "addStatement()" method. So, there is My
>>> question.
>>>
>>> Is it possible to have several ModuleDeclaration objects in one single
>>> AST? If no, what is the proper way to link ASTNode objects together ?
>>>
>>> Regards
Previous Topic:[BUILD] S-1.0RC2-200905271211
Next Topic:[BUILD] S-1.0RC2a-200905280900
Goto Forum:
  


Current Time: Fri Oct 31 07:51:15 GMT 2014

Powered by FUDForum. Page generated in 0.10837 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software