Problem p00861 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

map<string, ll> sz;
map<pair<string, ll>, ll> val;
string in[1000], s;
char buf[90];
int p;

ll expr() {
  if (isdigit(s[p])) {
    ll res = 0;
    while (isdigit(s[p]))
      res *= 10, res += s[p++] - '0';
    return res;
  }
  string name;
  while (isalpha(s[p]))
    name += s[p++];
  p++;
  ll d = expr();
  p++;
  if (d < 0 || d >= sz[name] || !val.count(mp(name, d)))
    throw "error";
  return val[mp(name, d)];
}

int main() {
  while (cin >> in[0], in[0] != ".") {
    int n = 1;
    while (cin >> in[n], in[n] != ".")
      n++;
    sz.clear();
    val.clear();
    rep(i, n) {
      try {
        int pos = in[i].find("=");
        ll d, d2;
        if (pos == in[i].npos) {
          rep(j, in[i].size()) if (in[i][j] == '[' || in[i][j] == ']')
              in[i][j] = ' ';
          sscanf(in[i].c_str(), "%s%lld", buf, &d);
          sz[buf] = d;
        } else {
          s = in[i].substr(pos + 1);
          in[i] = in[i].substr(0, pos);
          p = 0;
          d = expr();
          pos = in[i].find("[");
          string name = in[i].substr(0, pos);
          s = in[i].substr(pos + 1, in[i].rfind("]") - pos - 1);
          p = 0;
          d2 = expr();
          if (d2 < 0 || d2 >= sz[name])
            throw "error";
          val[mp(name, d2)] = d;
        }
      } catch (const char *e) {
        cout << i + 1 << endl;
        goto END;
      }
    }
    cout << 0 << endl;
  END:;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(c) (c).begin(), (c).end()
#define mp make_pair
#define pb push_back
#define each(i, c)                                                             \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
const int inf = (int)1e9;
const double INF = 1e12, EPS = 1e-9;

map<string, ll> sz;
map<pair<string, ll>, ll> val;
string in[1001], s;
char buf[90];
int p;

ll expr() {
  if (isdigit(s[p])) {
    ll res = 0;
    while (isdigit(s[p]))
      res *= 10, res += s[p++] - '0';
    return res;
  }
  string name;
  while (isalpha(s[p]))
    name += s[p++];
  p++;
  ll d = expr();
  p++;
  if (d < 0 || d >= sz[name] || !val.count(mp(name, d)))
    throw "error";
  return val[mp(name, d)];
}

int main() {
  while (cin >> in[0], in[0] != ".") {
    int n = 1;
    while (cin >> in[n], in[n] != ".")
      n++;
    sz.clear();
    val.clear();
    rep(i, n) {
      try {
        int pos = in[i].find("=");
        ll d, d2;
        if (pos == in[i].npos) {
          rep(j, in[i].size()) if (in[i][j] == '[' || in[i][j] == ']')
              in[i][j] = ' ';
          sscanf(in[i].c_str(), "%s%lld", buf, &d);
          sz[buf] = d;
        } else {
          s = in[i].substr(pos + 1);
          in[i] = in[i].substr(0, pos);
          p = 0;
          d = expr();
          pos = in[i].find("[");
          string name = in[i].substr(0, pos);
          s = in[i].substr(pos + 1, in[i].rfind("]") - pos - 1);
          p = 0;
          d2 = expr();
          if (d2 < 0 || d2 >= sz[name])
            throw "error";
          val[mp(name, d2)] = d;
        }
      } catch (const char *e) {
        cout << i + 1 << endl;
        goto END;
      }
    }
    cout << 0 << endl;
  END:;
  }
  return 0;
}