I can't use boost::spirit in my environment. But I would like to use STL and boost as much as possible to build my own expression evaluator. Is there such an alternative to boost::spirit?
The following code includes unit tests and a complete parser I wrote in an about 90 minute session at ACCU 200x (8 or 9). If you need more, it might be easy enough to extend. You can make it do doubles by defining Parse::value_type, or extracting it into a separate header file and make it a template class.
Or you can take the test cases and try yourself. (it uses CUTE from http://cute-test.com)
#include "cute.h"
#include "ide_listener.h"
#include "cute_runner.h"
#include <cctype>
#include <map>
namespace {
class Parser {
typedef int value_type;
typedef std::vector<value_type> valuestack;
typedef std::vector<char> opstack;
typedef std::map<std::string,value_type> memory;
public:
memory variables;
private:
void evaluateSingleOperator(char op,value_type &result,value_type operand) {
switch(op) {
case '+': result += operand; break;
case '-': result -= operand; break;
case '*': result *= operand; break;
case '/': result /= operand; break;
default: throw("invalid operand");
}
}
void evaluateStacks(valuestack &values, opstack &ops) {
while(ops.size() && values.size()>1) {
char op = ops.back(); ops.pop_back();
value_type operand = values.back(); values.pop_back();
evaluateSingleOperator(op,values.back(),operand);
}
}
bool higherPrecedenceOrLeftAssociative(char last, char current) {
return (last == current)||(last == '*' || last == '/') ;
}
bool shouldEvaluate(char op,opstack const &ops) {
return ops.size() > 0 && higherPrecedenceOrLeftAssociative(ops.back(),op);
}
std::string parseVariableName(std::istream &is) {
std::string variable;
char nextchar=0;
while ((is >> nextchar) && isalpha(nextchar)) {
variable += nextchar;
}
if (variable.size() == 0) throw std::string("internal parse error");
is.unget();
return variable;
}
int peekWithSkipWhiteSpace(std::istream &is) {
int nextchar = EOF;
while(isspace(nextchar = is.peek())) is.get();
return nextchar;
}
value_type getOperand(std::istream &is) {
int nextchar = peekWithSkipWhiteSpace(is);
if (nextchar == EOF) throw std::string("syntax error operand expected");
if (isdigit(nextchar)){
value_type operand=0;
if (!(is >> operand)) throw std::string("syntax error getting number") ;
return operand;
} else if ('(' == nextchar) {
is.get();
return parse(is);
} else if (isalpha(nextchar)) {
std::string variable= parseVariableName(is);
if( parseAssignmentOperator(is)) {
variables[variable] = parse(is);
} else {
if (!variables.count(variable)) throw std::string("undefined variable: ")+variable;
}
return variables[variable];
}
throw std::string("syntax error");
}
bool parseAssignmentOperator(std::istream &is) {
int nextchar = peekWithSkipWhiteSpace(is);
if ('=' != nextchar) {
return false;
}
is.get();
return true;
}
public:
value_type parse(std::istream &is) {
is >> std::skipws;
valuestack values;
opstack ops;
values.push_back(getOperand(is));
char op=')';
while((is >>op) && op != ')') {
if (shouldEvaluate(op, ops)) {
evaluateStacks(values, ops);
}
values.push_back(getOperand(is));
ops.push_back(op);
}
evaluateStacks(values,ops);
return values.back();
}
value_type eval(std::string s) {
std::istringstream is(s);
return parse(is);
}
};
int eval(std::string s) {
return Parser().eval(s);
}
void shouldThrowEmptyExpression() {
eval("");
}
void shouldThrowSyntaxError() {
eval("()");
}
void testSimpleNumber() {
ASSERT_EQUAL(5,eval("5"));
}
void testSimpleAdd() {
ASSERT_EQUAL(10,eval("5 +5"));
}
void testMultiAdd() {
ASSERT_EQUAL(10,eval("1 + 2 + 3+4"));
}
void testSimpleSubtract() {
ASSERT_EQUAL(5,eval("6-1"));
}
void testTenPlus12Minus100() {
ASSERT_EQUAL(-78,eval("10+12-100"));
}
void testMultiply() {
ASSERT_EQUAL(50,eval("10*5"));
}
void testDivision() {
ASSERT_EQUAL(7,eval("21/3"));
}
void testAddThenMultiply() {
ASSERT_EQUAL(21,eval("1+4 *5"));
}
void testAddThenMultiplyAdd() {
ASSERT_EQUAL(16,eval("1+4*5 -5"));
}
void testAddSubSub() {
ASSERT_EQUAL(-4,eval("1+2-3-4"));
}
void testSimpleParenthesis() {
ASSERT_EQUAL(1,eval("(1)"));
}
void testSimpleOperandParenthesis() {
ASSERT_EQUAL(2,eval("1+(1)"));
}
void testParenthesis() {
ASSERT_EQUAL(5,eval("2*(1+4)-5"));
}
void testNestedParenthesis() {
ASSERT_EQUAL(16,eval("2*(1+(4*3)-5)"));
}
void testDeeplyNestedParenthesis() {
ASSERT_EQUAL(8,eval("((2*((1+(4*3)-5)))/2)"));
}
void testSimpleAssignment() {
Parser p;
ASSERT_EQUAL(1, p.eval("a=1*(2-1)"));
ASSERT_EQUAL(8, p.eval("a+7"));
ASSERT_EQUAL(1, p.eval("2-a"));
}
void testLongerVariables() {
Parser p;
ASSERT_EQUAL(1, p.eval("aLongVariableName=1*(2-1)"));
ASSERT_EQUAL(42, p.eval("AnotherVariable=7*(4+2)"));
ASSERT_EQUAL(1, p.eval("2-(aLongVariableName*AnotherVariable)/42"));
}
void shouldThrowUndefined() {
eval("2 * undefinedVariable");
}
void runSuite(){
cute::suite s;
//TODO add your test here
s.push_back(CUTE_EXPECT(CUTE(shouldThrowEmptyExpression),std::string));
s.push_back(CUTE_EXPECT(CUTE(shouldThrowSyntaxError),std::string));
s.push_back(CUTE(testSimpleNumber));
s.push_back(CUTE(testSimpleAdd));
s.push_back(CUTE(testMultiAdd));
s.push_back(CUTE(testSimpleSubtract));
s.push_back(CUTE(testTenPlus12Minus100));
s.push_back(CUTE(testMultiply));
s.push_back(CUTE(testDivision));
s.push_back(CUTE(testAddThenMultiply));
s.push_back(CUTE(testAddSubSub));
s.push_back(CUTE(testAddThenMultiplyAdd));
s.push_back(CUTE(testSimpleParenthesis));
s.push_back(CUTE(testSimpleOperandParenthesis));
s.push_back(CUTE(testParenthesis));
s.push_back(CUTE(testNestedParenthesis));
s.push_back(CUTE(testDeeplyNestedParenthesis));
s.push_back(CUTE(testSimpleAssignment));
s.push_back(CUTE(testLongerVariables));
s.push_back(CUTE_EXPECT(CUTE(shouldThrowUndefined),std::string));
cute::ide_listener lis;
cute::makeRunner(lis)(s, "The Suite");
}
}
int main(){
runSuite();
}
YACC++ is a very good tool for parser generator for c++ applications. ANTLR is also agood option howver that does not have good documentation for its usage in C/C++.
Related
So I have been solving problems from this book called
Programming: Principles and Practice Using C++ by Bjarne Stroustrup to learn C++ and have been enjoying it a lot. I recently got stuck on this question in Chapter 10 (Input and Output
Streams).
The question is given below:
Add a command from x to the calculator from Chapter 7 that makes it
take input from a file x. Add a command to y to the calculator that
makes it write its output (both standard output and error output) to
file y.
The source code for the above mentioned calculator is given below:
/*
Simple Calculator
This program implements a basic expression calculator.
Input from cin; output to cout.
The grammar for input is:
Calculation:
Statement
Print
Quit
Statement:
Declaration
Expression
Declaration:
"let" Name "=" Expression
Print:
;
Quit:
q
Expression:
Term
Term '+' Expression //addition
Term '-' Expression //subtraction
Term:
Primary
Term '*' Primary //multiplication
Term '/' Primary //division
Term '%' Primary //mod division
Primary:
Number
'(' Expression ')'
'-' Primary
'+' Primary
Number:
floating-point-literal
Input comes from cin through the Token_stream called ts.
*/
#include "../../std_lib_facilities.h"
class Token //a very simple user-defined type
{
public:
char kind = ' '; //what kind of token
double value = 0; //for numbers: a value
string name = " "; //for strings: a name
Token(char ch) :kind{ ch } { } //initialize kind with ch
Token(char ch, double val) :kind{ ch }, value{ val } {} //initializes kind and value
Token(char ch, string n) :kind{ ch }, name{ n } { } //initializes kind and name
};
class Token_stream
{
public:
//user interface
Token get(); //get a Token
void putback(Token t); //put a Token back
void ignore(char c); //discard characters up to and including a c
private:
//implementation details
//(not directly accessible to users of Token_stream)
bool full{ false }; //is there a Token in the buffer?
Token buffer = 0; //here is where we keep a Token put back with putback()
};
class Variable
{
public:
string name = " ";
string const_name = " ";
double value = 0;
};
vector<Variable>var_table;
double get_value(string s)
//return the value of the Variable named s
{
for (const Variable& v : var_table) {
if (v.name == s) return v.value;
if (v.const_name == s) return v.value;
}
error("get: undefined variable ", s);
return 1;
}
void set_value(string s, double d)
//set the Variable named s to d
{
for (Variable& v : var_table)
{
if (v.name == s)
{
v.value = d;
return;
}
if (v.const_name == s)
{
error("set: cannot assign a new value to an existing constant");
return;
}
}
error("set: undefined variable", s);
}
const char number = '8'; //t.kind == number means that t is a number Token
const char quit = 'q'; //t.kind == quit means that t is a quit Token
const char print = ';'; //t.kind == print means that t is a print token
const char name = 'a'; //name token
const char constant = 'c'; //constant token
const string constdeclkey = "const"; //constant keyword
const char let = 'L'; //declaration token
const string declkey = "let"; //declaration keyword
const string prompt = "> ";
const string result = "= "; //used to indicate that what follows is a result
void Token_stream::putback(Token t)
{
if (full) error("putback() into a full buffer");
buffer = t; //copy t to buffer
full = true; //buffer is now full
}
void Token_stream::ignore(char c)
//c represents the kind of Token
{
//first look in buffer:
if (full && c == buffer.kind)
{
full = false;
return;
}
full = false;
//now search input:
char ch = 0;
while (cin >> ch)
if (ch == c) return;
}
Token Token_stream::get()
{
if (full)
{
full = false; //do we already have a Token ready?
return buffer; //remove Token from buffer
}
char ch;
cin >> ch; //note that >> skips whitespace(space, newline, tab, etc)
switch (ch)
{
case ';': //for "print"
case 'q': //for "quit"
case '(':
case ')':
case '+':
case '-':
case '*':
case '/':
case '%':
case '=':
return Token{ ch }; //let each character represent itself
case '.':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
cin.putback(ch); //put digit back into the input stream
double val;
cin >> val; //read a floating-point number
return Token{ number, val }; //let '8' represent a "a number"
}
default:
if (isalpha(ch))
{
string s;
s += ch;
while (cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) s += ch;
cin.putback(ch);
if (s == declkey) return Token(let); //declaration keyword
else if (s == constdeclkey) return Token(constant);
return Token{ name, s };
}
error("Bad Token");
return 1;
}
}
bool is_declared(string var)
//is var already in var_table?
{
for (const Variable& v : var_table)
{
if (v.name == var) return true;
}
for (const Variable& v : var_table)
{
if (v.const_name == var) return true;
}
return false;
}
double define_name(string var, double val)
//add (var, val) to var_table
{
if (is_declared(var)) set_value(var, val);
Variable v;
v.name = var;
v.value = val;
var_table.push_back(v);
return val;
}
double define_const_name(const string constant, double val)
//add (constant, val) to var_table
{
if (is_declared(constant)) error(constant, " constant declared twice");
Variable v;
v.const_name = constant;
v.value = val;
var_table.push_back(v);
return val;
}
Token_stream ts; //makes a Token_stream called ts that reads from cin
double expression(); //declaration so that primary() can call it
double declaration()
//assume we have seen "let"
//handle: name = expression
//declare a variable called "name" with the initial value "expression"
{
Token t = ts.get();
if (t.kind != name) error("name expected in declaration");
string var_name = t.name;
Token t2 = ts.get();
if (t2.kind != '=') error("= missing in declaration of ", var_name);
double d = expression();
define_name(var_name, d);
return d;
}
double const_declaration()
//assume we have seen "const"
//handle: const_name = expression
//declare a constant called "const" with the initial value "expression"
{
Token t = ts.get();
if (t.kind != name) error("constant_name expected in declaration");
const string const_name = t.name;
Token t2 = ts.get();
if (t2.kind != '=') error("= missing in declaration of ", const_name);
double d = expression();
define_const_name(const_name, d);
return d;
}
double primary()
//deals with numbers and parantheses
//calls expression() and get_token()
{
Token t = ts.get();
switch (t.kind)
{
case '(': //handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')') error("')' expected");
return d;
}
case number: //we use '8' to represent a number
return t.value; //return the number's value
case '-':
return -primary();
case '+':
return primary();
case name:
return get_value(t.name);
default:
error("primary expected");
return 1;
}
}
double term()
//deals with * and -
//calls primary and get_token()
{
double left = primary(); //read and evaluate a Primary
Token t = ts.get(); //get the next token
while (true)
{
switch (t.kind) //see which kind of token it is
{
case '*':
left *= primary(); //evaluate Primary and multiply
t = ts.get();
break;
case '/':
{
double d = primary(); //evaluate Primary and divide
if (d == 0) error("divide by zero"); //check if primary isnt zero
left /= d;
t = ts.get();
break;
}
case '%':
{
double d = primary();
if (d == 0) error("divide by zero");
left = fmod(left, d);
t = ts.get();
break;
}
default:
ts.putback(t); //put t back into Token stream
return left; //return result to Expression
}
}
}
double expression()
//deals with + and -
//calls term() and get_token()
{
double left = term(); //read and evaluate an Term
Token t = ts.get(); //get the next token from the Token stream
while (true)
{
switch (t.kind) //see which kind of token it is
{
case '+':
left += term(); //evaluate Term and add
t = ts.get();
break;
case '-':
left -= term(); //evaluate Term and subtract
t = ts.get();
break;
default:
ts.putback(t); //put t back into the token stream
return left; //return the answer
}
}
}
void clean_up_mess() //B)
{
//skip until we find a print
ts.ignore(print);
}
double statement()
{
Token t = ts.get();
switch (t.kind)
{
case let:
return declaration();
case constant:
return const_declaration();
default:
ts.putback(t);
return expression();
}
}
void calculate() //expression evaluation loop
{
double val = 0;
while (cin)
try
{
cout << prompt; //print prompt
Token t = ts.get();
while (t.kind == print) //';' for "print now"
t = ts.get(); //eat ';'
if (t.kind == quit) //'q' for "quit"
{
return;
}
ts.putback(t);
cout << result << statement() << '\n';
}
catch (exception& e)
{
cerr << e.what() << '\n'; //write error message
clean_up_mess();
}
}
int main() //main loop and deals with errors
{
try
{
//predefined names:
define_const_name("pi", 3.1415926535);
define_const_name("e", 2.7182818284);
calculate();
keep_window_open();
return 0;
}
catch (exception& e)
{
cerr << e.what() << '\n';
keep_window_open("~~");
return 1;
}
catch (...)
{
cerr << "exception \n";
keep_window_open();
return 2;
}
}
The calculator kind of works like this:
> (2+3)-4*(5/6); //Input
= 1.66667 //Output
> //Next Input
It can also declare variables and constants and then use them back into for calculations:
> let x = 2; //Variable 1
= 2 //assigned value for Var 1
> const y = 3; //Constant 1
= 3 //assigned value Const 1
> x + y; //added var 1 to const 1
= 5 //result
> //next input
Also ; acts like a print character. The expression gets evaluated and displayed as soon as it reads ; and then it clears the token stream and gets ready to read the next input.
The custom header file std.lib.facilities.h is a collection of most standard C++ libraries and functions some of which are written by Bjarne Stroustrup and might have been used in the code given above.
I have also left the code for that header file just in case if someone is curious:
/*
std_lib_facilities.h
*/
/*
simple "Programming: Principles and Practice using C++ (second edition)" course header to
be used for the first few weeks.
It provides the most common standard headers (in the global namespace)
and minimal exception/error support.
Students: please don't try to understand the details of headers just yet.
All will be explained. This header is primarily used so that you don't have
to understand every concept all at once.
By Chapter 10, you don't need this file and after Chapter 21, you'll understand it
Revised April 25, 2010: simple_error() added
Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
Revised November 28 2013: add a few container algorithms
Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
Revised Febrary 2 2015: randint() can now be seeded (see exercise 5.13).
Revised August 3, 2020: a cleanup removing support for ancient compilers
*/
#ifndef H112
#define H112 080315L
#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>
//------------------------------------------------------------------------------
typedef long Unicode;
//------------------------------------------------------------------------------
using namespace std;
template<class T> string to_string(const T& t)
{
ostringstream os;
os << t;
return os.str();
}
struct Range_error : out_of_range { // enhanced vector range error reporting
int index;
Range_error(int i) :out_of_range("Range error: " + to_string(i)), index(i) { }
};
// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
using size_type = typename std::vector<T>::size_type;
/* #ifdef _MSC_VER
// microsoft doesn't yet support C++11 inheriting constructors
Vector() { }
explicit Vector(size_type n) :std::vector<T>(n) {}
Vector(size_type n, const T& v) :std::vector<T>(n, v) {}
template <class I>
Vector(I first, I last) : std::vector<T>(first, last) {}
Vector(initializer_list<T> list) : std::vector<T>(list) {}
*/
using std::vector<T>::vector; // inheriting constructor
T& operator[](unsigned int i) // rather than return at(i);
{
if (i<0 || this->size() <= i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
const T& operator[](unsigned int i) const
{
if (i<0 || this->size() <= i) throw Range_error(i);
return std::vector<T>::operator[](i);
}
};
// disgusting macro hack to get a range checked vector:
#define vector Vector
// trivially range-checked string (no iterator checking):
struct String : std::string {
using size_type = std::string::size_type;
// using string::string;
char& operator[](unsigned int i) // rather than return at(i);
{
if (i<0 || size() <= i) throw Range_error(i);
return std::string::operator[](i);
}
const char& operator[](unsigned int i) const
{
if (i<0 || size() <= i) throw Range_error(i);
return std::string::operator[](i);
}
};
namespace std {
template<> struct hash<String>
{
size_t operator()(const String& s) const
{
return hash<std::string>()(s);
}
};
} // of namespace std
struct Exit : runtime_error {
Exit() : runtime_error("Exit") {}
};
// error() simply disguises throws:
inline void error(const string& s)
{
throw runtime_error(s);
}
inline void error(const string& s, const string& s2)
{
error(s + s2);
}
inline void error(const string& s, int i)
{
ostringstream os;
os << s << ": " << i;
error(os.str());
}
template<class T> char* as_bytes(T& i) // needed for binary I/O
{
void* addr = &i; // get the address of the first byte
// of memory used to store the object
return static_cast<char*>(addr); // treat that memory as bytes
}
inline void keep_window_open()
{
cin.clear();
cout << "Please enter a character to exit\n";
char ch;
cin >> ch;
return;
}
inline void keep_window_open(string s)
{
if (s == "") return;
cin.clear();
cin.ignore(120, '\n');
for (;;) {
cout << "Please enter " << s << " to exit\n";
string ss;
while (cin >> ss && ss != s)
cout << "Please enter " << s << " to exit\n";
return;
}
}
// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s) // write ``error: s and exit program
{
cerr << "error: " << s << '\n';
keep_window_open(); // for some Windows environments
exit(1);
}
// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max
// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
R r = R(a);
if (A(r) != a) error(string("info loss"));
return r;
}
// random number generators. See 24.7.
inline default_random_engine& get_rand()
{
static default_random_engine ran; // note: not thread_local
return ran;
};
inline void seed_randint(int s) { get_rand().seed(s); }
inline int randint(int min, int max) { return uniform_int_distribution<>{min, max}(get_rand()); }
inline int randint(int max) { return randint(0, max); }
//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x
// container algorithms. See 21.9. // C++ has better versions of this:
template<typename C>
using Value_type = typename C::value_type;
template<typename C>
using Iterator = typename C::iterator;
template<typename C>
// requires Container<C>()
void sort(C& c)
{
std::sort(c.begin(), c.end());
}
template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
std::sort(c.begin(), c.end(), p);
}
template<typename C, typename Val>
// requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
return std::find(c.begin(), c.end(), v);
}
template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
return std::find_if(c.begin(), c.end(), p);
}
#endif //H112
So to start with this problem I added the constant character from and constant string from_key just under all of the other constants in the calculator code.
const char from = 'f'; // file input read token
const string from_key = "from"; // file input read keyword
Then I added a new case for from token in the statement()'s function definiton
double statement()
{
Token t = ts.get();
switch (t.kind)
{
/.../
case from:
return input();
/.../
}
}
Ignore the function input() being returned from this case. That's kind of my main problem with this question. I will come back to it after the next part.
I followed it by adding a Token return for from's case in Token Token_stream::get() function's definition
Token Token_stream::get()
{
if (full)
{
/.../
}
char ch;
cin >> ch; //note that >> skips whitespace(space, newline, tab, etc)
switch (ch)
{
/.../
default:
if (isalpha(ch))
{
string s;
s += ch;
while (cin.get(ch) && (isalpha(ch) || isdigit(ch) || ch == '_')) s += ch;
cin.putback(ch);
if (s == declkey) return Token(let); //declaration keyword
else if (s == constdeclkey) return Token(constant);
else if (s == from_key) return Token(from);
return Token{ name, s };
}
error("Bad Token");
return 1;
}
}
After that I started writing the input() function which I mentioned seconds ago. It looks like this for now:
double input()
{
Token t = ts.get();
if (t.kind != name) error("read-file name expected in declaration");
string read_file = t.name;
ifstream ifs{ read_file + ".txt" };
if (!ifs) error("cannot open file " + read_file + ".txt");
while (ifs)
{
try
{
//idk what to do here
}
catch (exception& e)
{
cerr << e.what() << '\n';
clean_up_mess();
}
return 0;
}
}
So what I want to do is pretty simple.
I just want Token Token_stream::get() function to read input from ifs rather than cin, kind of like redirect the istream to ifs while its reading data from the file then switch it back to cin when its done. I'm thinking about calling the statement() function back here to print the evaluation on screen for now and then I might redirect it again to write the output to a file y.txt as it's mentioned in the question.
My issue is that I really don't know how to do this. I tried doing an if-else statement to switch istream from cin to ifs but getting ifs out from input()'s scope to Token Token_stream::get()'s scope is a big hurdle for me.
I'm trying to write a simple program to convert infix notation to prefix and postfix. So far the postfix one works perfectly. However, I can't seem to get the prefix conversion right. I used the shunting yard algorithm for both. Apologies beforehand if my code is a bit unusual (i.e. writing my own stack class instead of using #include, unnecessarily using other things), I have to meet assignment requirements (this is a college assignment). Here's what I've tried so far:
#include<iostream>
#include<string.h>
using namespace std;
const int Max=255;
class Stack
{
private:
char element[Max];
int top;
public:
Stack()
{
top=-1;
}
bool isFull()
{
if(top>=(Max-1)) return true;
else return false;
}
bool isEmpty()
{
if(top==-1) return true;
else return false;
}
bool push(char x)
{
if(!isFull())
{
top++;
element[top]=x;
return true;
}
else
{
cout<<"Stack is full"<<endl;
return false;
}
}
bool pop(char &x)
{
if(!isEmpty())
{
x=element[top--];
return true;
}
else
{
//cout<<"Stack is empty"<<endl;
return false;
}
}
char retrieve()
{
if(!isEmpty())
{
return element[top];
}
else
{
//cout<<"Stack is empty"<<endl;
return ' ';
}
}
};
class Converter
{
private:
public:
Converter(){}
bool isOperator(char x)
{
if(x=='+'||x=='-'||x=='*'||x=='/'||x=='^'||x=='('||x==')') return true;
else return false;
}
bool isOperand(char x)
{
if(x>='0'&&x<='9') return true;
else return false;
}
int Hierarchy(char x)
{
if(x=='+'||x=='-') return 1;
else if(x=='*'||x=='/') return 2;
else if(x=='^') return 3;
else return 0;
}
char*ToPostfix(char infix[])
{
Stack stack1;
char res[20];
int resindex=0;
for(int i=0;i<strlen(infix);i++)
{
if(isOperator(infix[i]))
{
if(infix[i]=='(')
{
stack1.push(infix[i]);
}
else if(infix[i]==')')
{
while(stack1.retrieve()!='(')
{
stack1.pop(res[resindex++]);
}
stack1.pop(res[resindex]);
}
else
{
while(Hierarchy(infix[i])<=Hierarchy(stack1.retrieve()))
{
stack1.pop(res[resindex++]);
}
stack1.push(infix[i]);
}
}
else if(isOperand(infix[i]))
{
res[resindex++]=infix[i];
}
}
while(!stack1.isEmpty())
{
stack1.pop(res[resindex++]);
}
res[resindex]='\0';
return res;
}
char*ToPrefix(char infix[])
{
char res[20];
strcpy(res,strrev(infix));
for(int i=0;i<strlen(res);i++)
{
if(res[i]=='(')
{
res[i]=')';
}
else if(res[i]==')')
{
res[i]='(';
}
}
strcpy(res,ToPostfix(res));
strcpy(res,strrev(res));
return res;
}
};
int main()
{
Converter convert;
char infix[20];
cout<<"Enter infix expression: ";
cin>>infix;
cout<<endl<<"Prefix: "<<convert.ToPrefix(infix)<<endl;
cout<<"Postfix: "<<convert.ToPostfix(infix)<<endl;
return 0;
}
when I try to convert a simple infix notation, i.e. 1*(2+3)/4^5-6, the postfix conversion is right (123+*45^/6-) but the prefix conversion returns the wrong answer (-*1/+23^456) instead of -/*1+23^456. can anyone help?
Actually, both answers are correct because you can switch the order of the division and the multiplication if multiplication comes first in infix. So your wrong answer is correct in this case. However, there is a left to right precedence, so your hierarchy handling is not correct: change else if(x=='*'||x=='/') return 2;.
I started writing a lexer, and I made the following file to test that everything so far is working okay:
Main.cpp
#include <iostream>
#include "Lexer.h"
#include "Token.h"
int main(void)
{
std::string str(""); // I'll use this to test expressions
Lexer lexer(str);
std::vector<Token> tokens = lexer.lex();
for(auto it = tokens.begin(); it != tokens.end(); ++it)
{
std::string str;
switch(it->type)
{
case TokenType::_EOF:
str = "EOF";
break;
case TokenType::ERROR:
str = "ERROR";
break;
case TokenType::SEMICOLON:
str = "SEMICOLON";
break;
case TokenType::PLUS:
str = "PLUS";
break;
case TokenType::LESS_THAN:
str = "LESS_THAN";
break;
case TokenType::GREATER_THAN:
str = "GREATER_THAN";
break;
case TokenType::INT:
str = "INT";
break;
case TokenType::ID:
str = "ID";
break;
case TokenType::WHITESPACE:
str = "WHITESPACE";
break;
default:
str = "<Unknown Token>";
}
std::cout << str << ", detail='" << it->detail << "'" << std::endl;
}
return 0;
}
The line lexer.lex() throws an exception. Looking in Lexer.h:
std::vector<Token> Lexer::lex(void)
{
// Reset input pointer
inputPtr = 0;
updateCurrentChar();
// Read tokens until EOF
std::vector<Token> tokens;
Token *token = nullptr;
do
{
token = getNext();
tokens.push_back(*token);
} while(token->type != TokenType::_EOF);
return tokens;
}
The line tokens.push_back(*token) is throwing an exception:
I tried looking at the info on push_back() here, and saw that:
If a reallocation happens, the storage is allocated using the container's allocator, which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).
This seems to be my problem, but I don't understand why the allocation request wouldn't succeed.
For completeness, here are all the files:
Token.h
#pragma once
#include <string>
enum class TokenType
{
_EOF,
ERROR,
EQUALS,
SEMICOLON,
PLUS,
LESS_THAN,
GREATER_THAN,
INT,
ID,
WHITESPACE
};
struct Token
{
TokenType type;
std::string detail;
};
Lexer.h
#pragma once
#include <string>
#include <vector>
#include "Token.h"
class Lexer
{
public:
Lexer(std::string);
~Lexer(void);
std::vector<Token> lex(void);
private:
Token* getNext(void);
void updateCurrentChar(void);
void increment(void);
bool matchCurrent(char);
bool isWhitespace(char) const;
bool isDigit(char) const;
bool isLetter(char) const;
bool isLowercaseLetter(char) const;
bool isUppercaseLetter(char) const;
std::string readWhitespace(void);
std::string readInt(void);
std::string readId(void);
std::string input;
int inputPtr;
char currentChar;
const char EOF_CHAR;
};
Lexer.cpp
#include "Lexer.h"
Lexer::Lexer(std::string _input)
: input(_input), EOF_CHAR(-1)
{
}
Lexer::~Lexer(void)
{
}
std::vector<Token> Lexer::lex(void)
{
// Reset input pointer
inputPtr = 0;
updateCurrentChar();
// Read tokens until EOF
std::vector<Token> tokens;
Token *token = nullptr;
do
{
token = getNext();
tokens.push_back(*token);
} while(token->type != TokenType::_EOF);
return tokens;
}
void Lexer::updateCurrentChar(void)
{
currentChar = inputPtr < input.length()
? input[inputPtr]
: EOF_CHAR;
}
void Lexer::increment(void)
{
inputPtr++;
updateCurrentChar();
}
bool Lexer::matchCurrent(char toMatch)
{
if(toMatch == currentChar)
{
increment();
return true;
}
return false;
}
Token* Lexer::getNext(void)
{
Token token;
if(isWhitespace(currentChar))
{
token.type = TokenType::WHITESPACE;
token.detail = readWhitespace();
return &token;
}
if(isDigit(currentChar))
{
token.type = TokenType::INT;
token.detail = readInt();
return &token;
}
if(isLetter(currentChar))
{
token.type = TokenType::ID;
token.detail = readId();
return &token;
}
if(currentChar == EOF_CHAR)
{
token.type = TokenType::_EOF;
return &token;
}
switch(currentChar)
{
case ';':
token.type = TokenType::SEMICOLON;
case '=':
token.type = TokenType::EQUALS;
case '<':
token.type = TokenType::LESS_THAN;
case '>':
token.type = TokenType::GREATER_THAN;
case '+':
token.type = TokenType::PLUS;
default:
token.type = TokenType::ERROR;
token.detail = currentChar;
}
increment();
return &token;
}
std::string Lexer::readWhitespace(void)
{
std::string ws;
while(isWhitespace(currentChar))
{
ws += currentChar;
increment();
}
return ws;
}
std::string Lexer::readInt(void)
{
std::string ws;
while(isDigit(currentChar))
{
ws += currentChar;
increment();
}
return ws;
}
std::string Lexer::readId(void)
{
std::string ws;
while(isWhitespace(currentChar))
{
ws += currentChar;
increment();
}
return ws;
}
bool Lexer::isDigit(char c) const
{
return c >= '0' && c <= '9';
}
bool Lexer::isLetter(char c) const
{
return isLowercaseLetter(c)
|| isUppercaseLetter(c);
}
bool Lexer::isLowercaseLetter(char c) const
{
return c >= 'a' && c <= 'z';
}
bool Lexer::isUppercaseLetter(char c) const
{
return c >= 'A' && c <= 'Z';
}
bool Lexer::isWhitespace(char c) const
{
switch(c)
{
case ' ':
case '\n':
case '\t':
case '\r':
return true;
default:
return false;
}
}
The problem is that you in getNext you return a pointer to a local variable. Remember that when the function returns all local variables are destructed and so the pointer now points to a destructed object. Or, since local variables are on the stack and the stack is reused between function calls, it can now point to something completely different. The result of you then dereferencing this now invalid pointer leads to undefined behavior, and it's common for undefined behavior to cause crashes, but it may also seem to work but the data is completely screwed up.
The obvious solution is of course to return a copy of the object, i.e. don't use pointers (which is a good tip in general).
Here i have a code for converting Infix To Postfix using Stack
the code compiled without errors but my problem is When I enter any infix e.g A+B i got a postfix of AB without the operators or parenthesis i couldn't solve this problem and i have an exam tomorrow please save me and tell me What i am missing here and thanks a lot.....
#include <iostream>
#include <string.h>
using namespace std;
struct stack
{
int ptr;
char arr[50];
stack()
{
ptr=0;
}
char top()
{
return arr[ptr];
}
void push(char ch)
{
ptr++;
arr[ptr]=ch;
}
void pop()
{
ptr-- ;
}
};
void Convert(char[50],char[50]);
bool IsOperand(char);
bool TakesPrecedence(char,char);
void main()
{
char Reply;
do
{
char Infix[50],Postfix[50]="";
cout<<"Enter an Infix expression: "<<endl;
cin>>Infix;
Convert(Infix,Postfix);
cout<<"The equivalent postfix expression is: "<<endl<<Postfix<<endl;
cout<<endl<<"Do another (y/n)? ";
cin>>Reply;
}
while (Reply =='y');
}
void Convert(char Infix[50],char Postfix[50])
{
stack OperatorStack;
char TopSymbol,Symbol;
int L;
for(unsigned k=0;k<strlen(Infix);k++)
{
Symbol=Infix[k];
if (IsOperand(Symbol))
{
L=strlen(Postfix);
Postfix[L]=Symbol;
Postfix[L+1]='\0';
}
else
{
while ( OperatorStack.ptr && TakesPrecedence(OperatorStack.top(),Symbol))
{
TopSymbol= OperatorStack.top();
OperatorStack.pop();
L=strlen(Postfix);
Postfix[L]=TopSymbol;
Postfix[L+1]='\0';
}
if( OperatorStack.ptr && Symbol==')')
OperatorStack.pop();
else
OperatorStack.push(Symbol);
}
}
while(OperatorStack.ptr)
{
TopSymbol=OperatorStack.top();
OperatorStack.pop();
L=strlen(Postfix);
Postfix[L+1]='\0';
}
}
bool IsOperand(char ch)
{
if((ch >='a' &&ch <= 'z') ||(ch >='A' &&ch <= 'Z')||(ch >='0' &&ch <= '9'))
return true;
else
return false;
}
bool TakesPrecedence(char OperatorA,char OperatorB)
{
if(OperatorA='(')
return false;
else if(OperatorB='(')
return false;
else if(OperatorB=')')
return true;
else if(OperatorA='^' && (OperatorB='^'))
return false;
else if(OperatorA='^')
return true;
else if(OperatorB='^')
return false;
else if(OperatorA='*' || (OperatorA='/'))
return true;
else if(OperatorB='*' || (OperatorB='/'))
return false;
else
return true;
}
Pretty sure
if(OperatorA='(')
and all other should be
if(OperatorA=='(')
But my main advice is for you to start using a debugger. This could have easily been spotted during debugging. I can't stress enough how important knowing how to debug is.
I wrote this program and it supposed to test for the correct use of the three grouping symbols "(",")";"[","]"; and "{","}". It is using the array implementation of the stacks and supposed to evaluate if it is good string or a bad string. For example: (a+b), [(a-b)+c] would be good and )a+b( etc. would be bad string. When i run the program i get only one error. I thought i am missing a semi-colon or something, but after looking through the code several time,i can't find it. Maybe i got tunnel vision. Can you please see what the problem here is? This is the error: project1.cpp:41: error: expected initializer before 'while'.
#include <string>
#include <iostream>
#include <stdio.h>
using namespace std;
const int DefaultListSize = 100;
typedef char Elem;
class Astack {
private:
int size;
int top;
Elem *listArray;
public:
Astack (int sz = DefaultListSize)
{size = sz; top= 0; listArray = new Elem[sz];}
~Astack() {delete [] listArray;}
void clear() {top=0;}
bool push(const Elem& item) {
if (top == size) return false;
else {listArray[top++] = item; return true;}}
bool pop(Elem& it) {
if (top==0) return false;
else {it = listArray[--top]; return true;}}
bool topValue(Elem& it) const {
if (top==0) return false;
else {it = listArray[top-1]; return true;}}
bool isEmpty() const {if (top==0) return true;
else return false;}
int length() const{return top;}
}; //end of class Astack
Astack s;
const string LEFTGROUP="([{";
const string RIGHTGROUP=")]}";
int main()
while (!EOF) {
while (!EOL) {
ch = getc();
if (ch == LEFTGROUP[0]) {
s.push(ch);
}
if (ch == LEFTGROUP[1] {
s.push(ch);
}
if (ch == LEFTGROUP[2] {
s.push(ch);
}
} //checking for openers
while (!EOL) {
ch = getc();
if (s.top() == LEFTGROUP[0]) {
if (ch == RIGHTGROUP[0]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[1]) {
if (ch == RIGHTGROUP[1]) {
s.pop();
}
}
if (s.top() == LEFTGROUP[2]) {
if (ch == RIGHTGROUP[2]) {
s.pop();
}
}
if (!s.empty()) {
cout<<"Bad String."<<endl;
else {
cout<<"Good String."endl;
}
}
}
return 0;
You forgot a { at the beginning of int main(). You should also end with }
int main(){
//your while code
return 0;
}