How to make a rewrite rule to change the root
1 posts in topic
Thread View Thread View
TOPIC ACTIONS:
 

How to make a rewrite rule to change the root...
emilianoarg
Tue Jun 12, 2012 11:07 AM

Hi everybody,

Im working in my tesis. So I'm learning ANTLR and I have a problem to make a tree with the variable as root and the type as child.

Suppose I've the following code to parse:  int VALOR, VALORD; that corresponds to a declaration statement.
My idea is to have a tree with the following form:
VALOR -> int
VALORD -> int


So in my Parser.g I've wrote the following rules that works but return a tree with int as root and Variables as children.

Scenario 1: 

declareStatement: type lala ->  ^(type lala);

lala: VARIABLE+ (COMMA! VARIABLE+)* SEMICOLON!;
Returns:

Program<null>

   IntegerTypeLiteral<int>

      Variable<VALOR>

      Variable<VALORD>

Scenario 2:

 

declareStatement: n+=type+ VARIABLE (COMMA VARIABLE)* SEMICOLON ->  ^(VARIABLE type+);
Returns:

Program<null>

   Variable<VALOR>

      IntegerTypeLiteral<int>

The Scenario 1 puts as root the int. And the Scenario 2 seems to create the tree as we want but only has one variable (Shows VALOR but not VALORD).
To resolve this I've spent hours managing the right side of declareStatement rule... doing changes like: 

declareStatement: n+=type+ VARIABLE (COMMA VARIABLE)* SEMICOLON ->  ^(VARIABLE+ type+);
declareStatement: n+=type+ VARIABLE (COMMA VARIABLE)* SEMICOLON ->  ^(VARIABLE ^(type));
declareStatement: n+=type+ VARIABLE (COMMA VARIABLE)* SEMICOLON ->  ^(VARIABLE ^(type+));
But no one of the changes descripted above has returned the tree required to our project.
I would appreciate your help in order to proceed with my tesis without errors and solve this issue.

Thank you very much,
Emiliano.

 

 



 


 




 I've discover that the following grammar seems to ...

emilianoarg
Sun Jun 17, 2012 04:40 AM

I've discover that the following grammar seems to work fine but its not casting properly to our Node class.

declareStatement: n+=type+ VARIABLE (COMMA VARIABLE)* SEMICOLON ->  ^(VARIABLE type+<type>)+;

Remember that the code to be parsed was  int VALOR, VALORD;
I have a NodeFactory and Nodes of type Variable, IntegerTypeLiteral, etc.
For any strange reason It creates the tree but presents the following description when I debug it:


1) Variable VALOR and a child IntegerTypeLiteral
2) Variable VALORD and a child CommonTree

I've captured the properties in runtime.
1) children=null, startIndex=0, stopIndex=0, token=CommonToken, type=null.
2) children=null, startIndex=-1, stopIndex=-1, token=CommonToken.

The two 'int' exist, but the second 'int' that corresponds to VALORD is not capable to cast to IntegerTypeLiteral. The first int has a property type that is not present in the second int.
I think this type is the key of the problem.

I would appreciate your help.

Thanks,
Emiliano.


 


I'm interested to participate in ANTLR forum. If you know another forum to participate in please send me information about this.

Thanks!
About | Sitemap | Contact