diff --git a/lab-5/README.md b/lab-5/README.md index b9e30d0..41ea8ad 100644 --- a/lab-5/README.md +++ b/lab-5/README.md @@ -5,24 +5,23 @@ See the [example lab for Python](https://gitlab.cs.wallawalla.edu/cptr354/langua In this lab, you will focus on basic operations that support your sample code. -* Arithmetic operations - * add - * subtract - * multiple - * divide -* Functions (but implemented directly in the language) - * Printing - * Square root -* Variable for numbers -* Variable for lists (or arrays) -* Branching statements -* Looping statements +- Arithmetic operations - Jenessy + - add + - subtract + - multiple + - divide +- Functions (but implemented directly in the language) + - Printing - Josh + - Square root - Jenessy +- Variable for numbers - Riley +- Variable for lists (or arrays) - Riley +- Branching statements - Josh +- Looping statements - Riley Notice you will find a new file `expr.c`. The file is used to implement the parser rule. Inside these functions, you will create the runtime code that is the interpreter. - ## Build Process The process is similar to prior labs. @@ -75,31 +74,30 @@ To remove the build files, use make clean. make clean ``` - ## Assignment Steps -1. Start by confirming these commands work for the sample code provided. +1. Start by confirming these commands work for the sample code provided. -1. Next copy over your `scanner.flex`, `parser.bison` and sample programs from lab 5. +1. Next copy over your `scanner.flex`, `parser.bison` and sample programs from lab 5. 1. Start working on your bison `parser.bison` and `expr.c` files to implement the interpreter part of your parser. - ## Grading Rubric The grading will be over the following categories. -Points Description ------------ ------------------------------------ -30 points Ability to interpret — Hello world -30 points Ability to interpret — Quadratic equation -30 points Ability to interpret — Arithmetic operations -30 points Ability to interpret — Integer Sort example -20 points Ability to interpret — Looping operations -20 points Ability to interpret — Branching operations -20 points Ability to interpret — Array variables -10 points Report formatting and readability +Points Description +--- + +30 points Ability to interpret — Hello world +30 points Ability to interpret — Quadratic equation +30 points Ability to interpret — Arithmetic operations +30 points Ability to interpret — Integer Sort example +20 points Ability to interpret — Looping operations +20 points Ability to interpret — Branching operations +20 points Ability to interpret — Array variables +10 points Report formatting and readability ## Turn In diff --git a/lab-5/samples/array_assignment.c b/lab-5/samples/array_assignment.c deleted file mode 100644 index cce2d38..0000000 --- a/lab-5/samples/array_assignment.c +++ /dev/null @@ -1 +0,0 @@ -a = [ 4, 8, 12 ]; diff --git a/lab-5/samples/array_read.c b/lab-5/samples/array_read.c deleted file mode 100644 index db30710..0000000 --- a/lab-5/samples/array_read.c +++ /dev/null @@ -1,2 +0,0 @@ -a = [ 3, 5 ]; -print a[0]; \ No newline at end of file diff --git a/lab-5/samples/array_write.c b/lab-5/samples/array_write.c deleted file mode 100644 index 4f81214..0000000 --- a/lab-5/samples/array_write.c +++ /dev/null @@ -1,4 +0,0 @@ -a = [ 3, 5, 7, 9, 11]; -print a[1]; -a[1] = 6; -print a[1]; diff --git a/lab-5/samples/assignment_statement.c b/lab-5/samples/assignment_statement.c deleted file mode 100644 index a5a2277..0000000 --- a/lab-5/samples/assignment_statement.c +++ /dev/null @@ -1 +0,0 @@ -a = 5; \ No newline at end of file diff --git a/lab-5/samples/hello-world.cbl b/lab-5/samples/hello-world.cbl new file mode 100644 index 0000000..737f19e --- /dev/null +++ b/lab-5/samples/hello-world.cbl @@ -0,0 +1,5 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. HELLO-WORLD. +PROCEDURE DIVISION. + DISPLAY 'Hello World!' +STOP RUN. diff --git a/lab-5/samples/multiple_statements.c b/lab-5/samples/multiple_statements.c deleted file mode 100644 index b31de4e..0000000 --- a/lab-5/samples/multiple_statements.c +++ /dev/null @@ -1,2 +0,0 @@ -print 5; -print 6; diff --git a/lab-5/samples/print_if_equal.c b/lab-5/samples/print_if_equal.c deleted file mode 100644 index e4a42e4..0000000 --- a/lab-5/samples/print_if_equal.c +++ /dev/null @@ -1,6 +0,0 @@ -if 3==3 then - print 3; -endif -if 3==4 then - print 4; -endif diff --git a/lab-5/samples/print_if_false.c b/lab-5/samples/print_if_false.c deleted file mode 100644 index 5340a03..0000000 --- a/lab-5/samples/print_if_false.c +++ /dev/null @@ -1,5 +0,0 @@ -if 0 then - print 5; - print 3; - print 1; -endif diff --git a/lab-5/samples/print_if_true.c b/lab-5/samples/print_if_true.c deleted file mode 100644 index 7848347..0000000 --- a/lab-5/samples/print_if_true.c +++ /dev/null @@ -1,5 +0,0 @@ -if 1 then - print 5; - print 3; - print 1; -endif diff --git a/lab-5/samples/print_statement.c b/lab-5/samples/print_statement.c deleted file mode 100644 index 1748ba7..0000000 --- a/lab-5/samples/print_statement.c +++ /dev/null @@ -1 +0,0 @@ -print 5; diff --git a/lab-5/samples/print_variable.c b/lab-5/samples/print_variable.c deleted file mode 100644 index 28aae29..0000000 --- a/lab-5/samples/print_variable.c +++ /dev/null @@ -1,2 +0,0 @@ -a = 4 + 4; -print a; diff --git a/lab-5/samples/program.c b/lab-5/samples/program.c deleted file mode 100644 index f1f084a..0000000 --- a/lab-5/samples/program.c +++ /dev/null @@ -1,2 +0,0 @@ -a=(101*20)+4; -print(a); diff --git a/lab-5/samples/quadratic-snippet.cbl b/lab-5/samples/quadratic-snippet.cbl new file mode 100644 index 0000000..ab76e40 --- /dev/null +++ b/lab-5/samples/quadratic-snippet.cbl @@ -0,0 +1,35 @@ + *> Code altered from https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation + *> Program finds the roots to a simple quadratic equation + + IDENTIFICATION DIVISION. + PROGRAM-ID. QuadraticSolver. + DATA DIVISION. + WORKING-STORAGE SECTION. + 77 a PIC S9(5)V9(5) COMP-3 VALUE 1. + 77 b PIC S9(5)V9(5) COMP-3 VALUE 5. + 77 c PIC S9(5)V9(5) COMP-3 VALUE 6. + 77 discriminant PIC S9(5)V9(5) COMP-3. + 77 root1 PIC S9(5)V9(5) COMP-3. + 77 root2 PIC S9(5)V9(5) COMP-3. + 77 square-root-discriminant PIC S9(5)V9(5) COMP-3. + + PROCEDURE DIVISION. *> program begins here + DISPLAY "EQUATION: (1x^2) + 5x + 6 = 0" + COMPUTE discriminant = (b ** 2) - (4 * a * c) + + IF discriminant > 0 + COMPUTE square-root-discriminant = FUNCTION SQRT(discriminant) + COMPUTE root1 = (-b + square-root-discriminant) / (2 * a) + COMPUTE root2 = (-b - square-root-discriminant) / (2 * a) + DISPLAY "The equation has two distinct real roots: " + DISPLAY "Root 1: " root1 + DISPLAY "Root 2: " root2 + + ELSE IF discriminant = 0 + COMPUTE root1 = -b / (2 * a) + DISPLAY "The equation has one real root: " + DISPLAY "Root: " root1 + ELSE + DISPLAY "The equation has no real roots." + + STOP RUN. diff --git a/lab-5/samples/sorting-snippet.cbl b/lab-5/samples/sorting-snippet.cbl new file mode 100644 index 0000000..8324e47 --- /dev/null +++ b/lab-5/samples/sorting-snippet.cbl @@ -0,0 +1,55 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. sorting. +DATA DIVISION. +WORKING-STORAGE SECTION. + 01 WS-SORT-AREA. + 05 WS-SORT-TABLE. + 10 WS-SORT-ROW PIC X(10) OCCURS 100. + 05 WS-TEMP-ROW PIC X(10). + 05 WS-ROW-MAX PIC S9(4) COMP VALUE 100. + 05 WS-SORT-MAX PIC S9(4) COMP. + 05 WS-I PIC S9(4) COMP. + 05 WS-J PIC S9(4) COMP. + 05 WS-INDEX PIC S9(4) COMP. + +PROCEDURE DIVISION. +*> Initialize test data + MOVE "30" TO WS-SORT-ROW(1) + MOVE "10" TO WS-SORT-ROW(2) + MOVE "50" TO WS-SORT-ROW(3) + MOVE "20" TO WS-SORT-ROW(4) + MOVE "40" TO WS-SORT-ROW(5) + MOVE 5 TO WS-SORT-MAX + +*> * Display original array + DISPLAY "Original Array Contents:" + DISPLAY "---------------------" + PERFORM VARYING WS-INDEX FROM 1 BY 1 + UNTIL WS-INDEX > WS-SORT-MAX + DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX) + END-PERFORM + DISPLAY SPACE + +*> * Simplified bubble sort + PERFORM VARYING WS-I FROM 1 BY 1 + UNTIL WS-I > WS-SORT-MAX - 1 + PERFORM VARYING WS-J FROM 1 BY 1 + UNTIL WS-J > WS-SORT-MAX - WS-I + IF WS-SORT-ROW(WS-J) > WS-SORT-ROW(WS-J + 1) + MOVE WS-SORT-ROW(WS-J) TO WS-TEMP-ROW + MOVE WS-SORT-ROW(WS-J + 1) TO WS-SORT-ROW(WS-J) + MOVE WS-TEMP-ROW TO WS-SORT-ROW(WS-J + 1) + END-IF + END-PERFORM + END-PERFORM + +*> * Display sorted array + DISPLAY "Sorted Array Contents:" + DISPLAY "--------------------" + PERFORM VARYING WS-INDEX FROM 1 BY 1 + UNTIL WS-INDEX > WS-SORT-MAX + DISPLAY "Element " WS-INDEX ": " WS-SORT-ROW(WS-INDEX) + END-PERFORM. + + STOP RUN. + \ No newline at end of file