build node tree in c++

Can anyone please help. I have to create a program that will read in an expression from a text file eg if(x>0 && x<9), and correctly build a node tree.
So far I am able to read the line in, and read through it to create tokens.
From here I am totally lost and no amount of reading can help. I think my nest stage (from research) must be to create an Abstract Syntax Tree. Is anyone able to help ...here is what I have to date:


// CompilerTest1.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <string>

#include <iostream>

#include <fstream>

#include <string> // needed for strcasecmp

 

 

using namespace std;

//typedef enum {

// IDENTIFIER,

// KEYWORD,

// NUMBER,

// LIMIT,

// REL_OP, // such as == < > =!= => =< OP, // such as = : + - * / % DELIM, // such as . ( ) , { } ; [ ]

// UNDEF, // undefined EOT // end of token

//} TokenType;

//typedef struct {

// TokenType tokenType;

// char *instance;

// int lineNum;

//} Token;

typedef struct Node {

string data;

int precedence;

struct Node* parent;

struct Node* left;

struct Node* right;

} CNode, *PNode;

//PNode CreateNode(const string& x)

PNode CreateNode(const string x)

{

PNode p = new CNode;

p->parent = p->left = p->right = NULL;

p->data = x;

cout << "I have just added this to the tree: " << x << endl;

return p;

}

string keywords[8] = { "if", "const", "while", "for", "unsigned", "#", "define", "int" };

string ROperator[8] = { "=", "==", "<", ">", "||", "=>", "=<", "&&" };

string OOperands[7] = { "++", ":", "+'" "-", "*", "/", "%" };

string Brackets[6] = {"(", ")", "{", "}", "[", "]" };

string otherChars[1] = { ";"};

string ANumber[10] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

int isKeyword(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of 8 keywords we have set up

for (i = 0; i < 8; i++) {

if ((keywords[i]) == word1)

{

// cout << "it matches keyword: " << (keywords[i]) << endl;

result = 1;

}

}

return result;

}

int isROperator(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of relational operators we have set up

for (i = 0; i < 8; i++) {

if ((ROperator[i]) == word1)

{

// cout << "it matches ROperator: " << (ROperator[i]) << endl;

result = 1;

}

}

return result;

}

int isOOperands(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of relational operators we have set up

for (i = 0; i < 7; i++) {

if ((OOperands[i]) == word1)

{

// cout << "it matches OOperands: " << (OOperands[i]) << endl;

result = 1;

}

}

return result;

}

int isBrackets(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of relational operators we have set up

for (i = 0; i < 6; i++) {

if ((Brackets[i]) == word1)

{

//cout << "it matches Brackets: " << (Brackets[i]) << endl;

result = 1;

}

}

return result;

}

int isotherChars(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of relational operators we have set up

for (i = 0; i < 1; i++) {

if ((otherChars[i]) == word1)

{

//cout << "it matches otherChars: " << (otherChars[i]) << endl;

result = 1;

}

}

return result;

}

int isANumber(string word1)

{

int i;

int result = 0; //set up as false

// we are checking thrugh the array of relational operators we have set up

for (i = 0; i < 10; i++) {

if ((ANumber[i]) == word1)

{

//cout << "it matches ANumber: " << (ANumber[i]) << endl;

result = 1;

}

}

return result;

}

void PostOrderPrintTree(PNode node)

{

if (node)

{

cout << "next line is from the tree fucker: " << endl;

PostOrderPrintTree(node->left);

PostOrderPrintTree(node->right);

cout << node->data;

}

}

int main()

{

ifstream pfile;

pfile.open("dataset1.txt", ios::in);

string delimiter = " ";

size_t pos = 0;

string token;

string line;

while (getline(pfile, line))

{

cout << "this is the line in the text file:" << line << endl;

while ((pos = line.find(delimiter)) != string::npos) {

token = line.substr(0, pos);

cout << token << endl;

if (isKeyword(token))

{

PNode p = CreateNode(token);

} // end of if for iskeyword

if (isROperator(token))

{

PNode p = CreateNode(token);

} // end of if for iskeyword

if (isOOperands(token))

{

PNode p = CreateNode(token);

}

if (isBrackets(token))

{

PNode p = CreateNode(token);

}

if (isotherChars(token))

{

PNode p = CreateNode(token);

}

if (isANumber(token))

{

PNode p = CreateNode(token);

}

line.erase(0, pos + delimiter.length());

} // end of while pos = line

} // end of while getline

pfile.close();

// PostOrderPrintTree(p);

return 0;

}
Topic archived. No new replies allowed.