I'm writing a compiler for a subset of the Java language and am at my wit's end with this bug I've encountered.
This is how things look internally:
Code:
classes variables
P * ------> x int -4
Q * y int -8
z boolean -12
methods parameters
M int * ------> a int 0
* variables (locals)
| d int -4
|
|
-------> ParseTree
corresponding to this code:
Code:
class P {
//variables
int x;
int y;
boolean z;
// methods
int M(int a) { // <---parameters
// variables (locals)
int d;
// some code... (this goes in the ParseTree)
}
}
class Q {
// empty
}
I'm having no trouble with the compiler writing process, but rather with some coding: copying a ParseTree then accessing it later. Basically what happens, is that when a class extends another, it inherits the base class' methods and those methods' ParseTrees as well.
What I want to be able to do is copy the ParseTree and stick it in the class' method's spot where it belongs, and also put a pointer to it in a deque. Then later I'll cycle through all the ParseTree*s in the deque for other processing.
When I try and access the ParseTrees from the deque I'm getting a segfault, which I'm unable to correct. I've been in & out of gdb all day and can't find the error. This is my first large scale project that I've written in C++ so I'm having a little trouble with memory management, but it seems like I'm not A) copying the ParseTree correctly, or B) I'm not putting it in the deque correctly, or C), I'm not accessing it from the deque correctly.
Now, this is a school project so I don't think I should be posting my source, but I'll provide what I think is sufficient.
Email me if you need more. jdgoettsch AT ucdavis DOT edu
Code:
Inserting the ParseTree:
ParseTree* ppp;
ppp = new ParseTree( mit->second.Get_PT_Ref() );
gst->methods.find(mit->first)->second.Insert_ParseTree(ppp);
q2.push_back(ppp);
// gst is a GlobalSymbolTable*
// GlobalSymbolTable has a member multimap<char*, EntityAttribute> methods;
// mit points to a map that holds the methods:
// multimap<char*, EntityAttribute>* mit;
// EntityAttribute holds a ParseTree*
// ParseTree& EntityAttribute::Get_PT_Ref() {return *parse_code}
and the ParseTree copy constructor:
Code:
ParseTree::ParseTree(ParseTree& p) {
root = p.root;
}
// root is a private member of ParseTree that points to the first node
Node::Node(Node& n) {
first_child = n.first_child;
second_child = n.second_child;
third_child = n.third_child;
owning_class = n.owning_class; // just a char*
owning_method = n.owning_method; // just a char*
t = n.t;
}
// first_child, etc, are Node*
then accessing through the deque:
Code:
while(!q2.empty()) {
cout << "\nDeque size: " << q2.size() << endl;
q2.front()->DFS(); // it segfaults here
q2.pop_front();
// q2 is deque<ParseTree*> q2
// void ParseTree::DFS() is a depth-first search
// that runs perfectly when called from other locations
// (which is why I think this part of the code is ok)
I should also add the everything compiles fine with g++, calling g++ -Wall -ansi ...