[antlr-interest] infinite recursion on tree parser

Duygu Altinok duygu_the_duygu at yahoo.com
Tue Jan 19 13:20:57 PST 2010


I'm writing a C -like language compiler for my language processors course . I defined a rule compound_expr which represents  nested blocks closed within curly braces. Compilation of the parser is fine but tree parser gives an error , can anybody please help?? 

f3.g:1014:10: infinite recursion to rule statement from rule statement_list
f3.g:1030:20: infinite recursion to rule statement from rule ca
f3.g:1029:26: infinite recursion to rule statement from rule compound_expr
f3.g:1023:13: infinite recursion to rule statement from rule statement
f3.g:1014:10: infinite recursion to rule statement from rule statement_list
f3.g:1014:10: infinite recursion to rule statement from rule statement_list
f3.g:1014:10: infinite recursion to rule statement from rule statement_list
f3.g:1014: warning:nondeterminism upon
f3.g:1014:     k==1:NULL_TREE_LOOKAHEAD,NUM,ID,PLUS,MINUS,MULT,DIV,LT,LEQ,EQ,NEQ,ISEQ,OUTPUTT,INPUTT,PARANTEZLISIN,IF,"return","while"
f3.g:1014:     between alt 1 and exit branch of block


Here's my code for compound_expr in the parser and the tree parser :

parser :

program:

    function_list  

{


    #program= #([PROGRAM,"program"],symbol_table, program);
}
;


function_list:
{
    is_in_function_list = true;
}
          (function)+
{
#function_list= #([FUNCTION_LIST, "function_list"], function_list);
}
         ;

function:
{
    String bt;
}
     bt=basic_type! "func"! i:ID! 
{
    String identifier = i.getText();
    
    if  (identifier.length() > 32)
    {
        error(WARN00, i.getLine(), i.getColumn());
        identifier = identifier.substring(0, 32);
    }
    
    which_function = new String(identifier);

    identifier=identifier + ":" + Integer.toString(i.getLine()) + ":" + Integer.toString(i.getColumn());
    
}
    
     parameter_list!  function_body 

