[antlr-interest] ArrayList output problem in code transformation

YINGAnnie yimm8369 at hotmail.com
Mon Mar 30 21:25:58 PDT 2009



Hi ,

 

I spend lots of my time on this problem, but I really don't know how to solve it. I am stuck. I need u to help.

 

The description of my question maybe a little long.

 

I want to change
public function ab(){
       var a :int = 15;
       var b :int = 15;
       var c :Number = 5;
       var d:Number = 5 ;
       var e:Number = 3 ;
   }  
 
to 
public function ab(){
      var a,b:int = 15 ;
      var c,d:Number = 5 ;
      var e:Number = 3 ;
       }  

 

in the original statement rule is like this:

 

statement
:variableStatement
;

 

 

Now I add action to statement.

 

statement
scope Symbols;                     //global scope have one field named names.
@init{ 
   $Symbols::names = new ArrayList();
 }           
    variableStatement    { 
    
    for(int i=0;i<$Symbols::names.size();i++)
     { decls.add($Symbols::names.get(i));         //give the value of $Symbols::names to a global ArrayList decls
     } 
    String str=" ";
      $Symbols::out= new ArrayList(); 
      ArrayList visited= new  ArrayList();
     
   for(int i=0;i<decls.size();i++)
     { 
       String si=decls.get(i).toString();
       int pos_i=si.indexOf(":");
       String stri=si.substring(0,pos_i);
       String tempi=si.substring(pos_i);
          
     if(visited.contains(String.valueOf(i))) continue; 
       str="";
       str+="var "+stri; 
         for(int j=i+1;j<decls.size();j++)
      { 
       String sj=decls.get(j).toString();
       int pos_j=sj.indexOf(":");
       String strj=sj.substring(0,pos_j);
       String tempj=sj.substring(pos_j);
       if(tempi.equals(tempj))                              //decide whether the parts after colon are the same.
       {str+=","+strj;
        visited.add(String.valueOf(j));
       }  
       
      }     
       str+=tempi+";";
       $Symbols::out.add(str);
   } 
     System.out.println($Symbols::out);          
     }
  
       
In the variableStatement, $Symbols::names is initialized.
variableStatement

@init{
}
    :   VAR? v=variableDeclaration ( COMMA variableDeclaration )* semic 
      {
      $Symbols::names.add($v.text);
  
  }                       
    ;

Then I got the output:


[var a :int = 15;]
[var a ,b :int = 15;]
[var a ,b :int = 15;, var c :Number = 5;]
[var a ,b :int = 15;, var c ,d:Number = 5;]
[var a ,b :int = 15;, var c ,d:Number = 5;, var e:Number = 3;]

 

Only the last line is the ideal result.

 

I thought  everytime parser parse a new variableStatement, it give out a value, Therefore ,there are 5 variableStatement for 5 output.

 

But when I try to get the last line of out, I even can not use System.out.println($Symbols::out.get(4)); the error message is as follows:

 

java.lang.IndexOutOfBoundsException: Index: 4, Size: 1
 at java.util.ArrayList.RangeCheck(Unknown Source)
 at java.util.ArrayList.get(Unknown Source)

 

My question is how can I get the last line of code.


Thanks a million.

 

Annie

 

 

_________________________________________________________________
MSN 表情魔法书,改变你的对话时代!
http://im.live.cn/emoticons/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090331/523b89e4/attachment.html 


More information about the antlr-interest mailing list