Sign in to follow this  

need help in parsing

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hello everone ,

iam working in c++ ..

iam stuck at a point ..

i want to make a expression tree out of a expression ..
the expression may be like

(A && B || C ) || D

so we also need to take note of operators priority ..

some nodes will of course contain the operators and their child can be either operator/value ..

i dont know how to start ??

please help

regards
y2jsave

Share this post


Link to post
Share on other sites
I believe this is called a "recursive-descent parser":
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <boost/shared_ptr.hpp>

/*
Operator precedence:
0: ()
1: &&
2: ||
*/


struct TreeNode {
virtual void print(std::ostream &os, int context_precedence) const = 0;
virtual ~TreeNode() {
}
};

typedef boost::shared_ptr<TreeNode> STreeNode;

std::ostream &operator<<(std::ostream &os, TreeNode const &tree) {
tree.print(os, 20);
return os;
}

class Letter : public TreeNode {
std::string name;
public:
Letter(std::string const &name)
: name(name) {
}

void print(std::ostream &os,int) const {
std::cout << name;
}
};

class BinaryOperation : public TreeNode {
std::string operation;
int precedence;

public:
BinaryOperation(std::string operation, int precedence,
STreeNode lhs,
STreeNode rhs)
: operation(operation),
precedence(precedence),
lhs(lhs),
rhs(rhs) {
}

STreeNode lhs, rhs;
void print(std::ostream &os, int context_precedence) const {
if (context_precedence < precedence)
os << '(';
lhs->print(os, precedence);
os << ' ' << operation << ' ';
rhs->print(os, precedence);
if (context_precedence < precedence)
os << ')';
}
};

STreeNode parse_expression(const char *&s);

STreeNode parse_leaf(const char *&s) {
if (std::strncmp(s,"(",1)) {
std::string letter(s,1);
++s;
return STreeNode(new Letter(letter));
}
++s;
STreeNode expression = parse_expression(s);
if (std::strncmp(s,")",1)) {
std::cerr << "expected `)'\n";
std::exit(1);
}
++s;
return expression;
}

STreeNode parse_term(const char *&s) {
STreeNode lhs = parse_leaf(s);
if (std::strncmp(s,"&&",2))
return lhs;
s += 2;
STreeNode rhs = parse_term(s);
return STreeNode(new BinaryOperation("&&", 1, lhs, rhs));
}

STreeNode parse_expression(const char *&s) {
STreeNode lhs = parse_term(s);
if (std::strncmp(s,"||",2))
return lhs;
s += 2;
STreeNode rhs = parse_expression(s);
return STreeNode(new BinaryOperation("||", 2, lhs, rhs));
}

int main() {
const char *s = "((A&&B||C)||D)&&E";
boost::shared_ptr<TreeNode> tree = parse_expression(s);
std::cout << *tree << '\n';
}


Share this post


Link to post
Share on other sites

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this