Language: C++

#include <iostream>
#include <cstdio>
#include <set>
#include <cassert>

using namespace std;

string str;

int P,Q,R;

int Minus[] = {2, 1, 0};

int Prod[][3] = {
  {0, 0, 0},
  {0, 1, 1},
  {0, 1, 2}};
int Sum[][3] = {
  {0, 1, 2},
  {1, 1, 2},
  {2, 2, 2}};

typedef pair<int, int> Res;

Res formula(int i) {
  if (str[i] == '-') {
    Res res = formula(i + 1);
    return Res(Minus[res.first], res.second);
  } else if (str[i] == '(') {
    Res lhs = formula(i + 1);
    i = lhs.second;
    if (str[i] == '+') {
      Res rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return Res(Sum[lhs.first][rhs.first], rhs.second + 1);
    } else if (str[i] == '*') {
      Res rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return Res(Prod[lhs.first][rhs.first], rhs.second + 1);
    }
    assert(true);
  } else if (str[i] == 'P') {
    return Res(P, i + 1);
  } else if (str[i] == 'Q') {
    return Res(Q, i + 1);
  } else if (str[i] == 'R') {
    return Res(R, i + 1);
  } else if (str[i] == '0') {
    return Res(0, i + 1);
  } else if (str[i] == '1') {
    return Res(1, i + 1);
  } else if (str[i] == '2') {
    return Res(2, i + 1);
  }
  assert(false);
}

void solve() {
  int cnt;
  for (P = 0; P < 3; P++) {
    for (Q = 0; Q < 3; Q++) {
      for (R = 0; R < 3; R++) {
        Res res =formula(0);
        //        assert(res.second == str.size());
        if (res.first == 2) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    getline(cin, str);
    if (str == ".") {
      return 0;
    }
    solve();
  }
}
#include <iostream>
#include <cstdio>
#include <set>
#include <cassert>

using namespace std;

string str;

int P,Q,R;

int Minus[] = {2, 1, 0};

int Prod[][3] = {
  {0, 0, 0},
  {0, 1, 1},
  {0, 1, 2}};
int Sum[][3] = {
  {0, 1, 2},
  {1, 1, 2},
  {2, 2, 2}};

typedef pair<int, int> Res;

Res formula(int i) {
  if (str[i] == '-') {
    Res res = formula(i + 1);
    return Res(Minus[res.first], res.second);
  } else if (str[i] == '(') {
    Res lhs = formula(i + 1);
    i = lhs.second;
    if (str[i] == '+') {
      Res rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return Res(Sum[lhs.first][rhs.first], rhs.second + 1);
    } else if (str[i] == '*') {
      Res rhs = formula(i + 1);
      assert(str[rhs.second] == ')');
      return Res(Prod[lhs.first][rhs.first], rhs.second + 1);
    }
    assert(false);
  } else if (str[i] == 'P') {
    return Res(P, i + 1);
  } else if (str[i] == 'Q') {
    return Res(Q, i + 1);
  } else if (str[i] == 'R') {
    return Res(R, i + 1);
  } else if (str[i] == '0') {
    return Res(0, i + 1);
  } else if (str[i] == '1') {
    return Res(1, i + 1);
  } else if (str[i] == '2') {
    return Res(2, i + 1);
  }
  assert(false);
}

void solve() {
  int cnt;
  for (P = 0; P < 3; P++) {
    for (Q = 0; Q < 3; Q++) {
      for (R = 0; R < 3; R++) {
        Res res =formula(0);
        //        assert(res.second == str.size());
        if (res.first == 2) {
          cnt++;
        }
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  while (true) {
    assert(fales);
    getline(cin, str);
    if (str == ".") {
      return 0;
    }
    solve();
  }
}

Compiler output:
a.cc:90:8: error: redefinition of 'std::string str'
   90 | string str;
      |        ^~~
a.cc:8:8: note: 'std::string str' previously declared here
    8 | string str;
      |        ^~~
a.cc:92:5: error: redefinition of 'int P'
   92 | int P,Q,R;
      |     ^
a.cc:10:5: note: 'int P' previously declared here
   10 | int P,Q,R;
      |     ^
a.cc:92:7: error: redefinition of 'int Q'
   92 | int P,Q,R;
      |       ^
a.cc:10:7: note: 'int Q' previously declared here
   10 | int P,Q,R;
      |       ^
a.cc:92:9: error: redefinition of 'int R'
   92 | int P,Q,R;
      |         ^
a.cc:10:9: note: 'int R' previously declared here
   10 | int P,Q,R;
      |         ^
a.cc:94:5: error: redefinition of 'int Minus []'
   94 | int Minus[] = {2, 1, 0};
      |     ^~~~~
a.cc:12:5: note: 'int Minus [3]' previously defined here
   12 | int Minus[] = {2, 1, 0};
      |     ^~~~~
a.cc:96:5: error: redefinition of 'int Prod [][3]'
   96 | int Prod[][3] = {
      |     ^~~~
a.cc:14:5: note: 'int Prod [3][3]' previously defined here
   14 | int Prod[][3] = {
      |     ^~~~
a.cc:100:5: error: redefinition of 'int Sum [][3]'
  100 | int Sum[][3] = {
      |     ^~~
a.cc:18:5: note: 'int Sum [3][3]' previously defined here
   18 | int Sum[][3] = {
      |     ^~~
a.cc:107:5: error: redefinition of 'Res formula(int)'
  107 | Res formula(int i) {
      |     ^~~~~~~
a.cc:25:5: note: 'Res formula(int)' previously defined here
   25 | Res formula(int i) {
      |     ^~~~~~~
a.cc:140:6: error: redefinition of 'void solve()'
  140 | void solve() {
      |      ^~~~~
a.cc:58:6: note: 'void solve()' previously defined here
   58 | void solve() {
      |      ^~~~~
a.cc:156:5: error: redefinition of 'int main()'
  156 | int main() {
      |     ^~~~
a.cc:74:5: note: 'int main()' previously defined here
   74 | int main() {
      |     ^~~~
In file included from /usr/include/c++/14/cassert:44,
                 from a.cc:86:
a.cc: In function 'int main()':
a.cc:158:12: error: 'fales' was not declared in this scope
  158 |     assert(fales);
      |            ^~~~~