当前位置: 首页>>代码示例>>C++>>正文


C++ Token_stream类代码示例

本文整理汇总了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;
}
开发者ID:quantum0813,项目名称:beaker,代码行数:11,代码来源:lexer.hpp

示例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);
		}
	}
}
开发者ID:JayDz,项目名称:PPP-answers,代码行数:19,代码来源:8-1.cpp

示例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();
    }
}
开发者ID:Tigrolik,项目名称:git_workspace,代码行数:17,代码来源:calculator08buggy.cpp

示例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");
    }
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:18,代码来源:ex2-3.cpp

示例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;
}
开发者ID:dreamaddict,项目名称:Stroustrup-book-exercises,代码行数:17,代码来源:ch7calcexercises1-9.cpp

示例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");
	}
}
开发者ID:jkunlin,项目名称:dest_calculator,代码行数:29,代码来源:Calculator.cpp

示例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();
	}
}
开发者ID:AlexNazarov88,项目名称:My-study-materials,代码行数:19,代码来源:Calculator_6.7(v2).cpp

示例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;
    }
  }
}
开发者ID:tol60,项目名称:STROUSTRUP_C11,代码行数:18,代码来源:ch7.e.2.calculator08buggy_fixed.C

示例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;
	}
}
开发者ID:dreamaddict,项目名称:Stroustrup-book-exercises,代码行数:19,代码来源:ch6-ex3.cpp

示例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();
        }
    }
}
开发者ID:Tigrolik,项目名称:git_workspace,代码行数:18,代码来源:calculator08buggy.cpp

示例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	
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:18,代码来源:ex10.cpp

示例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'
		}
	}
}
开发者ID:thelastpolaris,项目名称:Programming-Principles-and-Practice-Using-C-,代码行数:18,代码来源:ex10.cpp

示例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;
		}
	}
}
开发者ID:k-mi,项目名称:Stroustrup_PPP,代码行数:18,代码来源:drill7_11.cpp

示例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;
}
开发者ID:JayDz,项目名称:PPP-answers,代码行数:18,代码来源:7-4.cpp

示例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");
    }
    
}
开发者ID:konstest,项目名称:cpp_learning,代码行数:50,代码来源:code.cpp


注:本文中的Token_stream类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。