Language: C++

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <set>
#include <tuple>
using namespace std;
typedef string::const_iterator State;
class ParseError {};


enum Operator { add, sub, mul, divi, fac, integer };

struct Node{
    int num;
    Operator op;
    vector<Node> children;
    Node(int n){
        num=n;
        op=integer;
    }
    Node(Operator o){
        num=-1;
        op=o;
    }
};

int number(State &begin) {
    int ret=0;
    while(isdigit(*begin)) ret=ret*10+*begin++-'0';
    // cout<<ret<<endl;
    return ret;
}

Node expr(State &begin){
    Node node(fac);
    while(true){
        if(*begin=='('){
            begin++;
            node.children.push_back(expr(begin));
            begin++;
        }else if(*begin==')'){
            return node;
        }else if(isdigit(*begin)){
            int n = number(begin);
            node.children.push_back(Node(n));
        }else if(*begin=='+'){
            node.children.push_back(Node(add));
            begin++;
        }else if(*begin=='-'){
            node.children.push_back(Node(sub));
            begin++;
        }else if(*begin=='*'){
            node.children.push_back(Node(mul));
            begin++;
        }else if(*begin=='/'){
            node.children.push_back(Node(divi));
            begin++;
        }else{
            break;
        }
    }
    return node;
}

set<int> dfs(Node &node){
    set<int> ret;
    if(node.op==integer){
        ret.insert(node.num);
        return ret;
    }
    int n = (node.children.size()+1)/2;
    vector<tuple<int,Operator,set<int>>> V,VV;
    for(int i=0;i<n;i++){
        if(!i) VV.emplace_back(i,integer,dfs(node.children[2*i]));
        else VV.emplace_back(i,node.children[2*i-1].op, dfs(node.children[2*i]));
    }
    /*
    for(auto v:VV){
        int i;
        Operator op;
        tie(i,op,ignore)=v;
        cout<<i<<' ';
        switch(op){
            case add:
                cout<<"add ";
                break;
            case sub:
                cout<<"sub ";
                break;
            case mul:
                cout<<"mul ";
                break;
            case divi:
                cout<<"divi ";
                break;
            case fac:
                cout<<"fac ";
                break;
            case integer:
                cout<<"integer ";
        }
    }
    cout<<endl;
    */
    // return ret;
    vector<int> I(n-1);
    for(int i=0;i<n-1;i++) I[i]=i+1;

    do{
        V=VV;
        for(int i=0;i<I.size();i++){
            for(int v=0;v<V.size();v++){
                if(get<0>(V[v])==I[i]){
                    set<int> &S1=get<2>(V[v-1]);
                    set<int> &S2=get<2>(V[v]);
                    set<int> S3;
                    Operator op = get<1>(V[v]);
                    for(auto s1:S1){
                        for(auto s2:S2){
                            switch(op){
                                case add:
                                    S3.insert(s1+s2);
                                    break;
                                case sub:
                                    S3.insert(s1-s2);
                                    break;
                                case mul:
                                    S3.insert(s1*s2);
                                    break;
                                case divi:
                                    if(s2) S3.insert(s1/s2);
                                    break;
                                default:
                                    cout<<"!"<<endl;
                                    exit(1);
                            }
                        }
                    }
                    S1.clear();
                    for(auto s3:S3) S1.insert(s3);
                    V.erase(V.begin()+v);
                    break;
                }
            }
        }
        for(auto s:get<2>(V.front())) ret.insert(s);
    }while(next_permutation(I.begin(),I.end()));

    return ret;
}

void dfs2(Node &node, int depth = 0){
    cout<<string(depth,' ')<<"num: "<<node.num<<" op: ";
    switch(node.op){
        case add:
            cout<<"add"<<endl;
            break;
        case sub:
            cout<<"sub"<<endl;
            break;
        case mul:
            cout<<"mul"<<endl;
            break;
        case divi:
            cout<<"divi"<<endl;
            break;
        case fac:
            cout<<"fac"<<endl;
            break;
        case integer:
            cout<<"integer"<<endl;
    }
    for(auto &c:node.children) dfs2(c,depth+1);
}

int main(){
    while(true){
        string S;
        getline(cin, S);
        if(S[0]=='#') break;
        // cout<<S<<endl;
        State begin = S.begin();
        Node root = expr(begin);
        //dfs2(root);
        set<int> ans = dfs(root);
        cout<<ans.size()<<endl;
        // for(auto a:ans) cout<<a<<' ';
        // cout<<endl;
    }

    return 0;
}

Compiler output:
a.cc: In function 'std::set<int> dfs(Node&)':
a.cc:148:12: error: 'next_permutation' was not declared in this scope
  148 |     }while(next_permutation(I.begin(),I.end()));
      |            ^~~~~~~~~~~~~~~~