#include #include #include #include using namespace std; #define NUM 1 #define POP 2 #define INV 3 #define DUP 4 #define SWP 5 #define ADD 6 #define SUB 7 #define MUL 8 #define DIV 9 #define MOD 10 char str[8]; int cmd[100100]; int param[100100]; int n, m; int error = 0; stack < int > v; inline int pop( void ) { // printf("pop %d\n", v.size()); if ( v.empty() ) { error = 2; return 0; } int x = v.top(); v.pop(); return x; } inline void push( int x ) { // printf("push %d\n", x); v.push( x ); } inline void pushll( long long x ) { if ( x < -1000000000 || x > 1000000000 ) error = 1; push( (int)x ); } int evaluate( int x ) { while ( !v.empty() ) v.pop(); push( x ); for ( int i = 0; i < m; ++i ) { // printf("i=%d m=%d cmd=%d num=%d\n", i, m, cmd[i], param[i]); switch ( cmd[i] ) { case NUM: push( param[i] ); break; case POP: pop(); break; case INV: push( -pop() ); break; case DUP: { int x = pop(); push( x ); push( x ); break; } case SWP: { int a = pop(); int b = pop(); push( a ); push( b ); break; } case ADD: pushll( pop() + pop() ); break; case SUB: { int a = pop(); int b = pop(); pushll( b - a ); break; } case MUL: pushll( (long long)pop() * pop() ); break; case DIV: { int a = pop(); int b = pop(); if ( a == 0 ) { error = 1; return 0; } push( b / a ); break; } case MOD: { int a = pop(); int b = pop(); if ( a == 0 ) { error = 1; return 0; } push( b % a ); break; } }; if ( error ) return 0; } if ( v.size() != 1 ) error = 2; return pop(); } int main(void) { for ( ;; ) { for ( m = 0; ; ++m ) { scanf("%s", str); if ( strcmp( str, "QUIT" ) == 0 ) return 0; if ( strcmp( str, "NUM" ) == 0 ) { scanf("%d", param + m); cmd[m] = NUM; } if ( strcmp( str, "END" ) == 0 ) break; #define T(c) if ( strcmp( str, #c ) == 0 ) cmd[m] = c; T( POP ); T( INV ); T( DUP ); T( SWP ); T( ADD ); T( SUB ); T( MUL ); T( DIV ); T( MOD ); } scanf("%d", &n); for ( int i = 0; i < n; ++i ) { int x; scanf("%d", &x); error = 0; int y = evaluate( x ); if ( error == 2 ) { for ( int k = 0; k < n; ++k ) printf("ERROR\n"); for ( int k = 1; k < n; ++k ) scanf("%d", &x); break; } if ( error ) printf("ERROR\n"); else printf("%d\n", y); } printf("\n"); } return 0; }