Parsing/RPN to infix conversion: Difference between revisions

Corrected compilation errors of C++ implementation, fixed tags and pasted output.
(Modified to work with the two examples and fixed bugs)
(Corrected compilation errors of C++ implementation, fixed tags and pasted output.)
Line 660:
=={{header|C++}}==
Very primitive implementation, doesn't use any parsing libraries which would shorten this greatly.
<lang C++Cpp>
/*Corrected by Abhishek Ghosh, 6th November 2017*/
#include <iostream>
#include <stack>
Line 666 ⟶ 667:
#include <map>
#include <set>
 
using namespace std;
 
struct Entry_
{
Line 674 ⟶ 675:
string op_;
};
 
bool PrecedenceLess(const string& lhs, const string& rhs, bool assoc)
{
Line 686 ⟶ 687:
old->expr_ = '(' + old->expr_ + ')';
}
 
void AddToken(stack<Entry_>* stack, const string& token)
{
Line 694 ⟶ 695:
{ // it's an operator
if (stack->size() < 2)
throw exception(cout<<"Stack underflow");
auto rhs = stack->top();
Parenthesize(&rhs, token, false);
Line 704 ⟶ 705:
}
}
 
 
string ToInfix(const string& src)
{
Line 721 ⟶ 722:
}
if (stack.size() != 1)
throw exception(cout<<"Incomplete expression");
return stack.top().expr_;
}
 
int main(void)
{
Line 738 ⟶ 739:
return -1;
}
}
}</lang>
Output :
<pre>
3 + (4 * 2) / (1 - 5) ^ 2 ^ 3
((1 + 2) ^ (3 + 4)) ^ (5 + 6)
</pre>
 
=={{header|Common Lisp}}==
503

edits