#include <stdio.h>
#include <string.h>
#include <ctype.h>

char stack[100][10];
int top = -1;
int index = 0;
char input[100];

void removeSpaces(char *str)
{
    int i = 0, j = 0;

    while (str[i] != '\0')
    {
        if (!isspace(str[i]))
        {
            str[j] = str[i];
            j++;
        }
        i++;
    }
    str[j] = '\0';
}

void push(const char *s)
{
    strcpy(stack[++top], s);
}

void pop()
{
    top--;
}

void printStack()
{
    for (int i = 0; i <= top; i++)
        printf("%s", stack[i]);
    printf("\n");
}

int reduce()
{
    if (top >= 2 &&
        strcmp(stack[top - 2], "E") == 0 &&
        strcmp(stack[top - 1], "+") == 0 &&
        strcmp(stack[top], "E") == 0)
    {
        pop();
        pop();
        pop();
        push("E");
        return 1;
    }

    if (top >= 2 &&
        strcmp(stack[top - 2], "E") == 0 &&
        strcmp(stack[top - 1], "*") == 0 &&
        strcmp(stack[top], "E") == 0)
    {
        pop();
        pop();
        pop();
        push("E");
        return 1;
    }

    if (top >= 2 &&
        strcmp(stack[top - 2], "(") == 0 &&
        strcmp(stack[top - 1], "E") == 0 &&
        strcmp(stack[top], ")") == 0)
    {
        pop();
        pop();
        pop();
        push("E");
        return 1;
    }

    if (top != -1 && stack[top][0] >= 'a' && stack[top][0] <= 'z')
    {
        pop();
        push("E");
        return 1;
    }

    return 0;
}

int main()
{
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = '\0';
    removeSpaces(input);

    while (input[index])
    {
        char temp[2] = {input[index], '\0'};
        push(temp);
        index++;

        printf("Shift: ");
        printStack();

        while (reduce())
        {
            printf("Reduce: ");
            printStack();
        }
    }

    if (top == 0 && strcmp(stack[0], "E") == 0)
        printf("String Accepted\n");
    else
        printf("String Rejected\n");

    return 0;
}