#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>

typedef int Value;

/* sp points to the next free stack entry */
Value stack[4096];
Value *sp = stack;

bool push(Value thing) {
  if (sp > stack + sizeof(stack) / sizeof(Value)) {
    printf("[Stack overflow] ");
    return false;
  }
  *(sp++) = thing;
  return true;
}

bool pop(Value *thing) {
  if (sp <= stack) {
    printf("[Stack underflow] ");
    return false;
  }
  *thing = *(--sp);
  return true;
}

bool word_print(void) { Value thing;
  return pop(&thing) && printf("%i ", thing); }
bool word_add(void) { Value a,b;
  return pop(&b) && pop(&a) && push(a + b); }
bool word_sub(void) { Value a,b;
  return pop(&b) && pop(&a) && push(a - b); }
bool word_mul(void) { Value a,b;
  return pop(&b) && pop(&a) && push(a * b); }
bool word_div(void) { Value a,b;
  return pop(&b) && pop(&a) && push(a / b); }

struct {char *name; bool(*func)(void);} words[] = {
  {".", word_print},
  {"+", word_add},
  {"-", word_sub},
  {"*", word_mul},
  {"/", word_div},
  {NULL, NULL},
};

bool evaluate(const char *token) {
  char *endptr;
  long intval = strtol(token, &endptr, 10);
  int i;
  /*printf("<%s>", token);*/
  
  if (*endptr == '\0') return push(intval);
  
  for (i = 0; words[i].name; i++)
    if (!strcmp(words[i].name, token)) {
      return words[i].func();
      break;
    }
  
  printf("[Unknown word: %s] ", token);
  return false;
}

int main(void) {
  char token[1024];
  size_t tlen = 0;
  bool run = true;
  
  while (run) {
    int ch = getchar();
    if (isspace(ch) || ch == EOF) {
      if (tlen) {
        token[tlen] = '\0';
        tlen = 0;
        
        if (!evaluate(token)) {
          printf("[Abort]");
          while (!(ch == '\n' || ch == EOF)) ch = getchar();
        }
      }
      if (ch == EOF)       run = false;
      else if (ch == '\n') printf("\n");
    } else {
      if (tlen >= sizeof(token)) { printf("[Token overflow] "); tlen = 0; }
      token[tlen++] = ch;
    }
  }
  return 0;
}

