Пример #1
0
 parse_result read_lvl(string input, int from, int lvl) {
     from = skipSpaces(input,from);
     if (from >= (int)input.length()) {
         throw "End of input reached before the parsing finished";
     }
     if (lvl <= 0) { // parens or variable
         if (input[from] == '(') {
             parse_result pr = read_lvl(input,from+1,START_LVL);
             pr.idx = skipSpaces(input, pr.idx);
             if (pr.idx >= (int)input.length()) {
                 throw "End of input reached before the parsing finished";
             } else if (input[pr.idx] != ')') {
                 throw "'(' at character "+pr.idx;
             }
             pr.idx = pr.idx+1;
             return pr;
         } else {
             return read_var(input, from);
         }
     } else {
         operateur op = operateur_for_level(lvl);
         string s_op = operateur2string(op);
         if (is_binary(op)) {
             parse_result pr1 = read_lvl(input,from,lvl-1);
             pr1.idx = skipSpaces(input,pr1.idx);
             if ( input.compare(pr1.idx, s_op.length(), s_op) == 0 ) {
                 parse_result pr2 = read_lvl(input,pr1.idx+(int)s_op.length(), lvl);
                 parse_result res;
                 res.f = new formule();
                 res.f -> op = op;
                 res.f -> arg1 = pr1.f;
                 res.f -> arg2 = pr2.f;
                 res.idx = pr2.idx;
                 return res;
             } else {
                 return pr1;
             }
         } else {
             if ( input.compare(from, s_op.length(), s_op) == 0 )  {
                 parse_result pr = read_lvl(input,from + (int)s_op.length(),lvl);
                 parse_result res;
                 res.idx = pr.idx;
                 res.f = new formule();
                 res.f->op = op;
                 res.f->arg = pr.f;
                 return res;
             } else {
                 return read_lvl(input,from,lvl-1);
             }
         }
     }
 }
Пример #2
0
string formule2string(const formule* f) {
    switch (f->op) {
        case o_variable:
            return *(f->nom);
        case o_non:
            return "~" + formule2string(f->arg);
        case o_et:
        case o_ou:
        case o_equivaut:
        case o_implique:
            return "(" + formule2string(f->arg1) + " " + operateur2string(f->op) + " " + formule2string(f->arg2) + ")";
    }
}