Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You are to write an interpreter in C for the BrainFuck programming language. When run, your interpreter will check its command line arguments for the

You are to write an interpreter in C for the BrainFuck programming language. When run, your interpreter will check its command line arguments for the following (these arguments may be in any order): -v Run in verbose mode, optional. -r Run the program repeatedly, optional. filename The file which contains the Brainfuck source code, required. Brainfuck programs typically end in .b or .bf If the -v flag was used, the CODE and DATA (the first 10 cells) will be displayed before every command in the BrainFuck program. Identify the locations of the Code Pointer and the Data Pointer. See the examples below for a sample. If the -r flag was used, the interpreter will run the BrainFuck program repeatedly until killed (CtrlC). Otherwise, it will run it once and exit. When the Brainfuck program reads an integer, prompt the user with a ?. When the Brainfuck program writes an integer, write the integer followed by a space (no newline).

To understand the language you must first understand the programming model. The model is made up of a sequence of characters that make up the CODE, and an unlimited sequence of integers that make up the DATA. A Code Pointer starts at 0, the location of the first command. A Data Pointer also starts at 0, the location of the first integer (also known as the current data element). All DATA integers are initialized to 0 with each run. Our version of this language is made up of the following eight, single-letter commands: ; Set the value of the current data element to an integer value supplied by the user. You should prompt the user with a ?. : Print the current data element. < Move the Data Pointer to the previous data element. Print an error and stop execution if the Data Pointer goes negative. > Move the data pointer to the next data element. There is no largest element. [ If the integer at the current data pointer is zero, move the Code Pointer to the right to the matching ]. Otherwise, do nothing. Print an error and stop execution if there is no matching brace. ] Move the Code Pointer left to the matching [. Print an error and stop execution if there is no matching brace. + Increment the current data element. - Decrement the current data element. Data elements can go negative. Spaces, tabs and newlines are ignored. A program is executed as follows: 0. Set the Code Pointer and Data Pointer to 0. Initialize all the data elements to 0. 1. Get the command at the current Code Pointer. 2. Execute the command. 3. Increment the Code Pointer. 4. If there is another command at the Code Pointer, go to step 1. 5. Otherwise, exit.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Factors Affecting Conflict

Answered: 1 week ago

Question

Describe the factors that lead to productive conflict

Answered: 1 week ago

Question

Understanding Conflict Conflict Triggers

Answered: 1 week ago