Brainfuck consists of only eight simple commands and an instruction pointer. While it is fully Turing-complete, it is not intended for practical use, but to challenge and amuse programmers.
BrainFuck consists of 8 character commands only which makes its use very challenging even for simple tasks –
- The > command increments the data pointer (to point to the next cell to the right).
- The < command decrements the data pointer (to point to the next cell to the left).
- The + command increments (increase by one) the byte at the data pointer.
- The – command decrements (decrease by one) the byte at the data pointer.
- The . command outputs the byte at the data pointer.
- The , command accept one byte of input, storing its value in the byte at the data pointer.
- [ – if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
- ] – if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
- (Alternatively, the ] command may instead be translated as an unconditional jump to the corresponding [ command, or vice versa; programs will behave the same but will run more slowly, due to unnecessary double searching.)
- [ and ] match as parentheses usually do: each [ matches exactly one ] and vice versa, the [ comes first, and there can be no unmatched [ or ] between the two.
Since BrainFuck consists of only these 8 commands, building an interpreter for BrainFuck is quite simple. In this article, we will build a simple program which takes a BrainFuck code as input and produces the desired output. We will simply accept the BrainFuck code as a String and produce the output by parsing the String and checking every character for its actual functionality. The memory is represented by an array of byte type simulating memory of max 65535 bits from 0 to 65534(65535 is the highest number which can be represented by an unsigned 16-bit binary number). The variable ptr refers to the current index of the memory array.
In this article, we won’t be discussing the details of writing programs in BrainFuck.For more details on writing BrainFuck programs, refer to the following links:
Examples:
Input :
Output : Hello World!
Input :
Output : GEEKS FOR GEEKS
Java Implementation of the BrainFuck Interpreter-
Java
import java.util.*;
class BrainFuck
{
private static Scanner ob = new Scanner(System.in);
private static int ptr;
private static int length = 65535 ;
private static byte memory[] = new byte [length];
private static void interpret(String s)
{
int c = 0 ;
for ( int i = 0 ; i < s.length(); i++)
{
if (s.charAt(i) == '>' )
{
if (ptr == length - 1 )
ptr = 0 ;
else
ptr ++;
}
else if (s.charAt(i) == '<' )
{
if (ptr == 0 )
ptr = length - 1 ;
else
ptr --;
}
else if (s.charAt(i) == '+' )
memory[ptr] ++;
else if (s.charAt(i) == '-' )
memory[ptr] --;
else if (s.charAt(i) == '.' )
System.out.print(( char )(memory[ptr]));
else if (s.charAt(i) == ',' )
memory[ptr] = ( byte )(ob.next().charAt( 0 ));
else if (s.charAt(i) == '[' )
{
if (memory[ptr] == 0 )
{
i++;
while (c > 0 || s.charAt(i) != ']' )
{
if (s.charAt(i) == '[' )
c++;
else if (s.charAt(i) == ']' )
c--;
i ++;
}
}
}
else if (s.charAt(i) == ']' )
{
if (memory[ptr] != 0 )
{
i --;
while (c > 0 || s.charAt(i) != '[' )
{
if (s.charAt(i) == ']' )
c ++;
else if (s.charAt(i) == '[' )
c --;
i --;
}
}
}
}
}
public static void main(String args[])
{
System.out.println( "Enter the code:" );
String code = ob.nextLine();
System.out.println( "Output:" );
interpret(code);
}
}
|
Output 1:
Enter the code:
--[+++++++>-->+>+>+<<<->---.>--..>+.<<<.+>->>.+++[.<]
Output:
Hello World!
Output 2:
Enter the code:
++++++++++[>+++++++>++++++++>+++<+++.>++..<+.
Output:
GEEKS FOR GEEKS
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!