Exemple #1
0
void test_is_operand()
{
    test(is_operand("56", "0123456789"));
    test(is_operand("BD", "0123456789") == 0);
    test(is_operand("BD", "0123456789ABCDEF"));

}
Exemple #2
0
int main(int argc, char ** argv)
{
	if (argc < 2) {
		printf("Usage: rpncalc op1 op2 ...\n");
		exit(1);
	}
	
	STACK * s = stack_create();

	int i;
	char * c;
	for (i = 1; i < argc; i++) {
		if (is_operand(c = argv[i])) {
			if (stack_is_empty(s)) {
				printf("Elements remain in the stack.\n");
				exit(1);
			} else {
				compute_operation(s, c);
			}
		} else {
			stack_push(s, atof(c));
		}
	}
	
	double result = s->top->val;
	stack_pop(s);
	if(stack_is_empty(s)) {
		printf("%f\n", result);
	} else {
		printf("Elements remain in the stack.\n");
	}

	exit(0);
}
SymbolListPtr CodeBlock::convert_postfix(SymbolListPtr p) {
	// data structs
	unique_ptr<stack<SymbolPtr>> op_stack = unique_ptr<stack<SymbolPtr>>(new stack<SymbolPtr>);
	SymbolListPtr unpostfix_symbols = SymbolListPtr(new SymbolList());
	
	// shunting yard (modified for unary operators!)
	for (auto i = p->begin(); i !=
		 p->end(); i++) {
		if (is_operand(*i)) {
			unpostfix_symbols->push_back(*i);
			if (!op_stack->empty()) {
				if (is_unary(op_stack->top())) {
					unpostfix_symbols->push_back(op_stack->top());
					op_stack->pop();
				}
			}
		} else if (is_operator(*i)) {
			if (is_unary((*i))) {
				op_stack->push(*i);
			} else {
				while (!op_stack->empty()
					   && !is_lparen(op_stack->top())
					   && compare_ops(*i, op_stack->top()) <= 0) {
					unpostfix_symbols->push_back(op_stack->top());
					op_stack->pop();
				}
				op_stack->push(*i);
			}
		} else if (is_lparen(*i)) {
			op_stack->push(*i);
		} else if (is_rparen(*i)) {
			while (!op_stack->empty()) {
				if (is_lparen(op_stack->top())) {
					op_stack->pop();
					if (!op_stack->empty()) {
						if (is_unary(op_stack->top())) {
							unpostfix_symbols->push_back(op_stack->top());
							op_stack->pop();
						}
					}
					break;
				}
				unpostfix_symbols->push_back(op_stack->top());
				op_stack->pop();
			}
		}
	}
	while(!op_stack->empty()) {
		unpostfix_symbols->push_back(op_stack->top());
		op_stack->pop();
	}
	
	// set the temp symbols as the postfix stuff
	return unpostfix_symbols;
}
Exemple #4
0
double
evaluate_expression(ListElmts *outputList)
{
   if(!outputList || outputList->type !=0)
   {
     print_error("Expression mal formée");
     exit(0); 
   }
   ListElmts *current,
             *stack = NULL,
             rslt
   ;
   current = outputList;
   while(current != NULL)
   {
      if(is_operand(current))
      {
         if(push_stack(&stack, current)!= TRUE)
         {
            print_error("Malloc");
            exit(0);
         }
      }
      else
      {

         if(stack->next == NULL)
         {
            print_error("Expression mal formée");
            exit(0);
         }
         rslt = make_operation(stack->next, stack, current);
         pop_stack(&stack);
         pop_stack(&stack);
         if(push_stack(&stack, &rslt)!=TRUE)
         {
            print_error("Malloc");
            exit(0);
         }
      }
      current = current->next;
   }
   return stack->data;
}
Exemple #5
0
void		find_operand(char *input, int *tuple)
{
	size_t	len;
	int		ret;

	tuple[0] = -1;
	tuple[1] = -1;
	if (!input)
		return ;
	len = ft_strlen(input) - 1;
	while (len)
	{
		ret = is_operand(input, (int)len);
		if (ret)
		{
			tuple[0] = ret;
			tuple[1] = (int)len;
			break ;
		}
		len--;
	}
}
Exemple #6
0
EntryType eval_expr(char * expr)
{
    char op, c = expr[0];
    int a, b;
    StackPtr OPTR, OPND;  
    
    stack_init(OPTR);
    stack_init(OPND);
    stack_push(OPTR, '#');
    
    while(c != '#' ||  OPTR->top->entry  != '#') {
        if (!is_operand(c)){         // 如果c是操作数
            stack_push(OPND, c);  // 对c进行入栈操作
            expr++;
            c = *expr;
        }
        else{
            switch(prec_compare(OPTR->top->entry, c) ){
                case '<':            // 栈顶元素优先级低于当前运算符
                    push(OPTR, c);    // 将当前运算符入栈
                    expr++;
                    c = *expr;            // 读入下一个字符
                    break;
                case '=':             // 处理括号
                    pop(OPTR, &op);  // 弹出左括号
                    expr++;
                    c = *expr;            // 读入下一个字符
                    break;
                case '>':            // 栈顶元素优先级高于当前运算符
                    pop(OPTR, &op);    // 运算符退栈
                    pop(OPND, &a); pop(OPND, &b);
                    push(OPND, calc(a, b, op));   // 将运算结果入栈
                    break;
            } // end switch
        }
    }// end while
    
    return OPND->top->entry;
}
Exemple #7
0
void infix2rpn(char *expr, RPNEntry ***rpne, int *nrpne) {
    Array *rpn = array_new(32, 16);
    Stack *stack_ops = stack_new(16);
    //static char token[64];
    char *token;
    int tokenlen;
    int i;

    int uminus = 1;
    int inoperand = 0;
    //printf("%s:-> ", expr);

    char lookahead;

    for(token = gettoken(expr, &tokenlen, &lookahead);token != NULL; token = gettoken(NULL, &tokenlen, &lookahead)) {
        if (uminus) {
            if (*token == T_MINUS) {
                *token = T_UMINUS;
            }
            uminus = 0;
        }

        if (is_function(token, tokenlen)) {
            stack_push(stack_ops, token);
        } else if (is_operand(token)) {
            array_add(rpn, token);
        } else if (is_arg_separator(token)) {
            while(!stack_empty(stack_ops) && !is_lparen(stack_peek(stack_ops))) {
                array_add(rpn, stack_pop(stack_ops));
            }
        } else if (is_operator(token)) {
            uminus = 1;
            if (stack_empty(stack_ops)) {
                stack_push(stack_ops, token);
            } else {
                if (compare_ops(token, stack_peek(stack_ops))) {
                    array_add(rpn, stack_pop(stack_ops));
                }
                stack_push(stack_ops, token);
            }
        } else if (is_lparen(token)) {
            uminus = 1;
            stack_push(stack_ops, token);
        } else if (is_rparen(token)) {
            while(!stack_empty(stack_ops) && !is_lparen(stack_peek(stack_ops))) {
                array_add(rpn, stack_pop(stack_ops));
            }
            stack_pop(stack_ops);
        } else {
            // errorski
        }
    }
    while (!stack_empty(stack_ops)) {
        array_add(rpn, stack_pop(stack_ops));
    }

    stack_free(&stack_ops);
    *rpne = (RPNEntry **)malloc(rpn->count * sizeof(RPNEntry*));
    for (i = 0; i < rpn->count; i++) {
        token = array_get(rpn, i);
        (*rpne)[i] = new_rpnentry(token, strlen(token));
    }
    *nrpne = rpn->count;
    array_free(&rpn);
}
Exemple #8
0
/* Generar el codigo con el algoritmo de seguimiento de registros */
void generate() {
	Element *obj;
	extern Symbol *symbol_table;

	fprintf(output_fd, ".MODEL SMALL\n.CODE\n\t\tORG\t100h\n__MAIN:\t\t\t\t\t; Cuerpo\n");
	while (element_stack != (Element *)0) {
		obj = pop();
		if (is_operand(obj)) {
			spush(&pila, (void *)obj);
		} else {
			switch (obj->code) {
				case '+':
					generar_suma(obj);
					break;
				case '-':
					generar_resta(obj);
					break;
				case '*':
					generar_multiplicacion(obj);
					break;
				case '/':
					generar_division(obj);
					break;
				case '=':
					generar_asignacion(obj);
					break;
				case '!':
					generar_not(obj);
					break;
				case AND:
					generar_and(obj);
					break;
				case OR:
					generar_or(obj);
					break;
				case MENOR_IGUAL:
					generar_comparacion(obj, "JBE");
					break;
				case MAYOR_IGUAL:
					generar_comparacion(obj, "JAE");
					break;
				case DISTINTO:
					generar_comparacion(obj, "JNE");
					break;
				case IGUAL:
					generar_comparacion(obj, "JE");
					break;
				case '>':
					generar_comparacion(obj, "JA");
					break;
				case '<':
					generar_comparacion(obj, "JB");
					break;
				case BF:
					generar_BF(obj);
					break;
				case BI:
					generar_BI(obj);
					break;
				case CL:
					create_label(obj);
					break;
			}
		}
	}
	// Rutina de fin de programa
	fprintf(output_fd, "\tLABEL FIN:\t\t\t; Fin de programa\n");
	fprintf(output_fd, "\t\tMOV\tAH, 00h\n");
	fprintf(output_fd, "\t\tINT\t21h\n");
	
	/* Generar las declaraciones de variables a partir de la tabla de simbolos */
	Symbol *sym;

	fprintf(output_fd, "\n\n\t\tORG\t1000h\t\t; Definicion de variables\n");
	while (symbol_table != (Symbol *)0) {
		sym = get_first();
		fprintf(output_fd, "\t\t_%s", sym->name);
		fprintf(output_fd, "\tDB\t0\n");
	}
	
	// Cierre de __MAIN
	fprintf(output_fd, "END __MAIN\n");
}
int value(char *s)
{
struct stack operand,operator;
create(&operand);
create(&operator);
int i=0,j,op1,op2;
char c,temp;
printf("operand stack\toperator stack\n");
while(s[i]!='\0')
{
for(j=0;j<=operand.top;j++)
printf("%d ",operand.data[j]);
printf("\t\t");
for(j=0;j<=operator.top;j++)
printf("%d ",operator.data[j]);
printf("\n");
c=s[i++];
if(is_operand(c)==1)
{
push(&operand,c-'0');
continue;
}
else
{
if(operator.top==-1)
{
push(&operator,c);
continue;
}
temp=pop(&operator);
if(preference(temp,c)==1)
{
op1=pop(&operand);
op2=pop(&operand);
push(&operator,c);
switch(temp)
{
case '+':{push(&operand,op2+op1);break;}
case '-':{push(&operand,op2-op1);break;}
case '*':{push(&operand,op2*op1);break;}
case '/':{push(&operand,(int)(op2/op1));break;}
case '$':{push(&operand,(int)(pow(op2,op1)));break;}
default:{printf("wrong symbol\n");exit(3);}
}
continue;
}
else
{
push(&operator,temp);
push(&operator,c);
}
}
}
while(operator.top!=-1)
{
op1=pop(&operand);
op2=pop(&operand);
switch(pop(&operator))
{
case '+':{push(&operand,op2+op1);break;}
case '-':{push(&operand,op2-op1);break;}
case '*':{push(&operand,op2*op1);break;}
case '/':{push(&operand,(int)(op2/op1));break;}
case '$':{push(&operand,(int)(pow(op2,op1)));break;}
default:{printf("wrong symbol\n");exit(3);}
}
}
return pop(&operand);
}
Exemple #10
0
ListElmts* 
construct_output_list(ListElmts *inputList)
{
   ListElmts *stack = NULL;
   ListElmts *outputList = NULL,
             *current
   ;
   current = inputList;
   while(current != NULL)
   {
      if (is_operand(current))
      {
         slist_append(&outputList, current->data, current->type);
      }
      else if (current->data == '(')
      {
         if (push_stack(&stack, current) != TRUE)
         {
            print_error("Malloc");
            exit(0);
         }
      }
      else if (is_operator(current))
      {
         while(stack != NULL && is_operator(stack) && (get_operator_priority(current) <= get_operator_priority(stack)))
         {
            slist_append(&outputList, stack->data, stack->type);
            pop_stack(&stack);
         }
         if (push_stack(&stack, current) != TRUE)
         {
            print_error("Malloc");
            exit(0);
         }
      }
      else if (current->data == ')')
      {  
         if (!stack)
         {
            print_error("Mauvaise parenthésage de l'expression");
            exit(0);
         }
         while(stack != NULL && stack->data != '(')
         {
            slist_append(&outputList, stack->data, stack->type);
            if (!(stack->next))
            {
               print_error("Mauvaise parenthésage de l'expression");
               exit(0);
            }
            pop_stack(&stack);
         }
         pop_stack(&stack);
      }
      current = current->next;
   }
   while(stack != NULL)
   {
      if (stack->data == '(')
      {
         print_error("Mauvaise parenthésage de l'expression");
         exit(0);
      }
      slist_append(&outputList, stack->data, stack->type);
      pop_stack(&stack);
   }
   return outputList;
}