示例#1
0
文件: cc.c 项目: Fedjmike/mini-c
void unary () {
    if (try_match("!")) {
        //Recurse to allow chains of unary operations, LIFO order
        unary();

        fputs("cmp eax, 0\n"
              "mov eax, 0\n"
              "sete al\n", output);

    } else if (try_match("-")) {
        unary();
        fputs("neg eax\n", output);

    } else {
        //This function call compiles itself
        object();

        if (see("++") || see("--")) {
            fprintf(output, "mov ebx, eax\n"
                            "mov eax, [ebx]\n"
                            "%s dword ptr [ebx], 1\n", see("++") ? "add" : "sub");

            needs_lvalue("assignment operator '%s' requires a modifiable object\n");
            next();
        }
    }
}
示例#2
0
文件: parser.cpp 项目: Jenny-fa/lingo
// Parse a unary epxression. A unary expressions is one
// that begins with an operator and is followed by a
// unary expression.
//
//    unary-expression ::=
//        primary-expression
//      | unary-operator unary-expression.
Expr const*
Parser::unary()
{
  if (Token tok = match_if(plus_tok))
    return on_unary(tok, unary());
  if (Token tok = match_if(minus_tok))
    return on_unary(tok, unary());
  return primary();
}
示例#3
0
文件: expr.c 项目: BigEd/Cores
int64_t unary()
{
    int64_t val;
    
    switch(token) {
    case '!': NextToken(); return !unary();
    case '-': NextToken(); return -unary();
    case '+': NextToken(); return +unary();
    case '~': NextToken(); return ~unary();
    default:
        return primary();
    }
}
示例#4
0
Node *primary(void)
{
    Node *np;

    switch (rtok) {
    case CHAR:
        np = op2(CHAR, NIL, itonp(rlxval));
        rtok = relex();
        return (unary(np));
    case ALL:
        rtok = relex();
        return (unary(op2(ALL, NIL, NIL)));
    case EMPTYRE:
        rtok = relex();
        return (unary(op2(ALL, NIL, NIL)));
    case DOT:
        rtok = relex();
        return (unary(op2(DOT, NIL, NIL)));
    case CCL:
        np = op2(CCL, NIL, (Node*) cclenter((char *) rlxstr));
        rtok = relex();
        return (unary(np));
    case NCCL:
        np = op2(NCCL, NIL, (Node *) cclenter((char *) rlxstr));
        rtok = relex();
        return (unary(np));
    case '^':
        rtok = relex();
        return (unary(op2(CHAR, NIL, itonp(HAT))));
    case '$':
        rtok = relex();
        return (unary(op2(CHAR, NIL, NIL)));
    case '(':
        rtok = relex();
        if (rtok == ')') {	/* special pleading for () */
            rtok = relex();
            return unary(op2(CCL, NIL, (Node *) tostring("")));
        }
        np = regexp();
        if (rtok == ')') {
            rtok = relex();
            return (unary(np));
        }
        else
            FATAL("syntax error in regular expression %s at %s", lastre, prestr);
    default:
        FATAL("illegal primary in regular expression %s at %s", lastre, prestr);
    }
    return 0;	/*NOTREACHED*/
}
示例#5
0
Expression* Syntactic::F() {
	Expression* value = nullptr;
	Expression* una = nullptr;
	Expression* aux = nullptr;

	una = unary();
	if(lexic->type == Token::IDENTIFIER) {
		value = new ID(lexic->type,lexic->symbol);
		lexic->Next();
	}
	else if(lexic->type == Token::INTEGER || lexic->type == Token::FLOAT) {
		value = new Value(lexic->type,lexic->symbol);
		lexic->Next();
	}
	else if(lexic->type == Token::PARENTHESES_O) {
		lexic->Next();
		value = Expr();
		Check(")");
	} else {
		Error();
	}

	if(una != nullptr) {
		aux = una;
		while(aux->r != nullptr)
			aux = aux->r;
		aux->r = value;
		value = una;
	}
	return value;
}
示例#6
0
文件: .old.c 项目: jpirsch/piscines
t_btree		*P_ops(char *str, int i)
{
	t_btree *tree;
	t_btree *tree1;
	op;

	if (str[i + 1] >= '0' && str[i + 1] <= '9')
	{
		tree = mkleaf(str, i + 1);
		read;
		return (tree);
	}
	else if (str[i + 1] == '(')
	{
		read;
		tree = E_ops(str, i);
		expect(')');
		return (tree);
	}
	else if (str[i + 1] == '-')
	{
		read;
		tree = P_ops(str, i);
		return (mknode(unary('-'), tree));
	}
	else
		write(2, "Parse error\n", 12);
	return (tree);
}
示例#7
0
文件: expr.c 项目: BigEd/Cores
int64_t mult_expr()
{
    int64_t val;
    
    val = unary();
    while(1) {
        switch(token) {
        case '*': NextToken(); val = val * unary(); break;
        case '/': NextToken(); val = val / unary(); break;
        case '%': NextToken(); val = val % unary(); break;
        default: goto j1;
        }
    }
j1:
    return val;
}
示例#8
0
文件: evaluate.c 项目: denizt/hatari
static void operation (long long value, char oper)
{
	/* uses globals par[], id.error[], op[], val[]
	 * operation executed if the next one is on same or lower level
	 */
	/* something to calc? */
	if(id.valid == true) {
		
		/* add new items to stack */
		PUSH(op, oper);
		PUSH(val, value);
		
		/* more than 1 operator  */
		if(op.idx > par.opx[par.idx]) {

			/* but only one value */
			if(val.idx == par.vax[par.idx]) {
				apply_prefix();
			} else {
				/* evaluate all possible operations */
				eval_stack();
			}
		}
		/* next a number needed */
		id.valid = false;
	} else {
		/* pre- or post-operators instead of in-betweens? */
		unary(oper);
	}
}
示例#9
0
文件: parser.cpp 项目: Jenny-fa/lingo
// Parse a multiplicative expression.
//
//    multiplicative-expression ::=
//        unary-expression
//      | multiplicative-expression multiplicative-operator unary-expression
Expr const*
Parser::multiplicative()
{
  Expr const* e = unary();
  while (true) {
    if (Token tok = match_if(star_tok))
      e = on_binary(tok, e, unary());
    else if (Token tok = match_if(slash_tok))
      e = on_binary(tok, e, unary());
    else if (Token tok = match_if(percent_tok))
      e = on_binary(tok, e, unary());
    else
      break;
  }
  return e;
}
示例#10
0
// Gradient computations
double DenseCRF::gradient( int n_iterations, const ObjectiveFunction & objective, VectorXf * unary_grad, VectorXf * lbl_cmp_grad, VectorXf * kernel_grad) const {
	// Run inference
	std::vector< MatrixXf > Q(n_iterations+1);
	MatrixXf tmp1, unary( M_, N_ ), tmp2;
	unary.fill(0);
	if( unary_ )
		unary = unary_->get();
	expAndNormalize( Q[0], -unary );
	for( int it=0; it<n_iterations; it++ ) {
		tmp1 = -unary;
		for( unsigned int k=0; k<pairwise_.size(); k++ ) {
			pairwise_[k]->apply( tmp2, Q[it] );
			tmp1 -= tmp2;
		}
		expAndNormalize( Q[it+1], tmp1 );
	}
	
	// Compute the objective value
	MatrixXf b( M_, N_ );
	double r = objective.evaluate( b, Q[n_iterations] );
	sumAndNormalize( b, b, Q[n_iterations] );

	// Compute the gradient
	if(unary_grad && unary_)
		*unary_grad = unary_->gradient( b );
	if( lbl_cmp_grad )
		*lbl_cmp_grad = 0*labelCompatibilityParameters();
	if( kernel_grad )
		*kernel_grad = 0*kernelParameters();
	
	for( int it=n_iterations-1; it>=0; it-- ) {
		// Do the inverse message passing
		tmp1.fill(0);
		int ip = 0, ik = 0;
		// Add up all pairwise potentials
		for( unsigned int k=0; k<pairwise_.size(); k++ ) {
			// Compute the pairwise gradient expression
			if( lbl_cmp_grad ) {
				VectorXf pg = pairwise_[k]->gradient( b, Q[it] );
				lbl_cmp_grad->segment( ip, pg.rows() ) += pg;
				ip += pg.rows();
			}
			// Compute the kernel gradient expression
			if( kernel_grad ) {
				VectorXf pg = pairwise_[k]->kernelGradient( b, Q[it] );
				kernel_grad->segment( ik, pg.rows() ) += pg;
				ik += pg.rows();
			}
			// Compute the new b
			pairwise_[k]->applyTranspose( tmp2, b );
			tmp1 += tmp2;
		}
		sumAndNormalize( b, tmp1.array()*Q[it].array(), Q[it] );
		
		// Add the gradient
		if(unary_grad && unary_)
			*unary_grad += unary_->gradient( b );
	}
	return r;
}
示例#11
0
void test_sequence_n(Sequence & seq, mpl::int_<1>)
{
    fobj f;
    COMPARE_EFFECT(f(element1), fusion::invoke_procedure(f , seq ));
    COMPARE_EFFECT(f(element1), fusion::invoke_procedure(f , const_(seq)));
    COMPARE_EFFECT(const_(f)(element1), fusion::invoke_procedure<fobj const  >(const_(f), seq ));
    COMPARE_EFFECT(const_(f)(element1), fusion::invoke_procedure<fobj const &>(const_(f), const_(seq)));

    fobj_nc nc_f;
    COMPARE_EFFECT(nc_f(element1), fusion::invoke_procedure<fobj_nc &>(nc_f, seq ));
    COMPARE_EFFECT(nc_f(element1), fusion::invoke_procedure<fobj_nc &>(nc_f, const_(seq)));
    COMPARE_EFFECT(const_(nc_f)(element1), fusion::invoke_procedure<fobj_nc const &>(const_(nc_f), seq ));
    COMPARE_EFFECT(const_(nc_f)(element1), fusion::invoke_procedure<fobj_nc const &>(const_(nc_f), const_(seq)));

    COMPARE_EFFECT(unary(element1), fusion::invoke_procedure<int (&)(int &)>(unary, seq));
    COMPARE_EFFECT(func_ptr1(element1), fusion::invoke_procedure(func_ptr1, seq));
    COMPARE_EFFECT(func_ptr2(element1), fusion::invoke_procedure(func_ptr2, seq));
    COMPARE_EFFECT(func_ptr3(element1), fusion::invoke_procedure(func_ptr3, seq));
    COMPARE_EFFECT(func_ptr4(element1), fusion::invoke_procedure(func_ptr4, seq));

    COMPARE_EFFECT(that.unary(element1), fusion::invoke_procedure(& members::unary, fusion::join(sv_ref_ctx,seq)));
    COMPARE_EFFECT(that.unary(element1), fusion::invoke_procedure(& members::unary, fusion::join(sv_ptr_ctx,seq)));
    COMPARE_EFFECT(that.unary(element1), fusion::invoke_procedure(& members::unary, fusion::join(sv_spt_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_obj_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_ref_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_ptr_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_spt_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_obj_c_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_ref_c_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_ptr_c_ctx,seq)));
    COMPARE_EFFECT(that.unary_c(element1), fusion::invoke_procedure(& members::unary_c, fusion::join(sv_spt_c_ctx,seq)));
}
示例#12
0
Node *unary(Node *np)
{
    switch (rtok) {
    case STAR:
        rtok = relex();
        return (unary(op2(STAR, np, NIL)));
    case PLUS:
        rtok = relex();
        return (unary(op2(PLUS, np, NIL)));
    case QUEST:
        rtok = relex();
        return (unary(op2(QUEST, np, NIL)));
    default:
        return (np);
    }
}
示例#13
0
static Tree expr3(int k) {
	int k1;
	Tree p = unary();

	for (k1 = prec[t]; k1 >= k; k1--)
		while (prec[t] == k1 && *cp != '=') {
			Tree r;
			Coordinate pt;
			int op = t;
			t = gettok();
			pt = src;
			p = pointer(p);

			if (op == ANDAND || op == OROR) {
				r = pointer(expr3(k1));

				if (events.points)
					apply(events.points, &pt, &r);
			} else
				r = pointer(expr3(k1 + 1));
			p = (*optree[op])(oper[op], p, r);
		}

	return p;
}
示例#14
0
int unary(){
	int t,t1;
	if(look->tag == '-'){
		move();
		t = temp++;
		t1 = unary();
		gen("t%d = -t%d\n",t,t1);
		return t;
	}else if(look->tag == '!'){
		move();
		t = temp++;
		t1 = unary();
		gen("t%d = !t%d\n",t,t1);
		return t;
	}
	else return factor();
}
示例#15
0
int term(){
	int t,t1,t2;
	token tok;
	t1 = unary();
	t = t1;
	while(look->tag == '*' || look->tag == '/'){
		tok = look;
		t = temp++;
		move();
		t2 = unary();
		if(tok->tag == '*')
			gen("t%d = t%d * t%d\n",t,t1,t2);
		if(tok->tag == '/')
			gen("t%d = t%d / t%d\n",t,t1,t2);
		t1 = t;
	}
	return t;
}
示例#16
0
int Expression::multiply()
{
    int val1 = unary();
    int kw = lexer.GetToken()->GetKeyword();
    while (kw == Lexer::star || kw == Lexer::divide || kw == Lexer::mod)
    {
        lexer.NextToken();
        if (lexer.GetToken() != NULL)
        {
            int val2 = unary();
            switch(kw)
            {
                case Lexer::star:
                    val1 *= val2;
                    break;
                case Lexer::divide:
                    if (val2 == 0)
                    {
                        throw new std::runtime_error("Divide by zero in preprocessor constant");
                        val1 = 1;
                    }
                    else
                    {
                        val1 /= val2;
                    }
                    break;
                case Lexer::mod:
                    if (val2 == 0)
                    {
                        throw new std::runtime_error("Divide by zero in preprocessor constant");
                        val1 = 1;
                    }
                    else
                    {
                        val1 %= val2;
                    }
                    break;
            }
        }
        kw = lexer.GetToken()->GetKeyword();
    }
    return val1;
}
示例#17
0
void Ontology::normalize() {
    hierarchy.closure();

    //reduce transitivity for universals
    set<const UniversalConcept *> s;   
    FOREACH(u, positive_universals) 
	FOREACH(i, transitive_roles)
	    if (hierarchy(*i, (*u)->role()->ID())) {
		const Role *t = factory.role(*i);
		const UniversalConcept *c = factory.universal(t, factory.universal(t, (*u)->concept()));
		if (t != (*u)->role())
		    unary(Concept::concept_decompose(*u), Disjunction(Concept::concept_decompose(c)));
		s.insert(c);
	    }
    FOREACH(u, s) {
	unary(Concept::concept_decompose((*u)->concept()), Disjunction(Concept::concept_decompose(*u)));
	positive_universals.insert(*u);
	positive_universals.insert((const UniversalConcept *) (*u)->concept());
    }
示例#18
0
文件: cc.c 项目: Fedjmike/mini-c
void expr (int level) {
    if (level == 5) {
        unary();
        return;
    }

    expr(level+1);

    while (  level == 4 ? see("+") || see("-") || see("*")
           : level == 3 ? see("==") || see("!=") || see("<") || see(">=")
           : false) {
        fputs("push eax\n", output);

        char* instr = see("+") ? "add" : see("-") ? "sub" : see("*") ? "imul" :
                      see("==") ? "e" : see("!=") ? "ne" : see("<") ? "l" : "ge";

        next();
        expr(level+1);

        if (level == 4)
            fprintf(output, "mov ebx, eax\n"
                            "pop eax\n"
                            "%s eax, ebx\n", instr);

        else
            fprintf(output, "pop ebx\n"
                            "cmp ebx, eax\n"
                            "mov eax, 0\n"
                            "set%s al\n", instr);
    }

    if (level == 2) while (see("||") || see("&&")) {
        int shortcircuit = new_label();

        fprintf(output, "cmp eax, 0\n"
                        "j%s _%08d\n", see("||") ? "nz" : "z", shortcircuit);
        next();
        expr(level+1);

        fprintf(output, "\t_%08d:\n", shortcircuit);
    }

    if (level == 1 && try_match("?"))
        branch(true);

    if (level == 0 && try_match("=")) {
        fputs("push eax\n", output);

        needs_lvalue("assignment requires a modifiable object\n");
        expr(level+1);

        fputs("pop ebx\n"
              "mov dword ptr [ebx], eax\n", output);
    }
}
示例#19
0
/* Is a unary + or -. */
void level5(double* result){
   char  op; 

  op = 0; 
  if((bas_token_type==DELIMITER) && (*bas_token=='+' || *bas_token=='-') ) {
    op = *bas_token; 
    get_token(); 
  }
  level6(result); 
  if(op)
    unary(op, result); 
}
示例#20
0
void CMPolynomial::level6(double& result)    // unary operators
{
	int op = 0;

	if (token == Plus || token == Minus || token == Not) {
		op = token;
		get_token();
	}
	level7(result);
	if (op)
		unary(op,result);
}
示例#21
0
void term()
{
	unary();
	while (s[lookahead]) {
		switch (s[lookahead]) {
		case '*':
			match('*');
			unary();
			printf("*");
			break;
		case '/':
			match('/');
			unary();
			printf("/");
			break;
		default:
			unary();
			return;
		}
	}
}
示例#22
0
void level5(double *hold)
{
	char op;

	op = 0;
	if (((token_type) == DELIMITER) && *(token) == '+' || *(token) == '-')
	{
		op = *(token);
		get_token();
	}
	level6( hold);

	if (op)
		unary(op, hold);
}
示例#23
0
void Ontology::subsumption(const Concept* c, const Concept* d) {
    if (c->type() == 'B' || d->type() == 'T')
	return;

    if (c->type() == 'T' && d->type() == 'U') {
	const UniversalConcept* u = (const UniversalConcept*) d;
	role_range.insert(make_pair(u->role()->ID(), Disjunction(Concept::concept_decompose(u->concept()))));
	u->concept()->accept(*pos_str);
	c->accept(*neg_str);
	return;
    }
    c->accept(*neg_str);
    d->accept(*pos_str);
    unary(c->ID(), Disjunction(Concept::concept_decompose(d)));
}
示例#24
0
MatrixXf DenseCRF::inference ( int n_iterations ) const {
	MatrixXf Q( M_, N_ ), tmp1, unary( M_, N_ ), tmp2;
	unary.fill(0);
	if( unary_ )
		unary = unary_->get();
	expAndNormalize( Q, -unary );
	
	for( int it=0; it<n_iterations; it++ ) {
		tmp1 = -unary;
		for( unsigned int k=0; k<pairwise_.size(); k++ ) {
			pairwise_[k]->apply( tmp2, Q );
			tmp1 -= tmp2;
		}
		expAndNormalize( Q, tmp1 );
	}
	return Q;
}
示例#25
0
void ExprEvaluator::P() {
  tokenizer.nextToken();
  if(tokenizer.isNumber()){
    operands.push_back(tokenizer.value());
    tokenizer.consumeToken();
  }else if(tokenizer.isOpenBrace()){
    tokenizer.consumeToken();
    operators.push_back(oper::SANTINEL);
    E();
    expect(ExprTokenizer::token_type::CLOSE_BRACE);
    pop(operators);
  } else if(tokenizer.isUnary()){
    pushOperator(unary(tokenizer.nextToken()));
    tokenizer.consumeToken();
    P();
  } else
    throw new Error(tokenizer.getExpr(), tokenizer.getTokPos(), ":Arithmetic expression missmatch."); 
}
示例#26
0
Expression* Syntactic::unary() {
	Expression* ope = nullptr;
    Expression* aux = nullptr;

	std::string auxs;
	int auxt;

	if (lexic->type == Token::ADD ||
		lexic->type == Token::SUB) {
			auxs = lexic->symbol;
			auxt = lexic->type;
			lexic->Next();
			aux = unary();
			ope = new Unary(aux,auxt,auxs);
		}

	return ope;
}
示例#27
0
NodeInt term(){
	NodeInt nodeL = unary();
	switch (CurrToken) {
		case MUL_OP:{
			Token tok = mutch(MUL_OP);
			NodeInt nodeR = term();
			return AddNode(tok, nodeL, 0,nodeR);
			break;
		}
		case DIV_OP:{
			Token tok = mutch(DIV_OP);
			NodeInt nodeR = term();
			return AddNode(tok, nodeL, 0,nodeR);
			break;
		}
		default:
			break;
	}
	return nodeL;
}
示例#28
0
int
intunary (char *fname)
{
  int ainf, asup, op;
  int sa[2], flag[1];
  int cinf, csup, f;
  int ma, na;
  int r, s;
  int t, u, v, w;

  CheckRhs (3, 3);
  CheckLhs (2, 3);

  GetRhsVar (1, "d", &na, &ma, &ainf);
  GetRhsVar (2, "d", &na, &ma, &asup);
  GetRhsVar (3, "c", &t, &u, &op);

  r = na;
  s = ma;
  v = 1;
  w = 1;

  CreateVar (4, "d", &r, &s, &cinf);
  CreateVar (5, "d", &r, &s, &csup);
  CreateVar (6, "i", &v, &w, &f);

  sa[0] = na;
  sa[1] = ma;

  unary (stk (ainf), stk (asup), sa, *cstk (op),
	 stk (cinf), stk (csup), flag);

  *istk (f) = flag[0];

  LhsVar (1) = 4;
  LhsVar (2) = 5;
  LhsVar (3) = 6;

  return 0;

}
示例#29
0
int Expression::unary()
{
    if (lexer.GetToken() != NULL)
    {
        int kw = lexer.GetToken()->GetKeyword();
        if (kw == Lexer::plus || kw == Lexer::minus || kw == Lexer::not || kw == Lexer::compl)
        {
            lexer.NextToken();
            if (lexer.GetToken() != NULL)
            {
                int val1 = unary();
                switch(kw)
                {
                    case Lexer::minus:
                        val1 = - val1;
                        break;
                    case Lexer::plus:
                        val1 = + val1;
                        break;
                    case Lexer::not:
                        val1 = !val1;
                        break;
                    case Lexer::compl:
                        val1 = ~val1;
                        break;
                }
                return val1;
            }
        }
        else
        {
            return primary();
        }
    }
    throw new std::runtime_error("Syntax error in constant expression");
    return 0;
}
示例#30
0
void expr()
{
	unary();
	while (s[lookahead]) {
		switch (s[lookahead]) {
		case '+':
			match('+');
			term();
			printf("+");
			break;
		case '-':
			match('-');
			term();
			printf("-");
			break;
		case '*':
		case '/':
			term();
			break;
		case ')':
			return;
		}
	}	
}