fix sorting code and add details about integer sort

This commit is contained in:
vel 2024-10-21 20:27:10 -07:00
parent f6dc7afd27
commit 14746ee3e5
Signed by: velvox
GPG Key ID: 59D9762F674151DF
7 changed files with 180 additions and 114 deletions

View File

@ -1,23 +1,31 @@
# Language Proposal
### 1. Hello World
- source used: [Cobol Emulator](https://www.jdoodle.com/execute-cobol-online)
- display the text "Hello World!" in the terminal
- https://www.tutorialspoint.com/cobol/cobol_program_structure.htm this website has useful information about the structure of the cobol language
- the Identification Division is mandatory for all programs and sub divisions have unique sentences within them and determine what is done with variables or IO in this example
#### Input
#### Input
there is no input for this example
#### Output
#### Output
the string "Hello World!" is sent to output via the included DISPLAY function
### 2. Quadratic Function
- Code altered from [Marco Biagini](https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation)
- Solves a quadratic equation of the form **ax^2 + bx + c = 0**
#### Input
#### Input
Coefficients are a fixed value with a = 1, b = 5, c = 6
#### Output
#### Output
EQUATION: (1x^2) + 5x + 6 = 0
The equation has two distinct real roots:
@ -26,35 +34,48 @@
### 3. Integer Sort
- Code altered from [Wikibooks]https://en.wikibooks.org/wiki/Algorithm_Implementation/Sorting/Bubble_sort#COBOL
- Uses bubble sort to sort an array of integers.
#### Input
An array of integers
#### Output
The array sorted in ascending order
### 4. Language Features
- #### Identifiers
All variables are declared within DATA DIVISION. Variable names accept letters(A-Z), digits(0-9), and hyphens(-). In the syntax of:
All variables are declared within DATA DIVISION. Variable names accept letters(A-Z), digits(0-9), and hyphens(-). In the syntax of:
<level-number> <variable-name>
[PIC/PICTURE <data-type(variable-length>)]
[VALUE <literal-value>]
Statements in [ ] are optional. Example:
Statements in [ ] are optional. Example:
01 INT-VAR PIC 9(03 VALUE 123).
- #### Special words
- Note: Keywords are not case sensitive but good practice is to only use uppercase
- Division statements divide the structure of the code. Some examples are PROGRAM-ID, DATA DIVISION, WORKING STORAGE DIVISION, PROCEDURE DIVISION, and STOP RUN.
- DISPLAY outputs data to user
- MOVE sends data from one variable to another
- ACCEPT allows for user input to go into a variable
- Supports boolean values TRUE and FALSE
- Note: Keywords are not case sensitive but good practice is to only use uppercase
- Division statements divide the structure of the code. Some examples are PROGRAM-ID, DATA DIVISION, WORKING STORAGE DIVISION, PROCEDURE DIVISION, and STOP RUN.
- DISPLAY outputs data to user
- MOVE sends data from one variable to another
- ACCEPT allows for user input to go into a variable
- Supports boolean values TRUE and FALSE
- #### Literal
*> This is a comment in COBOL
Non-numeric
Non-numeric
DISPLAY "This is a string in COBOL".
DISPLAY 'This is a string in COBOL'.
Numeric
Numeric
Digits 0 - 9
Positive numbers: +10
@ -62,27 +83,31 @@
Decimal: 10.00
- #### Math Operators
Supports additon (+), subraction (-), multiplication (*), and division (/) and exponentation (**). Equations begin with COMPUTE.
Supports additon (+), subraction (-), multiplication (\*), and division (/) and exponentation (\*\*). Equations begin with COMPUTE.
COMPUTE total = 1 + 2
- #### Relational Operators
Supports =, >, <, >=, <=, NOT =
Supports =, >, <, >=, <=, NOT =
IF a > b
- #### Delimiters
Section headers and verb statements should end with a separator period (.)
Section headers and verb statements should end with a separator period (.)
PROCEDURE DIVISION.
MOVE "Hello" TO VAR1.
Commas (,) can separate two variables
Commas (,) can separate two variables
ADD 3 TO VAR1, VAR2.
- #### Lists
Supports arrays (known in COBOL as tables). Declared in DATA DIVISION. In the syntax of:
Supports arrays (known in COBOL as tables). Declared in DATA DIVISION. In the syntax of:
01 <table-name>.
02 <variable-name> [PIC <data-type(length1)>]
@ -90,28 +115,29 @@
[DEPENDING ON]
[DESCENDING|ASCENDING KEY IS <key_var>]
[INDEXED BY <index_name>]
Statements in [ ] are optional. Example:
Statements in [ ] are optional. Example:
01 Students
03 Student-grades PIC 9(03) OCCURS 6 TIMES.
- #### Loops
Loops begin with a PERFORM statement and end with END-PERFORM. Note that a separator period (.) should not used within a PERFORM block.
Loops begin with a PERFORM statement and end with END-PERFORM. Note that a separator period (.) should not used within a PERFORM block.
PERFORM <conditional statement>
<statements-block>
END-PERFORM.
- #### Branching
Uses IF/END-IF and ELSE statements and nested statements. Note that a separator period (.) should not used within an IF statement.
Uses IF/END-IF and ELSE statements and nested statements. Note that a separator period (.) should not used within an IF statement.
IF <condition>
DISPLAY "Option 1"
ELSE
DISPLAY "Option 2"
END-IF.
` Language Feature Sources `
`Language Feature Sources`
- [tutorialspoint.com](https://www.tutorialspoint.com/cobol/cobol_basic_syntax.htm)

View File

@ -1,75 +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.
*> code taken from https://craftofcoding.wordpress.com/2021/03/23/coding-cobol-a-bubblesort/
identification division.
program-id. stats.
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
environment division.
input-output section.
file-control.
select input-file assign to dynamic fname-inp
organization is line sequential.
*> * 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
data division.
file section.
fd input-file.
01 sample-input pic x(80).
*> * 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
working-storage section.
77 n pic 9999 value 0.
77 feof pic A(1).
77 temp pic s9(14)v9(4) usage is computational-3.
77 fname-inp pic x(30).
77 i pic 9999.
77 j pic 9999.
77 jp1 pic 9999.
77 jp2 pic 9999.
*> * 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.
01 array-area.
02 x pic s9(14)v9(4) usage is computational-3
occurs 1000 times.
STOP RUN.
01 input-value.
02 in-x pic s9(14)v9(4).
02 filler pic x(62).
procedure division.
display "Input filename? "
accept fname-inp.
open input input-file.
perform input-loop until feof='Y'
perform bubblesort.
perform print-nums.
perform finish.
input-loop.
read input-file into input-value
at end move 'Y' to feof
not at end
add 1 to n
move in-x to x(n)
end-read.
bubblesort.
perform varying i from 1 by 1 until i is greater than n
compute jp1 = n - i
perform varying j from 1 by 1 until j is greater than jp1
compute jp2 = j + 1
if (x(j) > x(jp2))
move x(j) to temp
move x(jp2) to x(j)
move temp to x(jp2)
end-if
end-perform
end-perform.
print-nums.
move 1 to i.
perform until i > n
display i "->"x(i)
add 1 to i
end-perform.
finish.
close input-file.
stop run.

View File

@ -0,0 +1,6 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. HELLO-WORLD.
PROCEDURE DIVISION.
DISPLAY "Hello World!"
STOP RUN.
if

View File

@ -1 +0,0 @@
answer = 2020+4

View File

@ -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.

View File

@ -5,11 +5,8 @@ DIGIT [0-9]
LETTER [a-zA-Z]
%%
(" "|\t|\n) /* skip whitespace */
\+ { return TOKEN_ADD; }
"=" { return TOKEN_ASSIGNMENT; }
while { return TOKEN_WHILE; }
{LETTER}+ { return TOKEN_IDENT; }
{DIGIT}+ { return TOKEN_NUMBER; }
. { return TOKEN_ERROR; }
IDENTIFICATION { return TOKEN_IDENTIFICATION; }
DIVISION { return TOKEN_KEYWORD_DIVISION; }
%%
int yywrap() { return 1; }

View File

@ -1,9 +1,12 @@
typedef enum {
TOKEN_EOF = 0,
TOKEN_WHILE,
TOKEN_ADD,
TOKEN_ASSIGNMENT,
TOKEN_IDENT,
TOKEN_NUMBER,
TOKEN_ERROR
TOKEN_IDENTIFICATION,
TOKEN_KEYWORD_DIVISION,
TOKEN_PROGRAM_ID,
TOKEN_PROCEDURE,
TOKEN_DISPLAY,
TOKEN_STOP,
TOKEN_RUN,
TOKEN_STRING,
TOKEN_DOT
} token_t;