示例#1
0
static AVEvalExpr * parse_term(Parser *p){
    AVEvalExpr * e = parse_factor(p);
    while(p->s[0]=='*' || p->s[0]=='/'){
        int c= *p->s++;
        e= new_eval_expr(c == '*' ? e_mul : e_div, 1, e, parse_factor(p));
    }
    return e;
}
示例#2
0
文件: v7.c 项目: di3online/v7
//  term        =   factor { mul_op factor }
//  mul_op      =   "*" | "/"
static enum v7_err parse_term(struct v7 *v7) {
  TRY(parse_factor(v7));
  while (*v7->cursor == '*' || *v7->cursor == '/') {
    int ch = *v7->cursor;
    TRY(match(v7, ch));
    TRY(parse_factor(v7));
    TRY(do_arithmetic_op(v7, ch));
  }
  return V7_OK;
}
示例#3
0
static RegExp *
parse_term(void)
{
    RegExp *e, *f;
    e = parse_factor();
    while ((f = parse_factor())) {
	e = RegExp_new_CatOp(e, f);
    }
    return e;
}
示例#4
0
static bool parse_mulExpr(const char *&str, double &r) {
    if (!parse_factor(str, r)) return false;
    double rv;
    for (;;) {
        if (parse_char(str, '*') && parse_factor(str, rv)) {
            r *= rv;
        } else if (parse_char(str, '/') && parse_factor(str, rv)) {
            r /= rv;
        } else return true;
    }
}
示例#5
0
文件: 1400.c 项目: eagletmt/contests
expr *parse_term()
{
  expr *l = parse_factor();
  char c = getchar();
  while (c != '\n' && (c == '*' || c == '/')) {
    expr *r = parse_factor();
    l = binop(c, l, r);
    c = getchar();
  }
  ungetc(c, stdin);
  return l;
}
示例#6
0
static Value parse_factor (Context *s) {
    Value v = s->token_value;
    switch (s->token) {
    case '0':
        next (s);
        return v;
    case '-':
        next (s);
        return -parse_factor (s);
    case 'c':
        next (s);
        return s->col;
    case 'r':
        next (s);
        return s->row;
    case '(':
        next (s);
        v = parse_expr (s, 0);
        if (s->token != ')')
            complain (s, "Syntax error: expected ')'");
        next (s);
        return v;
    default:
        complain (s, "Syntax error: expected a factor");
        next (s);
        return 0;
    }
}
示例#7
0
Expr * parse_expr() {
    Binary_op * left = new Binary_op();
    if ((left->left = parse_factor()) == NULL) {
        return NULL;
    }
    if (operator_map[lookahead(0).tok] == NULL) {
        left->left->get_type();
        return left->left;
    }
    left->op = lookahead(0).tok;
    eat(left->op);
    if ((left->right = parse_factor()) == NULL) {
        error("right value expected");
    }
    Binary_op * ret = left;
    Binary_op * father = NULL;
    while (1) {
        Binary_op * _new = new Binary_op();
        if (operator_map[lookahead(0).tok] == NULL) {
            ret->get_type();
            return ret;
        }
        _new->op = lookahead(0).tok;
        eat(_new->op);
        if ((_new->right = parse_factor()) == NULL) {
            error("right value expected");
        }
        int temp = operator_map[left->op]->eye - operator_map[_new->op]->eye;
        if (temp > 0 || (temp == 0 && operator_map[_new->op]->left_associative)) {
            _new->left = left;
            if (father) {
                father->right = _new;
            }
            left = _new;
        } else {
            father = left;
            _new->left = left->right;
            left->right = _new;
            left = _new;
        }
    }
};
/******************************************************************************
    term := factor rest_term
 *****************************************************************************/
void
parse_term( val_t* val ) 
{
    printtab();
    dprintf("parse_term()\n");
    level++;

    parse_factor( val );
    parse_rest_term( val );

    level--;
    return;
}
示例#9
0
int parse_term(CALC_ELEMENT ** e)
{
	CALC_ELEMENT *t1 = NULL, *t2 = NULL;
	PARSE_SYMBOL s;
	if (parse_factor(&t1) != 0)
		return -1;
	s = get_symbol();
	while ((s == PARSE_MULT) || (s == PARSE_DIV)) {
		accept(s);
		if (parse_factor(&t2) != 0) {
			free_calc_element(t1);
			return -1;
		}
		if (s == PARSE_MULT)
			t1 = create_bin_op('*', t1, t2);
		else
			t1 = create_bin_op('/', t1, t2);
		t2 = NULL;
		s = get_symbol();
	}
	*e = t1;
	return 0;
}
示例#10
0
/******************************************************************************
    rest_term := * factor rest_term
                 / factor rest_term
                 % factor rest_term
                 <nil>
 *****************************************************************************/
void
parse_rest_term( val_t* val )
{
    printtab();
    dprintf("parse_rest_term()\n");
    level++;
    if ( match_char( '*' ) ) {
        val_t val2;
        parse_factor( &val2 );
        val->d.fval *= val2.d.fval;
        parse_rest_term( val );
    } else if ( match_char( '/' ) ) {
        val_t val2;
        parse_factor( &val2 );
        if ( val2.d.fval != 0 ) {
            val->d.fval /= val2.d.fval;
        } else {
            printf("Division by 0\n");
            longjmp(env, 0);
        }
        parse_rest_term( val );
    } else if ( match_char( '%' ) ) {
        val_t val2;
        parse_factor( &val2 );
        val->d.fval = fmod( val->d.fval, val2.d.fval );
        parse_rest_term( val );
    } else if ( match_eof() ) {

    } else {

    }

    level--;
    return;

}
示例#11
0
double NetworkSmpiModel::latencyFactor(double size)
{
    if (smpi_lat_factor.empty())
        smpi_lat_factor = parse_factor(xbt_cfg_get_string("smpi/lat-factor"));

    double current=1.0;
    for (auto fact: smpi_lat_factor) {
        if (size <= fact.factor) {
            XBT_DEBUG("%f <= %zu return %f", size, fact.factor, current);
            return current;
        } else
            current=fact.values.front();
    }
    XBT_DEBUG("%f > %zu return %f", size, smpi_lat_factor.back().factor, current);

    return current;
}
示例#12
0
/******************************************************************************
    factor := - factor
              num_op
 *****************************************************************************/
