S-Expression Tree of strings and symbols

Hello friendly people on this site who are smarter than me in C++.
I am teaching myself about s-expression trees in C++ and trying to make one. It's a very basic one, but even a basic one still seems very involved. I'm parsing characters into a string then the string into a vector then I'm getting stuck putting that into a working tree. I put a '*' at the end of the string when I attempted to tokenize it, but that froze up (for some odd reason). So it seems like the tokenizer function doesn't work, but doesn't error (making it weird), so I've just been using the vector in main(). Unlike the arithmetic evaluator, I want this to take strings between booleans (so like ((hope&pray)|fail_miserably)). I'm not getting on well with parsing the parentheses but I think this can be resolved later. I've been looking here http://www.cplusplus.com/forum/general/94528/
http://www.cplusplus.com/forum/beginner/93831/
http://en.wikipedia.org/wiki/S-expression
http://stackoverflow.com/questions/9435400/correct-way-of-parsing-s-expressions-in-oop?rq=1

I've not seen anything quite like what I'm trying to solve. Any constructive advice would be helpful.

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
  #pragma once
#include <fstream>
#include <cstring>
#include <ctype.h>
// set_union example
#include <iostream>     // std::cout
#include <algorithm>    // std::set_union, std::sort
#include <vector>       // std::vector
#include <set>
#include "stdio.h"
#include "stdlib.h"
#include <iomanip>
// istringstream::str
#include <string>       // std::string
#include <iostream>     // std::cout
#include <sstream>      // std::istringstream
using namespace std;


class Tree
{
public:
	Tree();
	~Tree();
	//void insert(string s){}
	string data;
	Tree *right;
	Tree *left;
private:
	string head;
};


vector<string> tokenize(string com)
{
	istringstream is(com); int i=0;
	vector<string> vs;
	string s;
	while(com[i] != '*')
	{
		if(vs.size()==0)
		{
			getline(is,s,' ');
		}
		if(com[i] == '(' || com[i]==')')
		{
			//cout << com[i-1]+" AND "+com[i+1] << endl;
			//intersection(com[i-1],com[i-2]);
		}
		if(com[i] == '&')
		{
			//s+= com[i-1]+" AND "+com[i+1] +' \n';
			s+= "&";
			getline(is,s,')');
			//intersection(com[i-1],com[i-2]);
		}
		else if (com[i] == '|')
		{
			//s+= com[i-1]+" OR "+com[i+1] +' \n';
			s+= "|";
			getline(is,s,')');
			//union(com[i-1],com[i-2]);
		}
		else
		{
			while (com[i] != ')' && com[i] != '&' && com[i] != '|')
			{
				s+=com[i];
			}
		}
		vs.push_back(s);
	}
	return vs;
}

int main () 
{
	std::istringstream iss;
	//std::string strvalues = "32 240 2 1450";
	string ss1 = "((((north)&(pole))|((santa)&(clause)))&(christmas))";
	iss.str (ss1); int paren=0; int expr=0; int aln=0; int words=0; string abc=""; string temp="";
	char val=' ';
	

	for (int n=0; n<ss1.size(); n++)
	{
	char val=' ';
	iss >> val;
	if ((val=='(')){paren++;}
	else if ((val==')')){paren--;}
	else if (val=='|'){expr++; temp +=" | "; words++;}
	else if (val=='&'){expr++; temp +=" & "; words++;}
	else if (isalnum(val)) 
	{
		abc+=val; aln++; temp += abc;
		abc = "";
	}
	else if(paren==0){cout<<"return \n";}
	else { cout << "error in expression \n";}
	}
	//cout << "temp: \n" << temp << "\n :temp \n";
	//cout <<"abc: \t" << abc << "\t :abc\n";
	//cout << "Finished writing the numbers in: ";
	//cout << iss.str() << '\n' << paren << " parentheses \n" << expr << " expression operators \n" << words << " words \n";

	string str(temp);
	string buf; // Have a buffer string
	stringstream ss(str); // Insert the string into a stream
	vector<string> tokens; // Create vector to hold our words
	while (ss >> buf)
	tokens.push_back(buf);
	cout << "tokens: \n";
	for (std::vector<string>::iterator it = tokens.begin() ; it != tokens.end(); ++it)
	{cout << *it << "||token||";}
	cout <<"\n"<< tokens.size() << " tokens.size() \n\n\n";  unsigned int n=0;
	
	string temp1 =temp+'*';
	vector<string> tokenf = tokenize(ss1);
	for (std::vector<string>::iterator it = tokenf.begin() ; it != tokenf.end(); ++it)
	{cout << *it << "||tokenf||";}
	cout <<"\n"<< tokenf.size() << " tokenf.size() \n";  //unsigned int n=0;

	int j; cin>>j;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.