Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » IMP » LPG question: how to add a method to generated code
LPG question: how to add a method to generated code [message #25498] Sat, 15 November 2008 10:36 Go to next message
Eclipse User
Originally posted by: jonathan910.freemail.br

Hi, not sure if this is the forum for LPG questions but
here goes:

I would like to add a getType metod to all the generated AST nod
objects. The method should return a constant representing
the AST node (this to avoid all the instanceof constructs).

What is the best way to do this?

BTW: Is it possible to define that all the AST node inherits from
a class that I declare?


Thanks in advance

Jonathan
Re: LPG question: how to add a method to generated code [message #25578 is a reply to message #25498] Wed, 19 November 2008 20:49 Go to previous messageGo to next message
Stan Sutton is currently offline Stan Sutton
Messages: 121
Registered: July 2009
Senior Member
Hi Jonathan,


Jonathan wrote:
> Hi, not sure if this is the forum for LPG questions but
> here goes:

This is not primarily an LPG forum, but since we provide LPG with IMP we
try to offer some support.

> I would like to add a getType metod to all the generated AST nod
> objects. The method should return a constant representing
> the AST node (this to avoid all the instanceof constructs).
>
> What is the best way to do this?

In the LPG grammar specification file (the .g file) it is possible to
associate some Java code with each production. This Java code gets
generated into the classes that LPG creates for the various node types.
I believe that you should be able to add a method to each node type of
interest by defining it in this way. (You can see examples in the
leg.g file that gets generated if you run the IMP wizard to generate a
new LPG grammar file.)

> BTW: Is it possible to define that all the AST node inherits from
> a class that I declare?

I don't think it's possible to do that specifically. LPG does generate
a type, ASTNode, that is the parent type for the various specific AST
node types. I don't know that there is a way to automatically add code
to that (as you can with the individual rules as described above).

I'll check with local experts and see if I can dig up any further ideas.

>
> Thanks in advance
>
> Jonathan
>

Good luck!

Stan
Re: LPG question: how to add a method to generated code [message #25617 is a reply to message #25498] Wed, 19 November 2008 21:25 Go to previous message
Stan Sutton is currently offline Stan Sutton
Messages: 121
Registered: July 2009
Senior Member
Jonathan,

Additional information on defining (or customizing) your own parent AST
class:

1) rename the Root node with the option ast_type=my_no_name...

(You can see this option near the top of the generated .g template.)


2) add additional class member declarations to it in the %Ast
section. For example, to add an additional int field, one
would specify:

%Ast
/.
int additionalField;
void setAdditionalField(int x) { additionalField = x; }
int getNewField() { return additionalField; }
./
%End

(You would need to add the %Ast section to your .g file.)

Hope this helps!

Stan

Jonathan wrote:
> Hi, not sure if this is the forum for LPG questions but
> here goes:
>
> I would like to add a getType metod to all the generated AST nod
> objects. The method should return a constant representing
> the AST node (this to avoid all the instanceof constructs).
>
> What is the best way to do this?
>
> BTW: Is it possible to define that all the AST node inherits from
> a class that I declare?
>
>
> Thanks in advance
>
> Jonathan
>
>
>
>
>
Re: LPG question: how to add a method to generated code [message #574281 is a reply to message #25498] Wed, 19 November 2008 20:49 Go to previous message
Stan Sutton is currently offline Stan Sutton
Messages: 121
Registered: July 2009
Senior Member
Hi Jonathan,


Jonathan wrote:
> Hi, not sure if this is the forum for LPG questions but
> here goes:

This is not primarily an LPG forum, but since we provide LPG with IMP we
try to offer some support.

> I would like to add a getType metod to all the generated AST nod
> objects. The method should return a constant representing
> the AST node (this to avoid all the instanceof constructs).
>
> What is the best way to do this?

In the LPG grammar specification file (the .g file) it is possible to
associate some Java code with each production. This Java code gets
generated into the classes that LPG creates for the various node types.
I believe that you should be able to add a method to each node type of
interest by defining it in this way. (You can see examples in the
leg.g file that gets generated if you run the IMP wizard to generate a
new LPG grammar file.)

> BTW: Is it possible to define that all the AST node inherits from
> a class that I declare?

I don't think it's possible to do that specifically. LPG does generate
a type, ASTNode, that is the parent type for the various specific AST
node types. I don't know that there is a way to automatically add code
to that (as you can with the individual rules as described above).

I'll check with local experts and see if I can dig up any further ideas.

>
> Thanks in advance
>
> Jonathan
>

Good luck!

Stan
Re: LPG question: how to add a method to generated code [message #574305 is a reply to message #25498] Wed, 19 November 2008 21:25 Go to previous message
Stan Sutton is currently offline Stan Sutton
Messages: 121
Registered: July 2009
Senior Member
Jonathan,

Additional information on defining (or customizing) your own parent AST
class:

1) rename the Root node with the option ast_type=my_no_name...

(You can see this option near the top of the generated .g template.)


2) add additional class member declarations to it in the %Ast
section. For example, to add an additional int field, one
would specify:

%Ast
/.
int additionalField;
void setAdditionalField(int x) { additionalField = x; }
int getNewField() { return additionalField; }
./
%End

(You would need to add the %Ast section to your .g file.)

Hope this helps!

Stan

Jonathan wrote:
> Hi, not sure if this is the forum for LPG questions but
> here goes:
>
> I would like to add a getType metod to all the generated AST nod
> objects. The method should return a constant representing
> the AST node (this to avoid all the instanceof constructs).
>
> What is the best way to do this?
>
> BTW: Is it possible to define that all the AST node inherits from
> a class that I declare?
>
>
> Thanks in advance
>
> Jonathan
>
>
>
>
>
Previous Topic:How to get LPG to treat comments as terminals
Next Topic:Parser message adaptation/customization
Goto Forum:
  


Current Time: Fri Oct 24 13:26:56 GMT 2014

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

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