#877. Brainfuck
Brainfuck
Background
Brainfuck is a minimal programming language, which contains only 8 kinds of characters.
In the memory model of Brainfuck, there is an infinitely large array and a data pointer p.
The table below illustrates the semantics of each brainfuck instruction.
Now you are given a brainfuck code, you need to translate it into C++.
It’s quite easy to finish this task, so the optimizations below are required for you to do
- You need to merge consecutive and and instructions into one C++ statement. For example,
should be simplified to ;.
- For and command, should be ignored if there are no statements between and . For
example, should be simplified to nothing.
- Any characters other than the eight characters in the table should be ignored.
- You need an indention of two spaces for do-while code block.
- If and are mismatched, just output Error!.
Input
One string representing the code in Brainfuck.
It is guaranteed the length of the input code does not exceed
Output
Output the translated Brainfuck code in C++.
Samples
++<<>[]--,
*p += 2;
p -= 1;
*p -= 2;
*p = getchar();
[[+]
Error!
[++[+>+]+]
if (*p) do {
*p += 2;
if (*p) do {
*p += 1;
p += 1;
*p += 1;
} while (*p);
*p += 1;
} while (*p);