void
parse_factor( val_t* val ) 
{
    printtab();
    dprintf("parse_factor()\n");
    level++;

    if ( match_char( '-' ) ) {
        parse_factor( val );
        val->d.fval = val->d.fval;
    } else {
        parse_num_op( val );
    }

    level--;

    return;
}
示例#13
0
Factor_call::Factor_call() {
    name = lookahead(0).str;
    eat(tok_identifier);
    eat(tok_punc_lparen);
    if (lookahead(0).tok != tok_punc_rparen) {
        while (1) {
            Expr * temp;
            if ((temp = parse_factor()) == NULL) {
                error("factor expected");
            }
            args.push_back(temp);
            if (lookahead(0).tok == tok_punc_rparen) {
                break;
            }
            eat(tok_punc_comma);
        }
    }
    eat(tok_punc_rparen);
};
示例#14
0
static Value parse_expr (Context *s, int precedence) {
    Value lhs = parse_factor (s);
    for (;;) {
        int lp, rp, rator = s->token;  // left/right precedence and operator
        switch (rator) {
        case '+':
            lp = 1;
            rp = 2;
            break;
        case '-':
            lp = 1;
            rp = 2;
            break;
        case '*':
            lp = 3;
            rp = 4;
            break;
        case '/':
            lp = 3;
            rp = 4;
            break;
        case '%':
            lp = 3;
            rp = 4;
            break;
        case '^':
            lp = 5;
            rp = 5;
            break;
        case '@':
            lp = 7;
            rp = 8;
            break;
        default:
            return lhs;
        }
        if (lp < precedence)
            return lhs;
        next (s);
        lhs = apply (s, rator, lhs, parse_expr (s, rp));
    }
}
示例#15
0
文件: expression.c 项目: 38/basiclone
Data* parse_term( char** p )
{
	Data* ret = NULL;
	while(1)
	{
		char op;
		int sign;
		if( ret )
		{
			parse_ws(p);
			op = *((*p));
			if( op != '*' && op != '/' ) 
			{
				break; /*last chr should not be parsed*/
			}
			else
				(*p)++;
		}
		sign=parse_sign(p);
		if( sign == 0 ) sign = 1;
		Data* factor = parse_factor(p);
		if( factor->type == TYPE_ERROR )
		{
			dispose(ret);
			return factor; /*throw*/
		}
		if( sign == -1 )
		{
			if( factor->type == TYPE_STRING )
			{
				dispose(factor);
				dispose(ret);
				return Raise(SYNTAX);
			}
			else if( factor->type == TYPE_REAL )
				factor->storage.Real = -factor->storage.Real;
			else
				factor->storage.Integer = - factor->storage.Integer;
		}
		parse_ws(p);
		if( **p == '^' )
		{/*exponent*/
			(*p)++;
			Data* exponent = parse_factor(p);
			factor = temp_var(factor);
			if( exponent->type == TYPE_ERROR )
			{
				dispose(ret);
				dispose(factor);
				return exponent;
			}
			if( exponent->type == TYPE_STRING || factor->type == TYPE_STRING)
			{
				dispose(ret);
				dispose(factor);	
				dispose(exponent);
				return Data_new( TYPE_ERROR , (Storage)Error_new(TYPE_MISMATCH) , 1 );
			}
			if( factor->type == TYPE_INTEGER )
			{
				factor->storage.Real = factor->storage.Integer;
				factor->type = TYPE_REAL;
			}
			if( exponent->type == TYPE_INTEGER )
			{
				factor->storage.Real = factor->storage.Integer;
				factor->type = TYPE_REAL;
			}
			factor->storage.Real = exp( log(factor->storage.Real) * exponent->storage.Real); 
			dispose(exponent);
		}
		
		if( ret == NULL )
			ret = factor;
		else
		{
			ret = temp_var(ret);
			if( ret->type == TYPE_STRING )
			{
				dispose(ret);
				dispose(factor);
				return Raise(TYPE_MISMATCH);
			}
			else
			{
				if( op == '*' )
				{
					if( ret->type == TYPE_REAL || factor->type == TYPE_REAL )
					{
						if( ret->type == TYPE_INTEGER )
						{
							ret->type = TYPE_REAL;
							ret->storage.Real = ret->storage.Integer;
						}
						if( factor->type == TYPE_INTEGER )
						{
							factor->type = TYPE_REAL;
							factor->storage.Real = factor->storage.Integer;
						}
						ret->storage.Real *= factor->storage.Real;
											
					}
					else
						ret->storage.Integer *= factor->storage.Integer;
				}
				else
				{
					if( ret->type == TYPE_INTEGER )
					{
						ret->type = TYPE_REAL;
						ret->storage.Real = ret->storage.Integer;
					}
					if( factor->type == TYPE_INTEGER )
					{
						factor->type = TYPE_REAL;
						factor->storage.Real = factor->storage.Integer;
					}
					if( factor->storage.Real == 0 )
					{
						dispose( ret );
						dispose( factor );
						return Raise(DIV_BY_ZERO);
					}
					ret->storage.Real /= factor->storage.Real;
				}
					
			}
			dispose(factor);
		}
	}
	return ret;
}