本文整理汇总了C++中Token_stream类的典型用法代码示例。如果您正苦于以下问题:C++ Token_stream类的具体用法?C++ Token_stream怎么用?C++ Token_stream使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Token_stream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1:
// Put the next token into the token stream. Returns
// true if scanning succeeded.
inline bool
Lexer::scan(Token_stream& ts)
{
if (Token tok = scan()) {
ts.put(tok);
return true;
}
return false;
}
示例2: calculate
void calculate(Token_stream& ts)
{
const string prompt = "> ";
const string result = "= ";
while (cin) {
try {
cout << prompt;
Token t = ts.get();
while (t.kind == print) t = ts.get();
if (t.kind == quit) return;
ts.putback(t);
cout << result << statement(ts) << endl;
}
catch (exception& e) {
cerr << e.what() << endl;
clean_up_mess(ts);
}
}
}
示例3: statement
double statement()
{
Token t = ts.get();
switch(t.kind) {
case sqrt_kind:
return func(sqrt_kind);
case power_kind:
return func(power_kind);
case let:
return declaration();
case name:
return assign(t.name);
default:
ts.unget(t);
return expression();
}
}
示例4: primary
// deal with numbers and parentheses
double primary()
{
Token t = ts.get();
switch (t.kind) {
case '(': case '{': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')' && t.kind !='}') error("')' or '}' expected");
return d;
}
case '8': // we use '8' to represent a number
return t.value; // return the number's value
default:
error("primary expected");
}
}
示例5: declaration
double declaration()
{
Token t = ts.get();
if (t.kind != VAR) {
ts.end();
error ("Valid name expected in declaration");
}
string name = t.name;
Token t2 = ts.get();
if (t2.kind != '=') {
ts.end();
error("'=' missing in declaration of " ,name);
}
double d = expression();
variables.set_value(name, d);
return d;
}
示例6: prim
double prim(bool get) {
if (get) ts.get();
switch (ts.current().kind) {
case Kind::number:
{
double v = ts.current().number_value;
ts.get();
return v;
}
case Kind::name:
{
double &v = table[ts.current().string_value];
if (ts.get().kind == Kind::assign) v = expr(true);
return v;
}
case Kind::minus:
{
return -prim(true);
}
case Kind::lp:
{
auto e = expr(true);
if (ts.current().kind != Kind::rp)
return error("'(' expected");
}
default:
return error("primary expected");
}
}
示例7: calculate
//------------------------------------------------------------------------------
void calculate()
{
double val = 0;
while (cin)
try {
cout << prompt;
Token t = ts.get();
while (t.kind == print) t=ts.get(); // first discard all “prints”
if (t.kind == quit) return;
ts.putback(t);
cout << result << statement() << '\n';
}
catch (exception& e) {
cerr << e.what() << '\n'; // write error message
clean_up_mess();
}
}
示例8: expression
double expression()
{ // deal we A+B and A-B
double left = term();
while(true) {
Token t = ts.get(); // get the next Token from the stream
switch(t.kind) {
case '+':
left += term();
break;
case '-':
left -= term();
break;
default:
ts.unget(t); // put the Token back onto the stream (we did not do any action here)
return left;
}
}
}
示例9: suffix
// check for a suffix after an expression (so far, only factorial) and evaluate.
double suffix(double expression)
{
Token t=ts.get();
switch(t.kind) {
case '!': // factorial (non-recursive algorithm)
{
int intexpr=(int)expression;
if(intexpr<0) error("cannot take the factorial of a negative number");
if(intexpr<2) return 1;
int fact=intexpr;
for(int i=intexpr;i>1;fact*=--i); // FORE
return fact;
}
default:
ts.putback(t);
return expression;
}
}
示例10: calculate
void calculate()
{
while(cin) {
try {
cout << prompt;
Token t = ts.get();
while (t.kind == print)
t = ts.get();
if (t.kind == quit)
return;
ts.unget(t);
cout << result << statement() << endl;
} catch(exception &e) {
cerr << e.what() << endl;
clean_up_mess();
}
}
}
示例11: pow
double pow(){ // Multiplies n by n m times
Token t = ts.get();
if (t.kind == '(') {
double lval = expression();
int rval = 0;
t = ts.get();
if(t.kind == ',') rval = narrow_cast<int>(primary());
else error("Second argument is not provided");
double result = 1;
for(double i = 0; i < rval; i++) {
result*=lval;
}
t = ts.get();
if (t.kind != ')') error("')' expected"); // If there wasn't any ')' return an error
return result;
}
else error("'(' expected"); // If there wasn't any ')' return an error
}
示例12: expression
double expression() // Performs '+' and '-' operations
{
double left = term(); // Get the number or the result of calculations in term
while(true) {
Token t = ts.get();
switch(t.kind) {
case '+':
left += term(); // Addition
break;
case '-':
left -= term(); // Subtraction
break;
default:
ts.unget(t); // If nothing was done return character to the stream
return left; // Return the new or unchanged value of 'left'
}
}
}
示例13: expression
double expression()
{
double left = term();
while(true) {
Token t = ts.get();
switch(t.kind) {
case '+':
left += term();
break;
case '-':
left -= term();
break;
default:
ts.unget(t);
return left;
}
}
}
示例14: declaration
double declaration(Token k)
// Taken from: http://www.stroustrup.com/Programming/Solutions/Ch7/e7-1.cpp
// handle: name = expression
// declare a variable called "name" with the initial value "expression"
// k will be "let" or "con"(stant)
{
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();
sym_table.declare(var_name,d,k.kind==let);
return d;
}
示例15: primary1
// deal with numbers and parentheses
double primary1()
{
Token t = ts.get();
switch (t.kind) {
case '(': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != ')')
error("')' expected");
else {
t = ts.get();
if (t.kind == '!')
d = factorial(d);
else
ts.putback(t);
}
return d;
}
case '{': // handle '(' expression ')'
{
double d = expression();
t = ts.get();
if (t.kind != '}')
error("'}' expected");
else {
t = ts.get();
if (t.kind == '!')
d = factorial(d);
else
ts.putback(t);
}
return d;
}
case '8': // we use '8' to represent a number
{
double d = t.value;
t = ts.get();
if (t.kind == '!')
d = factorial(d);
else
ts.putback(t);
return d; // return the number's value
}
default:
error("primary expexted");
}
}