{
    
    symbol_table.addChild(#([SYMBOL_FUNCTION, identifier ], [SYMBOL_TYPE, bt] , symbol_parameters, symbol_locals ));
    
#function=(#([ID,identifier],function));
}
    ;

function_body:
         LCURLY  declaration_list! statement_list RCURLY
         ;
declaration_list:
{
if (is_in_function_list)
    symbol_locals = (CommonAST) astFactory.create(SYMBOL_LOCALS, "symbol_locals");
}
         (declaration! SEMI!)*
        ;  
        
declaration:
{ String t  = new String("");
  String t2 = new String("");  } 
    t=basic_type i:ID t2=array_extension  {

 String identifier = i.getText();
                if  (identifier.length() > 32)
                {
                        error(WARN00, i.getLine(), i.getColumn());
                        identifier = identifier.substring(0, 32);
                }
t  += t2;
 identifier=identifier + ":" + Integer.toString(i.getLine()) + ":" + Integer.toString(i.getColumn());
                if (is_in_function_list && is_in_parameter)
                        symbol_parameters.addChild(#([SYMBOL_PARAMETER, identifier], [SYMBOL_TYPE, t], [SYMBOL_FUNCTION_SCOPE, which_function]));

        else if (is_in_function_list && ! is_in_parameter)
                        symbol_locals.addChild(#([SYMBOL_LOCAL, identifier], [SYMBOL_TYPE, t], [SYMBOL_FUNCTION_SCOPE, which_function]));
}
       ;


paramdecl :
{ String t  = new String("");
  String t2 = new String("");  } 
    t=basic_type i:ID t2=array_extension  {

 String identifier = i.getText();
                if  (identifier.length() > 32)
                {
                        error(WARN00, i.getLine(), i.getColumn());
                        identifier = identifier.substring(0, 32);
                }
t  += t2;
 identifier=identifier + ":" + Integer.toString(i.getLine()) + ":" + Integer.toString(i.getColumn());
                if (is_in_function_list && is_in_parameter)
                        symbol_parameters.addChild(#([SYMBOL_PARAMETER, identifier], [SYMBOL_TYPE, t], [SYMBOL_FUNCTION_SCOPE, which_function]));

        else if (is_in_function_list && ! is_in_parameter)
                        symbol_locals.addChild(#([SYMBOL_LOCAL, identifier], [SYMBOL_TYPE, t], [SYMBOL_FUNCTION_SCOPE, which_function]));
} ;


    
parameter_list:
{
    symbol_parameters = (CommonAST) astFactory.create(SYMBOL_PARAMETERS, "symbol_parameters");
    is_in_parameter = true;
}
 LPAREN

          ( variable_list {is_in_parameter = false;}
           | {is_in_parameter = false;} ) RPAREN
           ;
         

variable_list : LPAREN  (paramdecl (COMMA  paramdecl)*)?  RPAREN ;
        


type returns [String t]
{
    String bt=new String();
     String ar=new String();
    t = new String();
}
     :
     bt=basic_type ar=array_extension
     {
         t = bt + ar;
     }
    ;
   
basic_type returns [String bt]
{
    bt = new String();
}       
       :
       "int" {
               bt = new String("int");
        }
       |"float" {
               bt = new String("float");
        }
       
      ;


    
array_extension returns [String ar]
{
    ar = new String();
}
        :
            lbracket:LBRAC{ar = new String("[");} (n:NUM {ar += n.getText();}) RBRAC{ar += "]" ;}
        | 
          {
            ar = new String("");
          }
           ;

array_extension2 returns [String ar]
{
    ar = new String();
}
        :
            lbracket:LBRAC{ar = new String("[");}  RBRAC{ar += "]" ;}
        | 
          {
            ar = new String("");
          }
           ;
                              

statement_list : (statement)+; 
statement:
       (assignment_statement)=>assignment_statement
          | read_statement
      |return_statement
      |if_statement
      |while_statement
          | compound_expr
          | print_statement
          | expression SEMI
      ;
      


return_statement : "return"^ expression SEMI;
assignment_statement:
             variable EQ^ expression SEMI!
            ;



compound_expr   :        ca;
ca      :       LCURLY!   (statement_list)?  RCURLY!;




variable:
     i:ID! (LBRAC expression RBRAC)? 
     {
         String identifier=i.getText()+":"+i.getLine()+":"+i.getColumn();
         #variable = #([ID,identifier], variable);
    }
    
    ;

expression:
        simple_expression ( (LEQ^ |NEQ^|LT^ |ISEQ^) simple_expression)*
       ;

simple_expression:
          term ((PLUS^|MINUS^) term)*
          ;           



term :
    factor ( (MULT | DIV ) factor)*
    ;
    
    

factor :
    i:ID! (LBRAC expression RBRAC | LPAREN argument_list RPAREN)? 
    {
         String identifier=i.getText()+":"+i.getLine()+":"+i.getColumn();
         #factor = #([ID,identifier], factor);
    }
    | j: NUM!
    {
         String identifier=j.getText()+":"+j.getLine()+":"+j.getColumn();
         #factor = #([NUM,identifier], factor);
    }
    | LPAREN! expression RPAREN! { #factor = #([PARANTEZLISIN, "parantezli"], factor);}
    ;


read_statement : read_item EQ! INPUTT^ LPAREN! (STRING)? RPAREN! ;
read_item : variable; 
print_statement : OUTPUTT^  LPAREN! (STRING COMMA)? print_item RPAREN!  SEMI!;
print_item: variable;


function_call : ID LPAREN argument_list RPAREN SEMI;

expression_list :
    expression (COMMA! expression)*
    ;

argument_list :
    expression_list 
    | 
    ;

    




text_character :
    CHARLIT
    | special_character
    ;

special_character :
    "\n"
    ;



    
if_statement : if_part then_part else_part
{
#if_statement = #([IF , "if"], #if_statement); };

if_part :  IFF! LPAREN! expression RPAREN! ;
then_part : statement;
else_part  : (ELSE^  statement)? ENDIF! SEMI! ;

while_statement : WHILE^  LPAREN! expression  RPAREN! statement;
    

    
This part is OK. It gives no compilation errors. Here's the erronous part:




program
    :
    #(PROGRAM symbol_table 
    {
        sTable.sort();
    }  function_list 
    {
    sTable.prettyPrint();
    }
    )

    ;

symbol_table
    :
    #(SYMBOL_TABLE  
    (
    #(i:SYMBOL_FUNCTION 
    j:SYMBOL_TYPE 
    {
        //Parse info
        String identifier;
        int line, column;
        String [] params = new String[3];
        
        identifier = i.getText();
        params = identifier.split(":");
        identifier = params[0];
        line = Integer.parseInt(params[1]);
        column = Integer.parseInt(params[2]);
        
        Function newFunction=new Function(identifier,j.getText(), line, column); 
        
        
        //add Function to Symbol Table
        int index;
         if ((index = sTable.searchFunction(newFunction.name)) != -1)
            {
                error(ERR01, line, column, ((Function)sTable.functions.elementAt(index)).line, ((Function)sTable.functions.elementAt(index)).column);
                isFunctionLegal = false;
            }
        else
            {
                sTable.addFunction(newFunction);
                int last=sTable.functions.size()-1;
                currentFunction =(Function) sTable.functions.elementAt(last);
                isFunctionLegal = true;
                
            }
            
    } 
    symbol_parameters symbol_locals)

    
    )*
    )
    ;

symbol_parameters
    :
    #(SYMBOL_PARAMETERS 
    (
    #(i:SYMBOL_PARAMETER j:SYMBOL_TYPE 
    
{
        //Parse info
        String identifier;
        int line, column;
        String [] params = new String[3];
        
        identifier = i.getText();
        params = identifier.split(":");
        identifier = params[0];
        line = Integer.parseInt(params[1]);
        column = Integer.parseInt(params[2]);

 
    if (isFunctionLegal)
    {
        
        int last=sTable.functions.size()-1;         
        Symbol newSymbol = new Symbol(identifier,j.getText(),line,column);
        if (currentFunction.searchParameter(newSymbol.name) != -1){
            error(FuncErr+currentFunction.name,line,column);
            error(ERR03, line, column);
        }
        else 
            currentFunction.addParameter(newSymbol);
    }
} 
    
    SYMBOL_FUNCTION_SCOPE))*
    )
    ;
    
symbol_locals
    :
    #(SYMBOL_LOCALS (#(i:SYMBOL_LOCAL j:SYMBOL_TYPE
{ 
        //Parse info
        String identifier;
        int line, column;
        String [] params = new String[3];
        
        identifier = i.getText();
        params = identifier.split(":");
        identifier = params[0];
        line = Integer.parseInt(params[1]);
        column = Integer.parseInt(params[2]);

    if (isFunctionLegal)
    {
        int index;
        int last=sTable.functions.size()-1;         
        Symbol newSymbol = new Symbol(identifier,j.getText(),line, column);
        if ((index = currentFunction.searchParameter(newSymbol.name)) != -1){
            error(FuncErr+currentFunction.name,line,column);
            error(ERR04, line, column, ((Symbol)currentFunction.parameters.elementAt(index)).line,
            ((Symbol)currentFunction.parameters.elementAt(index)).column);
        
        }
        else if ((index = currentFunction.searchLocal(newSymbol.name)) != -1){
            error(FuncErr+currentFunction.name,line,column);
            error(ERR05, line, column, ((Symbol)currentFunction.locals.elementAt(index)).line,            ((Symbol)currentFunction.locals.elementAt(index)).column);
        }
        else if(j.getText().indexOf("[]")==-1)
        {
            currentFunction.addLocal(newSymbol);
        }
        else
            error(ERR06, line, column);    

    }
}  
    SYMBOL_FUNCTION_SCOPE))*)
    ;
                

