A mini-compilier I made stuck in an infinite loop (lots of code)

This is a discussion on A mini-compilier I made stuck in an infinite loop (lots of code) within the C++ Programming forums, part of the General Programming Boards category; I made a mini-compiler that takes in programs from .txt files and runs through them to find their return value. ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Post A mini-compilier I made stuck in an infinite loop (lots of code)

    I made a mini-compiler that takes in programs from .txt files and runs through them to find their return value. The programs are very simple and in a very, very specific syntax, for instance there must be a space between each token, it must take an int as an argument, and it must return an int. The code runs fine for many programs, but it does not work for a specific program I wrote for it. I have some of the example programs I wrote that run correctly as well as the program that gets stuck in an infinite loop.

    The compiler works by first putting each token of the program into a vector of strings. There is a program counter (int PC) that keeps track of where you are in the vector. Because each program is required to start a specific way, PC is actually initialized to 10, where the code truly begins. There are always two variables- int a and int n. int a is a local variable and always initialized to 0, while the user is prompted to give a value for int n. The variables are kept track of in a stack (named execution).

    When a recursive call is made, the PC that you want to return to is pushed (PC-4), then the argument is pushed, then another local variable initialized to 0 is pushed. On a return, if there the stack isnít empty after popping the local variable and argument, there must be the PC-4 that you pushed earlier for recursion. PC is set to whatever this number is, then that number is popped as well.

    Here is the code itself. Itís big (~1400 lines), but most of it is if-else's and I tried to comment as much as I could. If you can follow the code, itís not too hard to jump around the if-else's and keep track of where you are.

    Cminus-minus.cpp:
    Code:
    #include <fstream>
    #include <iostream>
    #include <vector>
    #include <string>
    #include <stack>
    #include <sstream>
    
    using namespace std;
    
    int main(int argc, char *argu[])
    {
        ifstream file(argu[1]);
        vector<string> token;
        int val, PC, returnVal;
        stack<int> execution;
        bool recursionDone, skipLast;
        
        if (!file) 
        {
            cout << "file not found" << endl;
            exit(1); // terminate with error
        }
        
        for (int i = 0; file.is_open(); i++)
        {
            string temp;
            file >> temp;
            token.push_back(temp);
        
            if (file.eof())
                file.close();
        }
        
        if (token[0] != "int")
           cout << endl << "Syntax error: expected 'int' at 1st token.";
        if (token[2] != "(")
           cout << endl << "Syntax error: expected '(' at 3rd token.";
        if (token[3] != "int")
           cout << endl << "Syntax error: expected 'int' at 4th token.";
        if (token[7] != "int")
           cout << endl << "Syntax error: expected 'int' at 8th token.";
        if (token[9] != ";")
           cout << endl << "Syntax error: expected ';' at 10th token.";
           
        cout << "Integer value? ";
        cin >> val;
        
        //execute the function
        execution.push(val); //parameter value
        execution.push(0); //local variable initialized to 0
        
        recursionDone = false;
        skipLast = false;
        
        for (PC = 10; PC < token.size(); PC++)
        {
    //-------------------------------------------------
    //ASSIGNMENT
    //-------------------------------------------------
       //--------------------
       //token[PC] = a
       //--------------------
            if (token[PC] == token[4]) //n
            {
                PC++;
                if (token[PC] == "=")
                {
                    PC++;
                    if (token[PC] == token[4]) //n = n wont do anything
                    {
                       PC++;
                    }
                    else if (token[PC] == token[8]) //n = a
                    {
                       int temp = execution.top();
                       execution.pop();
                       execution.pop();
                       execution.push(temp);
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //n = f(x) recursion
                    {  
                       PC += 2;
                       if (!recursionDone)  // into the recursion
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();  // a
                             execution.pop();
                             int temp2 = execution.top();  // n
                             execution.push(temp1);  // push a back
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);  // a
                             execution.push(0);
                            // cout << "n = fib ( a )" << endl;
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else  // returning from recursion
                       {
                          int temp = execution.top();
                          execution.pop();  // a
                          execution.pop();  // n
                          execution.push(returnVal);
                          execution.push(temp);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n = #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = v;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                }
                else if (token[PC] == "+=")
                {
                    PC++;
                    if (token[PC] == token[4]) //n += n
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 += temp2;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //n += a
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 += temp1;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //n += f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                            // cout << "n += fib ( a ). temp = " << temp << endl;
                            // cout << "returnVal = " << returnVal << endl;
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp1 = execution.top();
                          execution.pop();
                          int temp2 = execution.top();
                          execution.pop();
                          execution.push(temp2 + returnVal);
                          execution.push(temp1);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n += #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 += v;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                }
                else if (token[PC] == "-=")
                {
                    PC++;
                    if (token[PC] == token[4]) //n - n = 0
                    {
                       int temp = execution.top();
                       execution.pop();
                       execution.pop();
                       execution.push(0);
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //n -= a
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 -= temp1;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //n -= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp1 = execution.top();
                          execution.pop();
                          int temp2 = execution.top();
                          execution.pop();
                          execution.push(temp2 - returnVal);
                          execution.push(temp1);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n -= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 -= v;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                }
                else if (token[PC] == "*=")
                {
                    PC++;
                    if (token[PC] == token[4]) //n *= n
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 *= temp2;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //n *= a
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 *= temp1;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
    		        else if (token[PC] == token[1]) //n *= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp1 = execution.top();
                          execution.pop();
                          int temp2 = execution.top();
                          execution.pop();
                          execution.push(temp2 * returnVal);
                          execution.push(temp1);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n *= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 *= v;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                }
                else if (token[PC] == "/=")
                {
                    PC++;
                    if (token[PC] == token[4]) //n / n = 1
                    {
                       int temp = execution.top();
                       execution.pop();
                       execution.pop();
                       execution.push(1);
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //n /= a
                    {
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 /= temp1;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //n /= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp1 = execution.top();
                          execution.pop();
                          int temp2 = execution.top();
                          execution.pop();
                          execution.push(temp2 / returnVal);
                          execution.push(temp1);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n /= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp1 = execution.top();
                       execution.pop();
                       int temp2 = execution.top();
                       temp2 /= v;
                       execution.pop();
                       execution.push(temp2);
                       execution.push(temp1);
                       PC++;
                    }
                }
            }
       //--------------------
       //token[PC] = a
       //--------------------
            else if (token[PC] == token[8]) //a
            {
                PC++;
                if (token[PC] == "=")
                {
                    PC++;
                    if (token[PC] == token[4]) //a = n
                    {
                       execution.pop();
                       int temp = execution.top();
                       execution.pop();
                       execution.push(temp);
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //a = a wont do anything
                    {
                       PC++;
                    }
                    else if (token[PC] == token[1]) //a = f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          execution.pop();
                          execution.push(returnVal);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //a = #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp = execution.top();
                       execution.pop();
                       temp = v;
                       execution.push(temp);
                       PC++;
                    }
                }
                else if (token[PC] == "+=")
                {
                    PC++;
                    if (token[PC] == token[4]) //a += n
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp += execution.top();
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //a += a
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp += temp;
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //a += f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp = execution.top();
                          execution.pop();
                          execution.push(temp + returnVal);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //a += #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp = execution.top();
                       execution.pop();
                       temp += v;
                       execution.push(temp);
                       PC++;
                    }
                }
                else if (token[PC] == "-=")
                {
                    PC++;
                    if (token[PC] == token[4]) //a -= n
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp -= execution.top();
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //a - a = 0
                    {
                       execution.pop();
                       execution.push(0);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //a -= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp = execution.top();
                          execution.pop();
                          execution.push(temp - returnVal);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //a -= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp = execution.top();
                       execution.pop();
                       temp -= v;
                       execution.push(temp);
                       PC++;
                    }
                }
                else if (token[PC] == "*=")
                {
                    PC++;
                    if (token[PC] == token[4]) //a *= n
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp *= execution.top();
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //a *= a
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp *= temp;
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //a *= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp = execution.top();
                          execution.pop();
                          execution.push(temp * returnVal);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //a *= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp = execution.top();
                       execution.pop();
                       temp *= v;
                       execution.push(temp);
                       PC++;
                    }
                }
                else if (token[PC] == "/=")
                {
                    PC++;
                    if (token[PC] == token[4]) //a /= n
                    {
                       int temp = execution.top();
                       execution.pop();
                       temp /= execution.top();
                       execution.push(temp);
                       PC++;
                    }
                    else if (token[PC] == token[8]) //a / a = 1
                    {
                       execution.pop();
                       execution.push(1);
                       PC++;
                    }
                    else if (token[PC] == token[1]) //a /= f(x)
                    {
                       PC += 2;
                       if (!recursionDone)
                       {
                          if (token[PC] == token[4]) //f(n)
                          {
                             int temp1 = execution.top();
                             execution.pop();
                             int temp2 = execution.top();
                             execution.push(temp1);
                             execution.push(PC-4);
                             execution.push(temp2);
                             execution.push(0);
                          }
                          else if (token[PC] == token[8]) //f(a)
                          {
                             int temp = execution.top();
                             execution.push(PC-4);
                             execution.push(temp);
                             execution.push(0);
                          }
                          else //f(#)
                          {
                             int v;
                             istringstream iss(token[PC]);
                             iss >> v;
                             execution.push(PC-4);
                             execution.push(v);
                             execution.push(0);
                          }  
                          PC = 9; //reset PC (one gets added at end of for loop)
                       }
                       else
                       {
                          int temp = execution.top();
                          execution.pop();
                          execution.push(temp / returnVal);
                          recursionDone = false;
                          PC += 2;
                       }
                    }
                    else //n /= #
                    {
                       int v;
                       istringstream iss(token[PC]);
                       iss >> v;
                       int temp = execution.top();
                       execution.pop();
                       temp /= v;
                       execution.push(temp);
                       PC++;
                    }
                }
            }
    //-------------------------------------------------
    //IF-ELSE STATEMENT
    //-------------------------------------------------
    
    //if true, move PC to the if-then clause, then move PC past the else clause
    //if false, move PC to the else-then clause
    
            else if (token[PC] == "if")
            {
                PC++;
                PC++;
                
       //--------------------
       //token[PC] = n
       //--------------------
                if (token[PC] == token[4])//n
                {
                   PC++;
                   //==, !=, >, >=, <, <=
                   if (token[PC] == "==")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n == n, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else if (token[PC] == token[8]) //n == a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() == temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n == #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() == v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           } 
                          execution.push(temp);
                      }
                   }
                   if (token[PC] == "!=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n != n, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else if (token[PC] == token[8]) //n != a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() != temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n != #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() != v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           } 
                          execution.push(temp);
                      }
                   }
                   if (token[PC] == ">")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n > n, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else if (token[PC] == token[8]) //n > a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() > temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n > #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() > v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           } 
                          execution.push(temp);
                      }
                   }
                   if (token[PC] == ">=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n >= n, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else if (token[PC] == token[8]) //n >= a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() >= temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n >= #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() >= v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                          execution.push(temp);
                      }
                   }
                   if (token[PC] == "<")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n < n, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else if (token[PC] == token[8]) //n < a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() < temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n < #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() < v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           } 
                          execution.push(temp);
                      }
                   }
                   if (token[PC] == "<=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //n <= n, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else if (token[PC] == token[8]) //n <= a
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() <= temp)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else //n <= #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           int temp = execution.top();
                           execution.pop();
                           if (execution.top() <= v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                          execution.push(temp);
                      }
                   }
                }
       //--------------------
       //token[PC] = a
       //--------------------
                else if(token[PC] == token[8])//a
                {
                   PC++;
                   //==, !=, >, >=, <, <=
                   if (token[PC] == "==")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a == n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp == execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] == token[8]) //a == a, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else //a == #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           if (execution.top() == v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                   else if (token[PC] == "!=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a != n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp != execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] != token[8]) //a != a, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else //a != #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           if (execution.top() != v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                   else if (token[PC] == ">")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a > n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp > execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] != token[8]) //a > a, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else //a > #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           if (execution.top() > v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                   else if (token[PC] == ">=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a >= n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp >= execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] == token[8]) //a >= a, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else //a >= #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           if (execution.top() >= v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                   else if (token[PC] == "<")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a < n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp < execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] != token[8]) //a < a, always false
                      {
                           while(token[PC] != ";")//moves to else-then assignment
                              PC++;
                           PC++;
                      }
                      else //a < #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           //cout << "in if (a < #), # = " << v << endl;
                           if (execution.top() < v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                   if (token[PC] == "<=")
                   {
                      PC++;
                      if (token[PC] == token[4]) //a <= n
                      {
                           int temp = execution.top();
                           execution.pop();
                           if (temp <= execution.top())
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                           execution.push(temp);
                      }
                      else if (token[PC] == token[8]) //a <= a, always true
                      {
                           PC++; //moves to if-then assignment
                      }
                      else //a <= #
                      {
                           int v;
                           istringstream iss(token[PC]);
                           iss >> v;
                           if (execution.top() <= v)
                              PC++; //moves to if-then assignment
                           else
                           {
                              while(token[PC] != ";")//moves to else-then assignment
                                 PC++;
                              PC++;
                           }
                      }
                   }
                }
            }     
            else if (token[PC] == "else") 
            {//IMPORTANT: The only time it makes it to this else statement is if the if statement was true.
             //If the if statement was false, it moves directly to the else-then clause and skips the actual "else".
                 while(token[PC] != ";")//skips over else-then clause
                    PC++;
            }
            
    //-------------------------------------------------
    //RETURN STATEMENT
    //-------------------------------------------------
            else if (token[PC] == "return")
            {
               PC++;
               //cout << "PC = " << PC;
               recursionDone = true;
               if (token[PC] == token[4]) //return n ;
               {
                  execution.pop();
                  returnVal = execution.top();
                  execution.pop();
    	          if (!execution.empty())
    	          {
    		         PC = execution.top() - 1; //because it adds one after for loop
    		         execution.pop();
                  }
               }
               else if (token[PC] == token[8]) //return a ;
               {
                  returnVal = execution.top();
                  execution.pop();
                  execution.pop();
                  if (!execution.empty())
    	          {
    		         PC = execution.top() - 1;
    	             execution.pop();
                  }
               }
               else // return # ;
               {
                  istringstream iss(token[PC]);
                  iss >> returnVal;
                  execution.pop();
                  execution.pop();
    	          if (!execution.empty())
    	          {
    		         PC = execution.top() - 1;
    		         execution.pop();
                  }
               }
            }
        }
        
        cout << "Returned value: " << returnVal << endl;
                 
        return 0;
    };
    Here are some examples of programs that do work. Notice that there is recursion, but at most one recursive call in each program.

    boostSmall.txt:
    Code:
    int boostSmall ( int n ) 
    { 
    	int a ;
    
    	if ( n < 5 )
    		n *= 3 ;
    	else
    		n = n ;
    	return n ; 
    }
    factorial.txt:
    Code:
    int fact ( int n ) 
    { 
    	int a ;
    
    	a = n ;
    	a -= 1 ;
    	if ( n == 0 )
    		n = 1 ;
    	else
    		n *= fact ( a ) ;
    	return n ;
    }
    log.txt: (its base 2)
    Code:
    int log ( int n ) 
    { 
    	int a ;
    
    	a = n ;
    	a /= 2 ;
    	if ( n == 1 )
    		n = -1 ;
    	else
    		n = log ( a ) ;
    	n += 1 ;
    	return n ;
    }
    All of the above run fine in my program. However, the following does not work.

    fibonacci.txt
    Code:
    int fib ( int n ) 
    { 
    	int a ;
    
    	a = n ;
    	a -= 1 ;
    	if ( n < 2 )
    		n = 1 ;
    	else
    		n = fib ( a ) ;
    	a -= 1 ;
    	if ( a < 0 )
    		n = 1 ;
    	else
    		n += fib ( a ) ;
    	return n ;
    }
    fibonacci.txt gets stuck in an infinite loop and never finishes. If you look at my code, youíll see that I commented out a few ďcout << ď statements that I was using to try and debug. These gave me a couple clues. First, it is stuck calling ďn += fib ( a ) ;Ē over and over again. Second, the return value (int returnVal) does not change after it reaches eleven. Third, and probably most important, the code never enters the ďif (a < #)Ē section.

    I was looking at this all day yesterday and Iím completely stumped as to why it wonít work. The biggest thing with two recursive calls in one program was setting the global recursionDone Boolean back to false where I did, but that still doesnít fix it. It may just be some problem with my PC counter, but I donít know.

    I know this is a ton of code to dig through for this one problem. Something that may help is commenting out things you know isnít needed for the program. For instance, ďa > __Ē is never called in the program so you can just comment it out for now so itís easier to skip over. Doing this would reduce the amount of code significantly.

    Either way, I really appreciate anyone attempting to take a look at it. I know my description of how the compiler works wasnít great, so Iíll answer any questions you might have.

    Thanks.

    -Dan

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >fibonacci.txt gets stuck in an infinite loop and never finishes.
    Because it never processes a return token. Step through your code with a small test where you know all of the steps and when they happen. The first place that it doesn't step where you expect is going to give you a sign of what the problem is.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Infinite Loop HELP!!
    By victorti83plus in forum C++ Programming
    Replies: 5
    Last Post: 10-20-2002, 08:10 PM
  2. Stuck in a loop!.....Get me out of here!!
    By rabmaz in forum C Programming
    Replies: 3
    Last Post: 09-01-2002, 09:16 AM
  3. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  4. I am stuck in a loop!!!
    By cujo77 in forum C++ Programming
    Replies: 2
    Last Post: 03-24-2002, 08:52 AM
  5. Infinite Loop!!!
    By catmom in forum C++ Programming
    Replies: 9
    Last Post: 12-11-2001, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21