Here`s

CS352 Assignment #6 One Programming Problem (revised 4/16/15 9:27pm) Due Date: ​
Wed, April 29 at 7:00 PM Turnin: Submit all files to the ​
cs352s15assg6 ​
turnin directory. Recall that this command looks like turnin cs352s15assg6 add.c list.c run.c input.c You may check what files have been submitted by typing “turnin ­ls cs352s15assg6”. Your program will be put through a series of test cases and compared to ours using diff. This means that things like spelling, punctuation and whitespace are very important if you would like to pass all the test cases without needing to appeal. Additionally, ​
your filenames will need to match exactly​
in order for the script to pick it up. If you have any questions about the output of your scripts or the format of the filenames, please stop by our office hours, post on Piazza or email us. Remember, no late work will be accepted. The following rules apply to all programs: 1. Do not use anything covered after chapter 26 in the King textbook. 2. Only C89 will be accepted. Period. 3. A correct answer is composed of both correct output and exit status. 4. Issues with whitespace or debugging statements will not longer be eligible for an appeal.​
Please make sure you code works as expected on lectura before submitting it. 5. Programs that fail to compile will receive a 0. ​
We will be compiling this program like this: gcc ­Werror *.c ­Werror means that all warnings are now considered errors. Therefore, you should make sure your program runs on lectura with this argument before submission. B2 Interpreter (b2) Write a program that will function as an interpreter for a BASIC­like programming language, B2. Here’s an example B2 program: 10 LET x = 0 + 1 20 PRINT x 30 LET x = x + 1 40 PRINT x 50 END This program would print 1 2 Specifics of the assignment: 1. The B2 interpreter will read one line at a time, parse that line, and do the appropriate action. 2. The B2 interpreter will exit with status 0 when it reaches the end of input. 3. B2 has variables that hold integer values. 4. B2 variables names are one or more letters. Variable names are case­sensitive. X and x are different variables. Variable names can include PRINT, IF, and other B2 commands. 5. B2 also has integer literal values. Integer literals represent non­negative integers that will fit in a C int type. 6. B2 “terms” below refer to either an integer literal or a variable. 7. All statements can contain arbitrary amounts of whitespace. 8. All components of a statement will be separated by whitespace. For instance, “1+2” is not legal because the + is not separated from the 1 and 2. 9. There are 5 kinds of lines that the interpreter may read. All program lines begin with a line number, although the commands LIST and RUN do not have line numbers: a. linenum PRINT variable PRINT prints the value of the variable followed by a newline. Then, interpretation resumes at the next higher numbered statement. b. linenum IF term GOTO target IF causes conditional control­flow transfer. If “term” represents a non­zero value, then interpretation proceeds to the line numbered “target”. If term represents a zero value, the interpretation proceeds to the next higher numbered line. c. linenum LET var = term op term LET assigns a value to a variable as computed by the given terms and operator. i.
The legal operators are <, +, ­, *, and /. They all have C semantics. d. linenum END END causes the interpretation to stop, and then interpreter will then continue accepting more input (e.g., more program statements or commands). e. Lines that include only whitespace are ignored. 10. The program may include syntax errors, which must be reported per the directions below. Syntax errors include incorrect operators, incomplete statements, etc. 11. The interpreter must also accept the command “RUN” (without a line number). a. It causes the interpreter to execute the stored program. b. Execution begins at the lowest­numbered line number statement. c. The interpretation will terminate when it hits an END statement. At that point, the interpreter will accept more input. 12. The interpreter must also accept the command, “LIST” (without a line number). a. LIST causes the interpreter to print all the lines of the program, one per line. b. All components of each line must be separated by exactly one space. No extra space on the beginning or end of the line. 13. Notes on B2 interpretation­­­what happens during the RUN command: a. Accessing the value of an uninitialized variable will produce the value 0. b. Dividing by zero will cause the interpreter to print, “DIVIDE BY ZERO”, and then exit interpretation­­­the interpreter will then wait for another line of input. c. Trying to GOTO a non­existent target line number will cause the interpreter to print, “UNKNOWN LINE: linenum”, and then exit interpretation. d. After executing the last line of the program, the interpreter exits interpretation. Ie., if there is no END statement, then interpretation will stop when it “falls off” the end of the program. e. Errors like dividing by zero and trying to get a non­existent line number are found during interpretation when the offending statement is executed. They are not found when they are initially read in. f. An empty program is perfectly legal. It just doesn’t do anything. Similarly, programs may not have any output. g. Each new execution begins with no variables defined or initialized. 14. Lines do NOT need to be typed in order, but they will be executed in numeric order (except for IF statements that may make execution go elsewhere.) 15. Providing a program statement with a line number that already exists will cause the new statement to replace the old statement. 16. Any line input after a legal statement (on the same line) is ignored. 17. All characters after the 72nd on any given line may be ignored. It is important to understand how interpreters work: they just run in a big loop waiting for input and then doing whatever is appropriate for the just­read input. All input for this program is done one line at a time. 1. For each line that contains a syntax error, that syntax error is reported and then the offending line is ignored. 2. For each syntactically­correct line of program code, that line is silently added to the program. 3. The RUN command causes the interpreter to run until it either hits an END statement, runs off the end of the program, or hits an error condition (which terminates execution). In all three cases, after the interpretation terminates, the program goes back to reading lines of input. NOTE that the just­executed program continues to exist after being run­­­any new program statements will be added to or replace existing statements. 4. The LIST command causes the interpreter to list the program statements in order, and then go back to reading more lines of input. HINT: Get everything but RUN working before attempting to get RUN working. This means being able to handle arbitrary program statement input and LIST. Source Code You will turn in exactly four C files: 1. input.c The file input.c will contain the code necessary to read B2 program statements and commands. It will not interpret those statements. They are simply returned to the main driver routine. 2. run.c The file run.c will contain the code necessary to interpret B2 programs. 3. list.c The file list.c will contain the code necessary to list a B2 program. 4. add.c The file add.c will contain the code necessary to add a newly­parsed statement (Stmt *) to the linked list of statements. Those statements are kept in order by line number. The file main.c contains your main function, and it will be provided to you. Do NOT edit it. It will orchestrate everything, calling routines found in input.c, run.c, add.c and list.c as appropriate. The shared header file, interpreter.h, will be supplied for you. Do NOT edit it and do not create any other header files. In addition to data type definitions, interpreter.h includes prototypes for the functions you must write. You MUST use the types defined in interpreter.h. Notes on the implementation: 1. The Stmt type defines a linked list of statements. You should keep that linked list in order, sorted by line number. 2. The Value type defines a linked list of string/int values. You must use this to keep track of the values of B2 variables. The header file. (NOTE: Check for updates on Piazza.) enum stmt { PRINT = 1, IF, LET, END, RUN, LIST }; enum term { VAR = 1, INT }; typedef struct termNode { enum term type; union { char *var; int intlit; } u; } Term; typedef struct stmtNode { int line; enum stmt type; struct stmtNode *next; union { struct { char *var; } print; struct { Term *term; int target; } ifs; struct { char *var; Term *left; char op; Term *right; } let; } u; } Stmt; typedef struct value { char *name; int value; struct value *next; } Value; void run(Stmt *); void list(Stmt *); Stmt * input(); Stmt *add(Stmt *head, Stmt *p); The main.c file: (NOTE: Check for updates on Piazza.) #include <assert.h> #include <stdlib.h> #include "interpreter.h" int main(int argc, char *argv[]) { Stmt *p; Stmt *head; head = NULL; while ((p = input())) { switch (p­>type) { default: assert(0); case RUN: free(p); run(head); break; case LIST: free(p); list(head); break; case IF: case END: case LET: case PRINT: head = add(head, p); break; } } }