function_list
    :
    #(FUNCTION_LIST (function)+)
    ;

function
    :
    #(i:ID
{
    //Parse info
    String identifier;
    int line, column;
    
    String [] params = new String[3];
        
    identifier = i.getText();
    params = identifier.split(":");
    identifier = params[0];
    line = Integer.parseInt(params[1]);
    column = Integer.parseInt(params[2]);
    
    int index;
    int line2=0,column2=0; //line and column info from the symbol table
    index = sTable.getFunctionIndex(identifier);
    if(index != -1)
    {
    line2=((Function)sTable.functions.elementAt(index)).line;
    column2=((Function)sTable.functions.elementAt(index)).column;
    }
    if(index!=-1 && line==line2 && column==column2)
    {    
        isFunctionLegal=true;
        currentFunction = (Function) sTable.functions.elementAt(index);
    }
    else
        isFunctionLegal=false; 
        
    
}
     function_body)
    ;

function_body:
    statement_list
    ;
    
statement_list:
    (statement)+
    ;

statement:
          (assignment_statement)=>assignment_statement
          | read_statement
      |return_statement
      |if_statement
      |while_statement
          | compound_expr
          | print_statement
          | expression SEMI
      ;


compound_expr   :        ca;
ca      :         (statement_list)?  ;
      
