Problem p01682 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

// #define LOG(...) fprintf(stderr,__VA_ARGS__)
#define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ull term(string &e, int &idx);
ull expr(string &e, int &idx);

ull number(string &e, int &idx) {
  ull number = 0;
  for (; idx < e.size(); idx++) {
    if (!isdigit(e[idx])) {
      break;
    }
    number *= 10;
    number += e[idx] - '0';
  }
  LOG("--> number %d\\n", number);
  return number;
}

ull term(string &e, int &idx) {
  switch (e[idx]) {
  case '(': {
    idx++; // (
    ull n = expr(e, idx);
    ull val = (n * n) % 1000000007;
    LOG("--> pow %d = %d\\n", n, val);
    idx++; // )
    return val;
  }
  default:
    return number(e, idx);
  }
  assert(0);
}

ull expr(string &e, int &idx) {
  ull val = term(e, idx);
  for (; idx < e.size(); idx++) {
    switch (e[idx]) {
    case '>': {
      idx++;
      ull n = term(e, idx);
      LOG("--> shift %d %d = ", val, n);
      for (int i = n; i > 0 && val != 0; i--) {
        val >>= 1;
      }
      LOG("%d\\n", val);
      idx--;
      break;
    }
    case ')':
      return val;
    }
  }
  return val;
}

int main() {
  string s;
  while (getline(cin, s), s != "#") {
    stringstream ss;
    REP(i, s.size()) {
      if (s[i] != ' ') {
        ss << s[i];
      }
    }
    s = ss.str();
    stringstream ss2;
    REP(i, s.size()) {
      switch (s[i]) {
      case 'S':
        i++;
        ss2 << '(';
        break;
      case '>':
        if (i + 2 < s.size() && (isdigit(s[i + 2]) || s[i + 2] == 'S')) {
          ss2 << '>';
          i += 1;
        } else {
          ss2 << ')';
        }
        break;
      case ' ':
        break;
      default:
        ss2 << s[i];
      }
    }

    string e = ss2.str();
    LOG("%s\\n", e.c_str());

    int idx = 0;
    cout << expr(e, idx) << endl;
  }
}

Error: 0
((12>2))
--> number 12
--> number 2
--> shift 12 2 = 3
--> pow 3 = 9
--> pow 9 = 81
123>1>1
--> number 123
--> number 1
--> shift 123 1 = 61
--> number 1
--> shift 61 1 = 30
1000000000>129
--> number 1000000000
--> number 129
--> shift 1000000000 129 = 0
(((((2)))))
--> number 2
--> pow 2 = 4
--> pow 4 = 16
--> pow 16 = 256
--> pow 256 = 65536
--> pow 65536 = 294967268
(((2013)>11)>10)
--> number 2013
--> pow 2013 = 4052169
--> number 11
--> shift 4052169 11 = 1978
--> pow 1978 = 3912484
--> number 10
--> shift 3912484 10 = 3820
--> pow 3820 = 14592400


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

ull term(string &e, int &idx);
ull expr(string &e, int &idx);

ull number(string &e, int &idx) {
  ull number = 0;
  for (; idx < e.size(); idx++) {
    if (!isdigit(e[idx])) {
      break;
    }
    number *= 10;
    number += e[idx] - '0';
  }
  LOG("--> number %d\\n", number);
  return number;
}

ull term(string &e, int &idx) {
  switch (e[idx]) {
  case '(': {
    idx++; // (
    ull n = expr(e, idx);
    ull val = (n * n) % 1000000007;
    LOG("--> pow %d = %d\\n", n, val);
    idx++; // )
    return val;
  }
  default:
    return number(e, idx);
  }
  assert(0);
}

ull expr(string &e, int &idx) {
  ull val = term(e, idx);
  for (; idx < e.size(); idx++) {
    switch (e[idx]) {
    case '>': {
      idx++;
      ull n = term(e, idx);
      LOG("--> shift %d %d = ", val, n);
      for (int i = n; i > 0; i--) {
        val >>= 1;
      }
      LOG("%d\\n", val);
      idx--;
      break;
    }
    case ')':
      return val;
    }
  }
  return val;
}

int main() {
  string s;
  while (getline(cin, s), s != "#") {
    stringstream ss;
    REP(i, s.size()) {
      if (s[i] != ' ') {
        ss << s[i];
      }
    }
    s = ss.str();
    stringstream ss2;
    REP(i, s.size()) {
      switch (s[i]) {
      case 'S':
        i++;
        ss2 << '(';
        break;
      case '>':
        if (i + 2 < s.size() && (isdigit(s[i + 2]) || s[i + 2] == 'S')) {
          ss2 << '>';
          i += 1;
        } else {
          ss2 << ')';
        }
        break;
      case ' ':
        break;
      default:
        ss2 << s[i];
      }
    }

    string e = ss2.str();
    LOG("%s\\n", e.c_str());

    int idx = 0;
    cout << expr(e, idx) << endl;
  }
}