Jump to content
  • Advertisement
Sign in to follow this  
y2jsave

need help in parsing

This topic is 2760 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
Advertisement
The most famous way would probably be to use the Shunting Yard algorithm (look e.g. at wikipedia) to build an AST with the possibility to evaluate.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!