assignment_statement
{
    Symbol retType ;
    String type = new String("");
    int index;
    String exType = new String("");
}    
    :
#(EQ retType=variable exType=expression)
{    
if(isFunctionLegal)
{
    if (exType.startsWith("float"))
    {
        index=currentFunction.getParameterIndex(retType.name);
            if(index!=-1)
            {
                type=((Symbol)currentFunction.parameters.elementAt(index)).type;
            }
            else{
                index = currentFunction.getLocalIndex(retType.name);
                if(index!=-1){
                    type=((Symbol)currentFunction.locals.elementAt(index)).type;
                }

            }
        if (type.startsWith(new String("int")))
        {
            error(FuncErr+currentFunction.name,retType.line,retType.column);
            error(ERR12,retType.line,retType.column);
                
        }
    }
}
}
        
    ;
    
return_statement
{
    String exType;
    String type;
}
    :
    #("return" exType=expression
    {
    if(isFunctionLegal)
    {
        type=currentFunction.returntype;
        if(exType.startsWith("float") && type.startsWith("int"))
        {
            String identifier;
            int line, column;
            String [] params = new String[3];
        
            identifier = exType;
            params = identifier.split(":");
            identifier = params[0];
            line = Integer.parseInt(params[1]);
            column = Integer.parseInt(params[2]);
            
            error(FuncErr+currentFunction.name,line,column);
            error(ERR13,line,column);    
        }
    }
    }
    )
    ;

print_statement
    :
    #(OUTPUTT print_item)
    ;


print_item :
    variable 
    ;

read_statement
    :
#(INPUTT  read_item)
    ;


read_item 
{
    Symbol retType;
}:
     retType=variable 
{
if(isFunctionLegal)
{
    int index;
    String type=new String("");
    int line,column;

    index=currentFunction.getParameterIndex(retType.name);
    if(index!=-1)
    {
        type=((Symbol)currentFunction.parameters.elementAt(index)).type;
    }
    else{
        index = currentFunction.getLocalIndex(retType.name);
        if(index!=-1){
            type=((Symbol)currentFunction.locals.elementAt(index)).type;
        }
        else 
        {
                index=sTable.getFunctionIndex(retType.name);
                if(index!=-1)
                {
                    line=((Function)sTable.functions.elementAt(index)).line;
                    column=((Function)sTable.functions.elementAt(index)).column;
            
                    error(FuncErr+currentFunction.name,retType.line,retType.column);
                    error(ERR07,retType.line,retType.column,line,column);
                }
        }
        }
        
        if ( !(type.startsWith(new String("int"))) && !(type.equals("")))
        {
            error(FuncErr+currentFunction.name,retType.line,retType.column);
            error(ERR09,retType.line,retType.column);
        }
     

}
}
;    
            
    
if_statement:
    #(IF if_part then_part else_part)
    ;
    
