Okay, my teacher assigned my class to create a C++ program that reads in a postfix expression, create an expression tree (basically a series of nodes with 2 pointers each and a value), and then print out the initial expression in infix notation. He gave us a class to use called tokenizer that is supposed to handle the input. However, I think he expects us to know a little too much and I am a bit lost. Here is the code for the tokenizer:
// Take a postfix notation expression and convert it to infix
//
#include "tokenizer.hpp"
#include <iostream>
#include <stack>
#include <string>
usingnamespace std;
class ETree
{
public:
void addNode(const Tokenizer::Token& val)
{
if (nodeStack.empty() == true)
{
Node* root;
root->tok = val;
root->left = NULL;
root->right = NULL;
nodeStack.push(root);
}
else
{
Node* newNode1;
if (val.type() == Tokenizer::TOKEN_OP)
{
newNode1->tok = val;
Node* operand1 = nodeStack.top();
nodeStack.pop();
Node* operand2 = nodeStack.top();
nodeStack.pop();
newNode1->left = operand1;
newNode1->right = operand2;
nodeStack.push(newNode1);
}
elseif (val.type() == Tokenizer::TOKEN_NUM)
{
newNode1->tok = val;
newNode1->left = NULL;
newNode1->right = NULL;
nodeStack.push(newNode1);
}
else
{
cout << "Value(s) not acceptable";
delete newNode1;
}
}
}
void writeInfix(ostream& out)
{
writeInfix(nodeStack.top(), out);
}
private:
struct Node
{
Tokenizer::Token tok;
Node* left;
Node* right;
};
stack<Node*> nodeStack;
void writeInfix(Node* root, ostream& out)
{
switch(root->tok.type())
{
case Tokenizer::TOKEN_NUM:
out << root->tok.numValue();
break;
case Tokenizer::TOKEN_OP:
out << '(';
writeInfix(root->left, out);
out << root->tok.opValue();
writeInfix(root->right, out);
out << ')';
break;
}
}
};
std::ostream& operator<<(std::ostream& out, const Tokenizer::Token& tok);
int main()
{
Tokenizer t;
Tokenizer::Token tok;
ETree tree1;
do
{
do
{
tok = t.nextToken();
std::cout << "token: " << tok << std::endl;
if (!tok.isEOS())
{
tree1.addNode(tok);
}
} while (!tok.isEOS());
tree1.writeInfix(cout);
std::cout << "EOS" << std::endl;
} while (!tok.isEOF());
return 0;
}
std::ostream& operator<<(std::ostream& out, const Tokenizer::Token& tok)
{
switch(tok.type())
{
case Tokenizer::TOKEN_NUM:
out << "NUM:" << tok.numValue(); break;
case Tokenizer::TOKEN_VAR:
out << "VAR:" << tok.varValue(); break;
case Tokenizer::TOKEN_OP:
out << "OP:" << tok.opValue(); break;
case Tokenizer::TOKEN_EOF:
out << "EOF**"; break;
case Tokenizer::TOKEN_EOS:
out << "EOS**"; break;
}
return out;
}
Basically, when I run my code, I get an error saying the following:
"An unhandled exception of type 'System.AccessViolationException' occurred in PracticeAssignment.exe
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
and the line it refers to is the "newNode1->tok = val;" line beneath the "if (val.type() == Tokenizer::TOKEN_OP)" statement. I really don't know how to proceed from here, so any help is appreciated.
Just an update, I think the problem is just adding another value to the tree, since I got that same error but it was referring to the "newNode1->tok = val;" line after the "else if (val.type() == Tokenizer::TOKEN_NUM)" line.