Link to home
Start Free TrialLog in
Avatar of savante
savante

asked on

Linked list issues

It's probably been 3 years since I've worked with a linked list, and as a result, I'm sure my error is very basic, but I just can't figure it out.  I've been provided with my teacher's code, and I did the best I could, but it still provides a problem.

Basically, I've written an interpreter for a language he's given us.  I'm having problems with the parser, though.  The tokenizer works great.  The input I'm trying to deal with is :

program int X , Y , XY;

It skips over the program fine, no problem.  The next part (int X , Y, XY; ) is an id-list, and that's an issue.  We have to check to first make sure the Id is not already declared before we use the constructor, otherwise, it shouldn't be a part of the list.  The program works great for a bit, as it reads in the X, and sets it to the first node.  Then, the second time through, the parseNode procedure has an error in the list.  I'm thinking it's a result of me building the linked list incorrectly, but I've bolded the line that causes an issue.  Down below that is the debug output I've added.  

void Idlist::parse()
            {
                  cout << "Parsing id-list";
                  int tokNo, tokVal;
                  char idName[10];
                  ParseTree token;
                  
                  token.skipToken();
                  token.getToken(tokNo, tokVal, idName);

                  cout << idName;
//outputs X the first time through, Y the second
      
                  Id2* id2;
                  id = (id2->parseId());
                  token.skipToken();
                  token.getToken(tokNo, tokVal, idName);
                  if (tokNo == COMMA)
                  {

                        cout << "Choosing second alternative";
                        idlist->parse();
                  }
                  
            }


So, in this section, basically, the token 'int' is skipped, leading to an id.  The id is in the current token, and getToken reads it in (does not skip it).  Then, the X is parsed correctly, returning back.  It skips over the X, then getToken returns that it is a comma and there are more values, so it recursively calls itself.  The second time through, it starts off by skipping the comma, then the getToken returns the value Y.  In parseID, below, there becomes problems.


#include "Id2.h"
#include "ParseTree.h"

#include <iostream>

class ParseTree;
   
Id2::Id2(char n1[10], Id2* nid) {
           // private constructor; used by parseId()
           strcpy(name, n1);
           decl = false; init = false; nextId = nid; }

     
Id2* Id2::parseId() {

      cout << "Parsing ID"; //prints out twice
          char n2[10]; Id2* candId;

              int tokNo, tokVal;

              ParseTree token;

              //token.skipToken();
              token.getToken(tokNo, tokVal, n2);


              if (firstId != NULL)
              {
                    candId = Id2::firstId;
                    cout << "Before for"; //This  prints out twice
          for(candId = Id2::firstId; nextId != NULL; candId = nextId)
              {
                    cout << "in loop";
                         //This line never prints, and on the second go round, it should.
               if (strcmp(n2, candId->idName()) == 0) return candId;


              }
              }
          // if not, create a new Id, make that the firstId, and return
          // that.
          candId = new Id2(n2, Id2::firstId);
              Id2::firstId = candId;
              cout << "Id name = " << candId->idName();
          return candId;
        }
 
int Id2::getIdval() {
      if (!init)
      {
            cout<< "error; uninitialized var";
            exit(1);}
                        return currVal; }
 
void Id2::setIdval(int x) { init = true; currVal = x; }
 
char* Id2::idName() { return name; }




The error is simply an exception error, and when I run it through the Windows debugger, it occurs at the for line in parseId.  The contId does get set to firstId, which is good , but 'this' is set to 0xcccccc.   Therefore, I think it's a problem with next, as it never enters the for loop (and prints out "In for loop") .  I tried earlier using static arrays for the ID names, values, and if they were initialized at all, but it didn't work, as the static variables had problems with references in the member functions.

          for(candId = Id2::firstId; nextId != NULL; candId = nextId)  is the line where it's erroring, then.   Since candId is set to Id2::firstId, that's not the issue.  It's with either one of the nextId statements, leading me to believe (very possibly) that I set it up wrong.

Thanks in advance for your help.  Any insight at all would be helpful.. I've been staring at my code for the last 2 days and I've come to no conclusions.

-Brett
ASKER CERTIFIED SOLUTION
Avatar of mrwad99
mrwad99
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Three way split between all participants.