本文整理汇总了C++中TokenStream::empty方法的典型用法代码示例。如果您正苦于以下问题:C++ TokenStream::empty方法的具体用法?C++ TokenStream::empty怎么用?C++ TokenStream::empty使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TokenStream
的用法示例。
在下文中一共展示了TokenStream::empty方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
std::stack<std::unique_ptr<Token> > exprTree::getStack(TokenStream &ts) {
std::stack<std::unique_ptr<Token> > reverse;
while(!ts.empty()) {
std::unique_ptr<Token> t = ts.get();
reverse.push(std::move(t));
}
std::stack<std::unique_ptr<Token> > returnStack;
while (!reverse.empty()) {
returnStack.push(std::move(reverse.top()));
reverse.pop();
}
return returnStack;
}
示例2: if
std::stack<std::unique_ptr<Token> > exprTree::inToPost(TokenStream &ts) {
/* This will take your lousy stream of infix tokens and turn it into
* a glorious stack of tokens in postfix notation
*
* It is created in such a way the the begginning of the equation lies at the bottom of the stack
* so the stack is reversed at the end */
std::stack<std::unique_ptr<Token> > postEqn; // Reverse
std::stack<std::unique_ptr<Token> > workingStack;
while (!ts.empty()) {
std::unique_ptr<Token> t = ts.get();
if (t->kind == operand) {
postEqn.push(std::move(t));
} else if (t->kind == bracket) {
auto temp = static_cast<Bracket*>(t.release());
auto b = std::unique_ptr<Bracket>(temp);
if (b->typ == '(') {
workingStack.push(std::move(b));
} else {
// Closed bracket
bool matched = false; // Used for error checking
while(!workingStack.empty()) {
if (workingStack.top()->kind == bracket) {
// It must be open becuse closed don't go on this stack
workingStack.pop(); // We discard that open bracket
matched = true;
break;
} else { // a little redundant beause if true it returns anyway
// Add operand to the eqn
postEqn.push(std::move(workingStack.top()));
workingStack.pop();
}
}
if (!matched) {
std::cerr << "Unequal brackets!!" << std::endl;
}
}
} else if (t->kind == oprtor) {
auto temp = static_cast<Oprtor*>(t.release());
auto o = std::unique_ptr<Oprtor>(temp);
while(!workingStack.empty() &&
workingStack.top()->kind != bracket &&
(static_cast<Oprtor*>(workingStack.top().get()))->precedence >= o->precedence) {
postEqn.push(std::move(workingStack.top()));
workingStack.pop();
}
workingStack.push(std::move(o));
}
}
while(!workingStack.empty()) {
postEqn.push(std::move(workingStack.top()));
workingStack.pop();
}
// This is where it's reversed to we can actually use it
std::stack<std::unique_ptr<Token> > returnStack;
while (!postEqn.empty()) {
returnStack.push(std::move(postEqn.top()));
postEqn.pop();
}
return returnStack;
}