if_part:
    expression
    ;
    
then_part :
    #("then" statement)
    ;
    
else_part :
    #("else" statement)
    |
    ;
    
while_statement 
    :
    #("while" expression statement)
    ;

variable returns [Symbol v]{
    v =new Symbol(new String(""),new String(""),0,0);
    String exType;
    boolean isArray=false;
}
    :
#(i:ID (LBRAC exType=expression RBRAC 
{
if(isFunctionLegal)
{
    if(exType.startsWith(new String("float"))) 
    {    
            //Parse info
            String identifier;
            int line, column;
            String [] params = new String[3];
        
            identifier = exType;
            params = identifier.split(":");
            identifier = params[0];
            line= Integer.parseInt(params[1]);
            column= Integer.parseInt(params[2]);
            
            error(FuncErr+currentFunction.name,line,column);
            error(ERR11,line,column);        
    }

    isArray=true;
}
}
)? )
{    
if(isFunctionLegal)
{
    //Parse info
        String identifier;
        int line, column;
        String [] params = new String[3];
        
        identifier = i.getText();
        params = identifier.split(":");
        identifier = params[0];
        line = Integer.parseInt(params[1]);
        column = Integer.parseInt(params[2]);

        v = new Symbol(identifier,"", line, column);
        int index;
        String type=new String("");
        index=currentFunction.getParameterIndex(identifier);
        if(index!=-1)
        {
            type=((Symbol)currentFunction.parameters.elementAt(index)).type;
        }
        else{
            index = currentFunction.getLocalIndex(identifier);
            if(index!=-1){
                type=((Symbol)currentFunction.locals.elementAt(index)).type;
            }
        }
            
        if(!type.equals("") && type.indexOf("[")!=-1 && !isArray)    
        {
                error(FuncErr+currentFunction.name,line,column);
                error(ERR15,line,column);
        }
        if(!type.equals("") && type.indexOf("[")==-1 && isArray)    
        {
                error(FuncErr+currentFunction.name,line,column);
                error(ERR16,line,column);
        }
        if(isArray){
                isArray=false;
                
        }
}
}
    ;
    
