Dire need of assistance

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#ifndef _TOKENIZER_H_
#define _TOKENIZER_H_

#include <iostream>
#include <cctype>
#include <stdexcept>
#include <cstring>

class Tokenizer
{
public:

    enum token_type { TOKEN_NUM, TOKEN_OP, TOKEN_VAR, TOKEN_EOS, TOKEN_EOF };

    class Token
    {
    public:
	Token(token_type type=TOKEN_EOS) : _type(type)
	    { }

	Token(double nval) : _type(TOKEN_NUM)
	    {
		_value.num = nval;
	    }

	Token(token_type type, char cval) : _type(type)
	    {
		_value.c = cval;
	    }

	
	// the type of this token
	token_type type() const { return _type; }


	// get the value (make sure the method you call jives with the type)

	double numValue() const { return _value.num; }

	char opValue() const { return _value.c; }

	char varValue() const { return _value.c; }


	// is it the end-of-file token?
	bool isEOF() const
	    {
		return _type == TOKEN_EOF;
	    }

	// is it the end-of-statement token?
	bool isEOS() const
	    {
		return (_type == TOKEN_EOS || _type==TOKEN_EOF);
	    }



    private:
	token_type _type;

	union 
	{
	    double	num;
	    char	c;
	} _value;
    };

    
    Tokenizer(std::istream& in = std::cin) : in_stream(in)
	{ }


    Token nextToken()
	{
	    // slurp whitespace
	    char c;

	    do
	    {
		in_stream.get(c);
	    }
	    while  (in_stream.good() && std::isspace(c));


	    // end of statement
	    if (c == ';')
	    {
		return Token(TOKEN_EOS);
	    }

	    if (c=='!')
	    {
		return Token(TOKEN_EOF);
	    }

	    if (! in_stream.good())
	    {
		// special EOF token
		return Token(TOKEN_EOF);
	    }


	    if (std::isdigit(c))
	    {
		double dblval;

		in_stream.putback(c);
		in_stream >> dblval;

		return Token(dblval);
	    }
	    else if (std::isalpha(c))
	    {
		return Token(TOKEN_VAR, c);
	    }
	    else if ( std::strchr("+*-/=", c) != NULL ) // valid operator
	    {
		return Token(TOKEN_OP, c);
	    }
	    else
	    {
		throw std::runtime_error("illegal character in input");
	    }
	}

private:
    std::istream& in_stream;
};





#endif	// _TOKENIZER_H_ 


and here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Take a postfix notation expression and convert it to infix
//

#include "tokenizer.hpp"
#include <iostream>
#include <stack>
#include <string>

using namespace 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);
			}

			else if (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.
Topic archived. No new replies allowed.