[antlr-interest] newbie question- tree walker goes into infininte loop

Duygu Altinok duygu_the_duygu at yahoo.com
Fri Jan 22 17:01:21 PST 2010


Hi,
I'm writing a C-like language compiler . I both generate code and do usual stuff within my tree walker but , it goes into an infinite loop with the following , problem is with the function part. I really didin't get what I'm doing wrong , so please can anybody help?Thanx in advance.

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!  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());

}

LPAREN!  parameter_list!  RPAREN!  LCURLY  function_body  RCURLY

{

    symbol_table.addChild(#([SYMBOL_FUNCTION, identifier ], [SYMBOL_TYPE, bt] , symbol_parameters, symbol_locals ));

#function=(#([ID,identifier],function));
}
    ;

function_body:
            declaration_list! statement_list
         ;


Tree walker:

program
    :
    #(PROGRAM symbol_table
    {
        sTable.sort();
assemble.code+=new String("\n\t#Duygu\n\n\n");
assemble.code+=new String("\t.data\n");

    }  function_list
    {
    sTable.prettyPrint();
            try{
        FileWriter file=new FileWriter(new String("output.asm"));
            file.write(assemble.code.toCharArray());
            file.close();
        }catch (Exception e) {
                e.printStackTrace();
                    System.out.println(e);
        }
    }
    )

    ;
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);
                Vector parameters=currentFunction.parameters;
        int offset=0;
        for(int k=0;k<parameters.size();k++)
  {
        ((Symbol)parameters.elementAt(k)).place.isMemory=true;
    if(((Symbol)parameters.elementAt(k)).type.indexOf('[')!=-1)
        ((Symbol)parameters.elementAt(k)).place.isArray=true;
    else
        ((Symbol)parameters.elementAt(k)).place.isArray=false;
    ((Symbol)parameters.elementAt(k)).place.store=new String(Integer.toString(offset)+"($fp)");
    offset+=4;
        }

        if(identifier.equals(new String("main")))

        {

            firstFunc=false;

            firstName=new String(identifier);

            assemble.code+=new String("\t.text\n");

            assemble.code+=new String("\n\t.globl\tmain\n");

            assemble.code+=new String("main:\n");

        }

        else if (identifier.equals(firstName))

        {

            assemble.code+=new String("\n\t.globl\tmain\n");

            assemble.code+=new String("main:\n");

        }

        else

        {

            assemble.code+=new String("\n\t.globl\t"+currentFunction.name+"\n");

            assemble.code+=new String(currentFunction.name+":\n");

        }

        assemble.code+=new String("#Initialize function\n");

        String reg=new String();

        reg=new String("$fp");

        assemble.code+=new String("#fp\n");

        assemble.code+=Push(reg);

        assemble.code+=new String("\taddi $fp, $sp, 4\n");

        reg=new String("$ra");

        assemble.code+=new String("#ra\n");

        assemble.code+=Push(reg);
   assemble.code+=new String("#push s registers to the stack\n");

        for(int ind=0;ind<8;ind++)

            assemble.code+=Push(new String("$s"+Integer.toString(ind)));




        Vector locals=currentFunction.locals;

        for(int locVar=0;locVar<locals.size();locVar++)

        {

            //local icin stack'ten yer ayir... sp'yi asagi cekerek.

            String type = ((Symbol)locals.elementAt(locVar)).type;

            assemble.code+=new String("#space for local in the stack\n");

            if(type.trim().equals("int") || type.trim().equals("float"))

            {

                assemble.code+=new String("\taddi $sp, $sp, -4\n");

                currentFunction.ARoffset-=4;

                ((Symbol)locals.elementAt(locVar)).place.isArray = false;

            }

            else if(type.indexOf('[')!=-1)

            {

   ((Symbol)locals.elementAt(locVar)).place.isArray = true;

                int size=GetArrSize(type);

                for(int j=0;j<size;j++)

                {

                    assemble.code+=new String("\taddi $sp,$sp, -4\n");

                    currentFunction.ARoffset-=4;

                }

            }



            ((Symbol)locals.elementAt(locVar)).place.store = new String(Integer.toString(currentFunction.ARoffset)) + "($fp)";

            ((Symbol)locals.elementAt(locVar)).place.isMemory = true;


        }



    }
        else
        isFunctionLegal=false;



}
   function_body

    {



        //Stack Bookkeeping

        if(!currentFunction.ret)

        {

        assemble.code+=new String("#Stack bookkeeping\n");

        assemble.code+=new String("\taddi $sp, $fp, -40\n");

        //restore callee saved regs

        assemble.code+=new String("#pop s registers\n");

        for(int ind=7;ind>=0;ind--)

            assemble.code+=Pop(new String("$s"+Integer.toString(ind)));

        //restore ra

        assemble.code+=new String("#pop ra\n");

        assemble.code+=Pop(new String("$ra"));

        //restore fp

        assemble.code+=new String("#pop fp\n");

        assemble.code+=Pop(new String("$fp"));
        //back to caller fnc

        assemble.code+=new String("\tli $v0, 0\n");

        assemble.code+=new String("\tjr $ra\n");

        }

    }
)
    ;

function_body:
    statement_list
    ;

statement_list:
    (statement)+
    ;

statement:
          |assignment_statement
      |return_statement
      |if_statement
      |while_statement
         | print_statement
          | expression SEMI
          | read_statement
      ;


      


More information about the antlr-interest mailing list