expression returns [String exType]
{
    String sType;
    exType=new String(""); 
}
    
    :
         (#(ISEQ expression simple_expression)) => #(ISEQ exType=expression sType=simple_expression)
        {
        if(isFunctionLegal)
        {
        if (sType.startsWith(new String("float")) && !exType.startsWith(new String("float"))) 
        {    
            exType=sType;
        }    
        
            }
        }
        | (#(NEQ expression simple_expression)) => #(NEQ exType=expression sType=simple_expression)
        {
        if(isFunctionLegal)
        {
        if (sType.startsWith(new String("float")) && !exType.startsWith(new String("float"))) 
        {    
            exType=sType;
        }    
        
        }
        }
        |( #(LT expression simple_expression) ) => #(LT exType=expression sType=simple_expression)
        {
        if(isFunctionLegal)
        {
        if (sType.startsWith(new String("float")) && !exType.startsWith(new String("float"))) 
        {    
            exType=sType;
        }    
        
        }
        }
        |( #(LEQ expression simple_expression) ) => #(LEQ exType=expression sType=simple_expression)
        {
        if(isFunctionLegal)
        {
        if (sType.startsWith(new String("float")) && !exType.startsWith(new String("float"))) 
        {    
            exType=sType;
        }    
        
        }
        }
        
        |exType=simple_expression
        ;

simple_expression returns [String sType]
{
    String tType;
    sType=new String();
}
    :
      (#(PLUS simple_expression term))=>#(PLUS sType=simple_expression tType=term)
      {
      if(isFunctionLegal)
      {
        if (tType.startsWith(new String("float")) && !sType.startsWith(new String("float"))) 
        {    
            sType=tType;
        }    
        
      }
      }
      | (#(MINUS simple_expression term))=>#(MINUS sType=simple_expression tType=term)
      {
      if(isFunctionLegal)
      {
        if (tType.startsWith(new String("float")) && !sType.startsWith(new String("float"))) 
        {    
            sType=tType;
        }    
      } 
      }
      | sType=term
          ;           

term returns [String tType]
{
    Symbol retType=new Symbol(new String(""),new String(""),0,0);
    tType=new String("");
}
    :
    (#( MULT term factor))=>#( MULT tType=term retType=factor
    {
    if(isFunctionLegal)
    {
        int index;
        String type=new String("");
        int line,column;
        
        //Control whether it is a number or an identifier
        if(!retType.name.equals("") && new Character(retType.name.charAt(0))<=new Character('9') && new Character(retType.name.charAt(0))>=new Character('0'))
        {
            if(retType.name.indexOf('.')!=-1 || retType.name.indexOf('E')!=-1 || retType.name.indexOf('e')!=-1)
                type="float";
            else
                type="int";

        }
        else{
            index=currentFunction.getParameterIndex(retType.name);
            if(index!=-1)
            {
                type=((Symbol)currentFunction.parameters.elementAt(index)).type;
            }
            else{
                index = currentFunction.getLocalIndex(retType.name);
                if(index!=-1){
                    type=((Symbol)currentFunction.locals.elementAt(index)).type;
                }
                else 
                {
                    error(FuncErr+currentFunction.name,retType.line,retType.column);
                    error(ERR08,retType.line,retType.column);
                        }
                    }
        }
    
    if(type.startsWith(new String("float")) && !tType.startsWith(new String("float")))
            tType=type+new String(":")+new Integer(retType.line)+new String(":")+new Integer(retType.column);

    }
    }
    )
    |(#( DIV term factor))=>#( DIV tType=term retType=factor
    {
    if(isFunctionLegal)
    {
        int index;
        String type=new String("");
        int line,column;
        
        //Control whether it is a number or an identifier
        if(!retType.name.equals("") && new Character(retType.name.charAt(0))<=new Character('9') && new Character(retType.name.charAt(0))>=new Character('0'))
        {
            if(retType.name.indexOf('.')!=-1 || retType.name.indexOf('E')!=-1 || retType.name.indexOf('e')!=-1)
                type="float";
            else
                type="int";

        }
        else{
            index=currentFunction.getParameterIndex(retType.name);
            if(index!=-1)
            {
                type=((Symbol)currentFunction.parameters.elementAt(index)).type;
            }
            else{
                index = currentFunction.getLocalIndex(retType.name);
                if(index!=-1){
                    type=((Symbol)currentFunction.locals.elementAt(index)).type;
                }
                else 
                {
                        index=sTable.getFunctionIndex(retType.name);
                        if(index!=-1)
                        {
                            type=((Function)sTable.functions.elementAt(index)).returntype;    
                        }
                }
            }    
        }
    
        if(type.startsWith(new String("float")) && !tType.startsWith(new String("float")))
            tType=type+new String(":")+new Integer(retType.line)+new String(":")+new Integer(retType.column);
    }
    }
    )
           | retType=factor
    {
    if(isFunctionLegal)
    {    
        int index;
        String type=new String("");
        int line,column;
        
        //Control whether it is a number or an identifier
        if(!retType.name.equals("") && new Character(retType.name.charAt(0))<=new Character('9') && new Character(retType.name.charAt(0))>=new Charac
ter('0'))
        {
            if(retType.name.indexOf('.')!=-1 || retType.name.indexOf('E')!=-1 || retType.name.indexOf('e')!=-1)
                type="float";
            else
                type="int";

        }
        else{
            index=currentFunction.getParameterIndex(retType.name);
            if(index!=-1)
            {
                type=((Symbol)currentFunction.parameters.elementAt(index)).type;
            }
            else{
                index = currentFunction.getLocalIndex(retType.name);
                if(index!=-1){
                    type=((Symbol)currentFunction.locals.elementAt(index)).type;
                }
                else 
                {
                        index=sTable.getFunctionIndex(retType.name);
                        if(index!=-1)
                        {
                            type=((Function)sTable.functions.elementAt(index)).returntype;    
                        }    
                }
            }    
        }
    
        tType=type+new String(":")+new Integer(retType.line)+new String(":")+new Integer(retType.column);
    }
    }
    ;



factor returns [Symbol v]

{

    v=new Symbol(new String(""),new String(""),0,0);

    String exType;

    String errorStr;

    Vector argsVec;

    boolean isFunction=false;

    boolean isArray=false;

}    

    :

    (#(ID (LPAREN argument_list RPAREN))) => #(i:ID 

    {

    if(isFunctionLegal)

    {

        //Parse info

        String identifier;

        int line, column;

        String [] params = new String[3];

        

        identifier = i.getText();

        params = identifier.split(":");

        identifier = params[0];

        line = Integer.parseInt(params[1]);

        column = Integer.parseInt(params[2]);



        v = new Symbol(identifier, "", line, column);                            

                

    }

    }

    (LPAREN argsVec=argument_list RPAREN 

    {

    if(isFunctionLegal)

    {

        isFunction=true;

        String identifier;

        int line, column;

        String [] params = new String[3];

        

        identifier = i.getText();

        params = identifier.split(":");

        identifier = params[0];

        line = Integer.parseInt(params[1]);

        column = Integer.parseInt(params[2]);



        boolean errorVr=false;

        int index;

        index=sTable.getFunctionIndex(identifier);

        if(index!=-1)

        {

            currentCalledFunc=(Function)sTable.functions.elementAt(index);

            isCalledFunctionLegal=true;

        }

        else

        {

            isCalledFunctionLegal=false;

            error(FuncErr+currentFunction.name,line,column);

            error(ERR14,line,column);

        }



        if(isCalledFunctionLegal)

        {

            if(argsVec.size()!= currentCalledFunc.parameters.size())

                errorVr=true;

            else    

            for(int a=0;a<currentCalledFunc.parameters.size();a++)

            {

                //DEGISECEK

                if(((String)argsVec.elementAt(a)).indexOf("[")!=-1)

                {    

                    if(((Symbol)currentCalledFunc.parameters.elementAt(a)).type.indexOf("[")==-1)

                    {



                        errorVr=true;

                        break;

                    }    

                    

                    if((((String)argsVec.elementAt(a)).startsWith("float") &&

                    !((Symbol)currentCalledFunc.parameters.elementAt(a)).type.startsWith("float")) 

                    || (((String)argsVec.elementAt(a)).startsWith("int") &&

                    !((Symbol)currentCalledFunc.parameters.elementAt(a)).type.startsWith("int")) )

                    {



                        errorVr=true;

                        break;



                    }

                }

                else{

                    if(((Symbol)currentCalledFunc.parameters.elementAt(a)).type.indexOf("[")!=-1)

                    {



                        errorVr=true;

                        break;

                    }    

                    

                    if((((String)argsVec.elementAt(a)).startsWith("float") &&

                    !((Symbol)currentCalledFunc.parameters.elementAt(a)).type.startsWith("float")) 

                    || (((String)argsVec.elementAt(a)).startsWith("int") &&

                    !((Symbol)currentCalledFunc.parameters.elementAt(a)).type.startsWith("int")) )

                    {



                        errorVr=true;

                        break;



                    }

                



                }

            }

            

            if(errorVr)

            {

                //error(FuncErr+currentFunction.name,line,column);

                String protoFung,UseFung;

                String paramsStr=new String("");

                String arguments=new String("");

                

                for(int a=0;a<currentCalledFunc.parameters.size();a++)

                {

                    paramsStr+=((Symbol)currentCalledFunc.parameters.elementAt(a)).type;

                    if(a!=currentCalledFunc.parameters.size()-1)

                    paramsStr+=", ";

                }

                for(int a=0;a<argsVec.size();a++)

                {

                    arguments+=((String)argsVec.elementAt(a));

                    if(a!=argsVec.size()-1)

                    arguments+=", ";

                }

            

                errorStr=new String("Error 17: "+currentCalledFunc.name+"("+paramsStr+")"

                +" cannot be called with "+"("+arguments+")");

                

                error(FuncErr+currentFunction.name,line,column);

                error(errorStr,line,column);

            }

        }



    

    }

    }))


    |l:NUM 

    {

    if(isFunctionLegal)

    {

        //Parse info

        String identifier;

        int line, column;

        String [] params = new String[3];

        

        identifier = l.getText();

        params = identifier.split(":");

        identifier = params[0];

        line = Integer.parseInt(params[1]);

        column = Integer.parseInt(params[2]);



        v = new Symbol(identifier, "", line, column);



    }

    }

    |( #(ID (LBRAC expression RBRAC)?))=>#(j:ID

    {

    if(isFunctionLegal)

    {

        //Parse info

        String identifier;

        int line, column;

        String [] params = new String[3];

        

        identifier = j.getText();

        params = identifier.split(":");

        identifier = params[0];

        line = Integer.parseInt(params[1]);

        column = Integer.parseInt(params[2]);



        v = new Symbol(identifier, "", line, column);

    }

    }(LBRAC exType=expression RBRAC

    {

    if(isFunctionLegal)

    {

        isArray=true;

        if(exType.startsWith(new String("float")))

        {

            //Parse info

            String identifier;

            int line, column;

            String [] params = new String[3];

        

            identifier = exType;

            params = identifier.split(":");

            identifier = params[0];

            line = Integer.parseInt(params[1]);

            column = Integer.parseInt(params[2]);

            

            error(FuncErr+currentFunction.name,line,column);

            error(ERR11,line,column);        

        }

    }

    }

    )?

    {

    if(isFunctionLegal)

    {

            String identifier;

            int line, column;

            String [] params = new String[3];

        

            identifier = j.getText();

            params = identifier.split(":");

            identifier = params[0];

            line = Integer.parseInt(params[1]);

            column = Integer.parseInt(params[2]);

            

            int index;

            String type=new String("");

            index=currentFunction.getParameterIndex(identifier);

            if(index!=-1)

            {

                type=((Symbol)currentFunction.parameters.elementAt(index)).type;

            }

            else{

                index = currentFunction.getLocalIndex(identifier);

                if(index!=-1){

                    type=((Symbol)currentFunction.locals.elementAt(index)).type;

                }

                else 

                {



                        error(FuncErr+currentFunction.name,line,column);

                        error(ERR08,line,column);


                }

            }

            

            if(!type.equals("") && type.indexOf("[")!=-1 && !isArray)    

            {

                error(FuncErr+currentFunction.name,line,column);

                error(ERR15,line,column);

            }

            if(!type.equals("") && type.indexOf("[")==-1 && isArray)    

            {

                error(FuncErr+currentFunction.name,line,column);

                error(ERR16,line,column);

            }

            if(isArray)

                isArray=false;

    }

    }

    )

    | #(PARANTEZLISIN exType=expression

    {

    if(isFunctionLegal)

    {

        String identifier;

        int line, column;

        String [] params = new String[3];

        

        identifier = exType;

        params = identifier.split(":");

        identifier = params[0];

        line = Integer.parseInt(params[1]);

        column = Integer.parseInt(params[2]);



        v = new Symbol(identifier, "", line, column);                        

    }

    }

        )

    ;



        

expression_list returns [Vector args]
{
    args=new Vector();
    String argType;
}
    :
    (argType=expression
    {    
    if(isFunctionLegal)
    {
        String identifier;
        int line, column;
        String [] params = new String[3];
        
        identifier = argType;
        params = identifier.split(":");
        identifier = params[0];
        line = Integer.parseInt(params[1]);
        column = Integer.parseInt(params[2]);
        args.add(identifier);
    }
    }
    )+
    ;

argument_list returns [Vector args]
{
    args=new Vector();

} 
    :
    args=expression_list 
    |
    
    ;


      


More information about the antlr-interest mailing list