int evaluate_infix(char **expr, t_list **vs, t_list **os) { int i; i = -1; while (expr[++i] != 0) { if (is_operator(expr[i]) == 0 && expr[i][0] != '(' && expr[i][0] != ')') pf(vs, expr[i]); else if (is_operator(expr[i])) { while (ft_list_size(*os) != 0 && g(expr[i]) <= g((*os)->data)) pf(vs, a(pop(os), ft_atoi(pop(vs)), ft_atoi(pop(vs)))); pf(os, expr[i]); } else if (expr[i][0] == '(') pf(os, expr[i]); else if (expr[i][0] == ')') { while ((*os)->data[0] != '(') pf(vs, a(pop(os), ft_atoi(pop(vs)), ft_atoi(pop(vs)))); pop(os); } } return (0); }
int eval_expr(char *str) { int i; char **tabstring; t_list *op; t_list *nb; op = 0; nb = 0; i = 0; tabstring = str_to_tab_string(str); show_tab_string(tabstring); my_putstr("\n"); while (tabstring[i] != 0) { if (is_operator(tabstring[i]) == 3)//si '(' on empile le retour de pqrenthesis mode my_put_in_list(&nb, parenthesis_mode(&i, tabstring)); if (is_operator(tabstring[i]) == 0)// si nombre on empile my_put_in_list(&nb, tabstring[i]); if (is_operator(tabstring[i]) == 1)//si operateur de basse priorite on empile my_put_in_list(&op, tabstring[i]); if (is_operator(tabstring[i]) == 2)// si operqteur de priorite on resolv resolv(&i, tabstring, &op, &nb); i = i + 1; } return (depile_all(op, nb)); }
void rule_1(t_token *cur_tok, t_list **list, char *input, \ int *index) { char cur; cur = input[*index]; if (is_operator(cur) && are_complementary(input, *index)) build_token(cur_tok, cur); else { if (cur_tok->type == WORD) { build_token(cur_tok, cur); return ; } append_and_reset(list, cur_tok); if (is_operator(cur)) cur_tok->type = OPERATOR; discard_whitespace(input, index); if (!is_whitespace(cur)) build_token(cur_tok, cur); else cur_tok->preceded_by_whitespace = 1; } }
void loop(t_list **list, t_token **cur_tok, char *input) { int index; char cur; char prev; ft_init(&index, &cur, &prev, input); while (cur) { if ((*cur_tok)->token && is_operator(prev) && !is_newline(cur)) rule_1(*cur_tok, list, input, &index); else if (is_quoting(cur)) handle_quoting(*cur_tok, list, input, &index); else if (is_start_of_expansion(cur) || is_operator(cur)) rule_34(*cur_tok, list, input, &index); else if (is_newline(cur) || is_whitespace(cur)) rule_56(*cur_tok, list, input, &index); else if ((*cur_tok)->token && is_word(prev)) build_token(*cur_tok, cur); else if (cur == '#') discard_comment(input, &index); else build_token(*cur_tok, cur); prev = cur; cur = input[++index]; } }
void resolv(int *i, char **tabstring, t_list **op, t_list **nb) { char *save; int new_val; char *str; my_put_in_list(op, tabstring[*i]); *i =*i + 1; if (is_operator(tabstring[*i]) == 0) my_put_in_list(nb, tabstring[*i]); if (is_operator(tabstring[*i]) == 3) my_put_in_list(nb, parenthesis_mode(i, tabstring)); //if (is_operator(tabstring[*i]) == 1) //{ //save = malloc(sizeof(*save) * (my_strlen((*nb)->data) + 1)); //save = (*nb)->data; //*i = *i + 1; //my_strncat(save,(*nb)->data,my_strlen((*nb)->data)); // } //else //{ save = malloc(sizeof(*save) * (my_strlen((*nb)->data) + 1)); save =(*nb)->data; //} depile(nb); if (my_list_size(*(nb)) == 0) my_put_in_list(nb, "0"); new_val = eval(my_getnbr((*nb)->data), *(*op)->data, my_getnbr(save)); depile(op); depile(nb); str = malloc(sizeof(*str) * (digit_len(new_val) + 1)); my_itoa(new_val, str); my_put_in_list(nb, str); free(save); }
int eval_expr(char *str) { int i; char **tabstring; t_list *op; t_list *nb; op = 0; nb = 0; i = 0; tabstring = str_to_tab_string(str); while (tabstring[i] != 0) { if (is_operator(tabstring[i]) == 3) my_put_in_list(&nb, parenthesis_mode(&i, tabstring)); if (is_operator(tabstring[i]) == 0) my_put_in_list(&nb, tabstring[i]); if (is_operator(tabstring[i]) == 1) my_put_in_list(&op, tabstring[i]); if (is_operator(tabstring[i]) == 2) resolv(&i, tabstring, &op, &nb); i = i + 1; } return (depile_all(op, nb)); }
int expression(void) { int value = 0; char op = 0, ch = 0; skip_space(); ch = str[idx]; if (ch == '\0') { return 0; } if (ch == '(') { idx += 1; /* read '(' */ value = expression(); skip_space(); idx += 1; /* read ')' */ skip_space(); if (is_operator()) { op = operator(); /*printf("%d|%c ", order++, op);*/ int v = expression(); return do_num(op, value, v); } else { return value; } } else if (ch == '_' || isdigit(ch)) { int num = number(); /*printf("%d|%d ", order++, num);*/ skip_space(); if (is_operator()) { op = operator(); /*printf("%d|%c ", order++, op);*/ value = expression(); return do_num(op, num, value); } else { return num; } } else { char var = variable(); /*printf("%d|%c ", order++, var);*/ skip_space(); if (is_operator()) { op = operator(); /*printf("%d|%c ", order++, op);*/ value = expression(); return do_var(op, var, value); } else { return vars[var - 'A']; } } }
int Evaluate_Reverse_Polish_Notation(char **str, int len) { int top = -1; int *stk = new int[len]; int i; int operand_a; int operand_b; int temp = 0; for (i = 0; i < len; i++) { if (is_operator(str[i])) { operand_b = stk[top--]; operand_a = stk[top--]; stk[++top] = comp(operand_a, operand_b, *str[i]); } else { while (isdigit(*str[i])) { temp = temp * 10 + (*str[i] - '0'); str[i] ++; } //printf("%d\n", top); stk[++top] = temp; //i--; temp = 0; } } temp = stk[top]; delete stk; return temp; }
double calculate_post(queue<string>& post) { stack<double> result_stack; while(!post.empty()) { string temp = post.front(); post.pop(); if(is_operator(temp[0])) { //是操作符 if(result_stack.size()<2) { cout<<"表达式错误"<<endl; exit(-1); } //从栈中取出两个元素,计算并将结果压入栈中 double operand2 = result_stack.top(); result_stack.pop(); double operand1 = result_stack.top(); result_stack.pop(); double m = calculate_two(operand1, operand2, temp); result_stack.push(m); } else { //操作数 double temp_operand = atof(temp.c_str()); result_stack.push(temp_operand); } } return result_stack.top(); }
bool infix_is_correct(std::string infix_str){ int brk_k=0; int opr_k=0; for(int i=0;i<infix_str.length();++i){ if(prior(infix_str[i])==-1){ return false; } if(infix_str[i]=='('){ ++brk_k; } else if(infix_str[i]==')'){ --brk_k; } else if(infix_str[i]==' '){ ++opr_k; } else if(is_operator(prior(infix_str[i]))){ opr_k-=2; } if(brk_k<0){ return false; } } return (brk_k==0 && opr_k==1 )?true:false; }
int evaluate_postfix(const char *expression) { assert(expression && "No expression"); int operand = NOT_SET; DynamicArrayStack *stack = dynamic_array_stack_create(); const char *temp = expression; do { const char ch = *temp; if (is_digit(ch)) { operand = max(to_digit_int(ch), operand * BASE + to_digit_int(ch)); } else if (ch == ' ' || ch == '\0') { if (operand != NOT_SET) { dynamic_array_stack_push(stack, operand); operand = NOT_SET; } } else { assert(is_operator(ch) && "Unsupported character"); const int operand2 = dynamic_array_stack_top(stack); dynamic_array_stack_pop(stack); const int operand1 = dynamic_array_stack_top(stack); dynamic_array_stack_pop(stack); dynamic_array_stack_push(stack, apply(ch, operand1, operand2)); } } while (*(temp++) != '\0'); assert(operand == NOT_SET && "Operand was not flushed"); assert(dynamic_array_stack_size(stack) == 1 && "Result must be on stack"); const int result = dynamic_array_stack_top(stack); dynamic_array_stack_destroy(stack); return result; }
// Evaluate Postfix Expression. int evaluatePostfixExpression(const vector<string> &postfix) { if (postfix.empty()) { return 0; } stack<string> s; for (const auto& tok : postfix) { if (!is_operator(tok)) { s.emplace(tok); } else { int y = stoi(s.top()); s.pop(); int x = stoi(s.top()); s.pop(); if (tok[0] == '+') { x += y; } else if (tok[0] == '-') { x -= y; } else if (tok[0] == '*') { x *= y; } else { x /= y; } s.emplace(to_string(x)); } } return stoi(s.top()); }
int lexer(char *line, char **env) { char *op; if ((op = is_operator(line))) return (exec_op(op, line, env)); ft_execve(env, line); return (0); }
bool iscalcformula (const TCHAR *formula) { for (int i = 0; i < _tcslen (formula); i++) { TCHAR c = formula[i]; if (is_operator (c)) return true; } return false; }
//------------------------------------------------------------------ void code_colorer::color_code(const char_type* p) { while(*p) { unsigned len; if(is_block_comment(p, &len)) { m_result.add(keyword_rem, p, len, true); p += len; } else if(is_line_comment(p, &len)) { m_result.add(keyword_rem, p, len, true); p += len; } else if(is_string_literal(p, &len)) { m_result.add(keyword_str, p, len, true); p += len; } else if(is_number(p, &len)) { m_result.add(keyword_num, p, len, true); p += len; } else if(is_operator(p, &len)) { m_result.add(keyword_op, p, len, true); p += len; } else if(is_identifier(p, &len)) { const keyword* kw = 0; if (is_keyword(keyword_kw1_suffix, p, len)) kw = &keyword_kw1; else if(is_keyword(keyword_kw2_suffix, p, len)) kw = &keyword_kw2; else if(is_keyword(keyword_kw3_suffix, p, len)) kw = &keyword_kw3; else if(is_keyword(keyword_kw4_suffix, p, len)) kw = &keyword_kw4; if(kw) { m_result.add(backslash); m_result.add(kw->name, kw->len); m_result.add(open_brace); } m_result.add(p, len, true); if(kw) { m_result.add(close_brace); } p += len; } else { m_result.add(*p++); } } }
void Lex_analyzer::addToken(int tokenType, QString tokenValue){ TokenType *t = new TokenType; Token *tok = new Token; switch(tokenType){ case 1: if(is_keyword(tokenValue,t)){ qDebug() <<"Token" <<tokenValue <<"found in keywords: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "keywords"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else if(is_type(tokenValue,t)){ qDebug() <<"Token" <<tokenValue <<"found in types: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "types"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else{ is_id(tokenValue, t); qDebug() <<"Token" <<tokenValue <<"found in/inserted to ids: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "ids"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); } break; case 2: is_const(tokenValue, t); qDebug() <<"Token" <<tokenValue <<"found in/inserted to constants: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "constants"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); break; case 3: case 4: if(is_operator(tokenValue, t)){ qDebug() <<"Token" <<tokenValue <<"found in operators/separators: id=" <<t->id <<"type=" <<t->type <<endl; tok->id=t->id; tok->tokclass = "separators"; tok->toktype = t->type; tok->value = tokenValue; tokens->append(tok); }else{ qDebug() <<"Error unknown token type: " <<tokenType <<endl; } break; default: qDebug() <<"Error unknown token type: " <<tokenType <<endl; } qDebug() <<"Adding token of type[" <<tokenType <<"] = " <<tokenValue <<endl; }
static void strfilter_node__delete(struct strfilter_node *self) { if (self) { if (self->p && !is_operator(*self->p)) free((char *)self->p); strfilter_node__delete(self->l); strfilter_node__delete(self->r); free(self); } }
static void strfilter_node__delete(struct strfilter_node *node) { if (node) { if (node->p && !is_operator(*node->p)) zfree((char **)&node->p); strfilter_node__delete(node->l); strfilter_node__delete(node->r); free(node); } }
/* Determine if a command is complex (has an operator like pipe '|') */ int is_complex_command(char **tokens) { int i = 0; while(tokens[i]) { if (is_operator(tokens[i])) return 1; i++; } return 0; }
static const unsigned char* skip_action( const unsigned char* action) { const unsigned char* as; unsigned char ac; as = action; for ( ; ; ) { ac = *as++; switch (ac) { case PT_END: return as-1; case PT_SEPARATOR: return as; case PT_PUT_ARG: case PT_VAR1: case PT_AUX: case PT_QUOTE: as++; case PT_ONE_OPT: case PT_LINE: case PT_MATCHED_TEXT: break; case PT_DOMAIN: #if MAX_DOMAINS < 256 as = skip_action( as+1 ); #else as = skip_action( as+2 ); /* +2 since domains take up 2 bytes */ #endif break; case PT_OP: { int n; for ( n = fnnargs[*as++] ; n > 0 ; n-- ) { assert ( *as != PT_END ); as = skip_action(as); } break; } #ifdef PT_ARG_DELIM case PT_ARG_DELIM: #endif case PT_WORD_DELIM: case PT_ID_DELIM: case PT_SPACE: case PT_SKIP_WHITE_SPACE: break; default: assert( !is_operator(ac) ); break; } /* end switch ac */ } /* end for */ } /* end skip_action */
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; }
// Build expression tree by prefix expression. ExpressionTreeNode* buildExpressionTree(vector<string>& prefix, int& start) { if (prefix.empty()) { return nullptr; } ExpressionTreeNode *node = new ExpressionTreeNode(prefix[start++]); if (is_operator(prefix[start - 1])) { node->left = buildExpressionTree(prefix, start); node->right = buildExpressionTree(prefix, start); } return node; }
void read_token(t_env *env, char *str, int *i) { while (str[*i] == ' ' || str[*i] == '\t') (*i)++; if (is_number(str[*i])) add_to_queue(env, read_number(str, i)); else if (is_operator(str[*i])) add_operator(env, str, i); else add_paranthesis(env, str, i); }
std::vector<char> ExpressionParser::parse_expression(const std::string& expression) { output_.clear(); for (size_t pos = 0; pos < expression.length(); pos++) { if (is_left_paren(expression[pos])) process_left_paren(); else if (is_right_paren(expression[pos])) process_right_paren(); else if (is_operator(expression[pos])) process_operator(expression[pos]); else output_.push_back(expression[pos]); } while (!token_stack_.empty()) { assert(is_operator(token_stack_.top())); move_top_token_to_output(); } for (size_t i = 0; i < output_.size(); i++) if (output_[i] == EPSILON_INPUT_CHAR) output_[i] = (char)EPSILON; return output_; }
bool CheckTree(creek::tree<std::string> const& _tree) { typedef creek::tree<std::string>::const_pre_order_iterator const_pre_order_iterator; const_pre_order_iterator it = _tree.pre_order_begin(); const_pre_order_iterator end = _tree.pre_order_end(); while(it != end){ if((creek::child_order_begin(it) != creek::child_order_end(it)) && !is_operator(*it)) return false; if((creek::child_order_begin(it) == creek::child_order_end(it)) && !is_number(*it)) return false; ++it; } return true; }
int get_operator_pos(char *str, int pos, int pre_op) { int i; if (pre_op == 1) { i = pos - 1; while (i > 0 && is_operator(str[i], 0) == 0) --i; if (str[i] == '-' && i > 0 && is_operator(str[i - 1], 0) == 1) --i; return (i); } else { i = pos + 1; if (str[i] == '-' && i != 1) ++i; while (str[i] != '\0' && is_operator(str[i], 0) == 0) ++i; return (i); } }
static void postfix_infix(const char *postfix) { assert(postfix != 0); printf("Postfix expression: %s\n", postfix); char c; bool valid = true; Stack stack; stack_init(&stack); while ((c = *postfix++) != '\0') { if (isdigit(c)) stack_push_chr(&stack, c); else if (is_operator(c)) { char *rhs = stack_pop(&stack); char *lhs = stack_pop(&stack); size_t l_rhs = strlen(rhs); size_t l_lhs = strlen(lhs); char expr[l_lhs + l_rhs + sizeof("()*()")]; snprintf(expr, sizeof(expr), "%s%s%s%c%s%s%s", (l_lhs > 1) ? "(" : "", lhs, (l_lhs > 1) ? ")" : "", c, (l_rhs > 1) ? "(" : "", rhs, (l_rhs > 1) ? ")" : ""); stack_push_str(&stack, expr); free(lhs); free(rhs); } else if (isspace(c)) continue; else { err_remark("Invalid character %c (0x%.2X) found\n", c, c); valid = false; break; } } if (valid) { if (stack.tos != 1) err_remark("Stack has wrong number of entries (%zu instead of 1)\n", stack.tos); else { char *infix = stack_pop(&stack); printf("Infix expression: %s\n", infix); free(infix); } } stack_free(&stack); }
int apply(const char operation, const int operand1, const int operand2) { assert(is_operator(operation) && "Unsupported operation"); switch (operation) { case '+': return operand1 + operand2; case '-': return operand1 - operand2; case '/': return operand1 / operand2; case '*': return operand1 * operand2; default: assert(false && "Unsupported operation"); } }
/* args: {:} return value: return the value of expression (now only uint32) if the `e` is valid, or -1 Usage: */ uint32_t expr(char *e, bool *success) { if(!make_token(e)) { *success = false; return 0; } int i = 0; for (;i < nr_token; ++i) { if (tokens[i].type == MIN && (i == 0 || (i != 0 && is_operator(tokens[i-1].type) ))) { tokens[i].type = NEGA; puts("in negation"); } else if (tokens[i].type == MUL && (i == 0 || (i != 0 && is_operator(tokens[i-1].type) ))) { tokens[i].type = DEREF; } } su = true; uint32_t res = eval(0, nr_token); *success = su; return res; }
std::vector<std::string> postfix(std::string infix_str){ if(!infix_is_correct(infix_str)){ std::vector<std::string> err; err.push_back(infix_str); return err; } std::stack<char> char_stack; std::vector<std::string> result; char clean_number[5]; int clean_index=0; for(int i=0;i<infix_str.size() ;++i){ int q=prior(infix_str[i]); if(is_number(q)){ clean_number[clean_index++]=infix_str[i]; } else if(is_space(q) && clean_index>0){ clean_number[clean_index]='\0'; result.push_back(clean_number); clean_index=0; } else if(is_operator(q)){ while(!char_stack.empty() && prior(char_stack.top())>q){ save(char_stack,result); } char_stack.push(infix_str[i]); } else if(is_brk_open(q)){ char_stack.push(infix_str[i]); }else if(is_brk_close(q)){ while( char_stack.top()!='('){ save(char_stack,result); } char_stack.pop(); } } while(!char_stack.empty()){ save(char_stack,result); } return result; }