diff --git a/lab-3/main.c b/lab-3/main.c index 1187003..7e4a834 100644 --- a/lab-3/main.c +++ b/lab-3/main.c @@ -8,7 +8,7 @@ extern char *yytext; int main(int argc, char *argv[]) { FILE *file; - const char *filename = "samples/quadratic_snippet.cbl"; // Default filename + const char *filename = "samples/quadratic-snippet.cbl"; // Check if a filename is provided as a command-line argument if (argc > 1) { diff --git a/lab-3/main_test.c b/lab-3/main_test.c index 1eb729a..1dd60bd 100644 --- a/lab-3/main_test.c +++ b/lab-3/main_test.c @@ -20,7 +20,7 @@ struct token_st { UTEST(scanner, hello) { struct token_st tokens[] = { - {TOKEN_IDENTIFICATION, "IDENTIFICATION"}, + {TOKEN_KEYWORD_IDENTIFICATION, "IDENTIFICATION"}, {TOKEN_KEYWORD_DIVISION, "DIVISION"}, {TOKEN_DOT, "."}, {TOKEN_PROGRAM_ID, "PROGRAM-ID"}, @@ -56,7 +56,7 @@ UTEST(scanner, quadratic) { {TOKEN_COMMENT, "*> Code altered from https://www.quora.com/What-is-a-COBOL-program-that-will-solve-a-quadratic-equation"}, {TOKEN_COMMENT, "*> Program finds the roots to a simple quadratic equation"}, - {TOKEN_IDENTIFICATION, "IDENTIFICATION"}, + {TOKEN_KEYWORD_IDENTIFICATION, "IDENTIFICATION"}, {TOKEN_KEYWORD_DIVISION, "DIVISION"}, {TOKEN_DOT, "."}, {TOKEN_PROGRAM_ID, "PROGRAM-ID"}, @@ -316,7 +316,7 @@ UTEST(scanner, quadratic) { UTEST(scanner, sorting) { struct token_st tokens[] = { - {TOKEN_IDENTIFICATION, "IDENTIFICATION"}, + {TOKEN_KEYWORD_IDENTIFICATION, "IDENTIFICATION"}, {TOKEN_KEYWORD_DIVISION, "DIVISION"}, {TOKEN_DOT, "."}, {TOKEN_PROGRAM_ID, "PROGRAM-ID"}, diff --git a/lab-3/samples/quadratic-snippet.cbl b/lab-3/samples/quadratic-snippet.cbl index 18ba158..00eb26f 100644 --- a/lab-3/samples/quadratic-snippet.cbl +++ b/lab-3/samples/quadratic-snippet.cbl @@ -14,22 +14,21 @@ 77 square-root-discriminant PIC S9(5)V9(5) COMP-3. PROCEDURE DIVISION. *> program begins here - DISPLAY 'EQUATION: (1x^2) + 5x + 6 = 0' + 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 - + 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 + DISPLAY "The equation has one real root: " + DISPLAY "Root: " root1 ELSE - DISPLAY 'The equation has no real roots.' - + DISPLAY "The equation has no real roots." + END-IF STOP RUN. diff --git a/lab-3/scanner b/lab-3/scanner new file mode 100755 index 0000000..896da2e Binary files /dev/null and b/lab-3/scanner differ diff --git a/lab-3/scanner.flex b/lab-3/scanner.flex index cc22816..4b7a148 100644 --- a/lab-3/scanner.flex +++ b/lab-3/scanner.flex @@ -6,8 +6,8 @@ DIGIT [0-9]+ %% (" "|\t|\n) /* skip whitespace */ -\*>\ ?.* { return TOKEN_COMMENT; } -IDENTIFICATION { return TOKEN_IDENTIFICATION; } +\*>\ ?.* +IDENTIFICATION { return TOKEN_KEYWORD_IDENTIFICATION; } DIVISION { return TOKEN_KEYWORD_DIVISION; } PROGRAM-ID { return TOKEN_PROGRAM_ID; } PROCEDURE { return TOKEN_PROCEDURE; } @@ -27,8 +27,7 @@ PERFORM { return TOKEN_PERFORM; } END-PERFORM { return TOKEN_END_PERFORM; } IF { return TOKEN_IF; } END-IF { return TOKEN_END_IF; } -ELSE { return TOKEN_ELSE; } -SPACE { return TOKEN_SPACE; } +SPACE { return TOKEN_SPACE; } PIC { return TOKEN_PICTURE; } OCCURS { return TOKEN_KEYWORD_OCCURS; } VALUE { return TOKEN_KEYWORD_VALUE; } @@ -43,7 +42,6 @@ COMP-1 { return TOKEN_COMPUTATION_LEVEL_1; } COMP-2 { return TOKEN_COMPUTATION_LEVEL_2; } COMP-3 { return TOKEN_COMPUTATION_LEVEL_3; } - {DIGIT} { return TOKEN_INTEGER; } {NAME} { return TOKEN_IDENT; } \+ { return TOKEN_ADD; } @@ -55,7 +53,6 @@ COMP-3 { return TOKEN_COMPUTATION_LEVEL_3; } \< { return TOKEN_LESS_THAN; } \= { return TOKEN_EQUAL;} - "\""[^"]*"\"" { return TOKEN_STRING; } "\'"[^']*"\'" { return TOKEN_STRING; } "(" { return TOKEN_LEFT_PARENTHESIS; } diff --git a/lab-3/token.h b/lab-3/token.h index e64609e..20c1ffa 100644 --- a/lab-3/token.h +++ b/lab-3/token.h @@ -1,6 +1,6 @@ typedef enum { TOKEN_EOF = 0, - TOKEN_IDENTIFICATION, + TOKEN_KEYWORD_IDENTIFICATION, TOKEN_KEYWORD_DIVISION, TOKEN_KEYWORD_DATA, TOKEN_KEYWORD_SECTION, diff --git a/lab-4/parser.bison b/lab-4/parser.bison index c0c71ca..dfeb38e 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -93,43 +93,53 @@ simple_stmt : cbl_func_stmt cbl_func_stmt : cbl_function | cbl_function op_parms | cbl_function assignment_stmt - | cbl_function op_parms assignment_stmt + | cbl_function op_parm assignment_stmt ; assignment_stmt : TOKEN_EQUAL ext_function - | TOKEN_EQUAL function + | TOKEN_EQUAL op_parms | TOKEN_KEYWORD_TO op_parms ; -op_parms : op_parms TOKEN_ADD op_parms - | op_parms TOKEN_SUB op_parms - | op_parms TOKEN_MULTIPLY op_parms - | op_parms TOKEN_DIVIDE op_parms - | op_parms TOKEN_EXPONENTIAL op_parms - | op_parms TOKEN_LESS_THAN op_parms - | op_parms TOKEN_GREATER_THAN op_parms - | op_parms TOKEN_EQUAL op_parms - | TOKEN_SUB op_parms - | TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS - | expr - | op_parms op_parms +op_parms : op_parm + | op_parms op_parm ; -expr : TOKEN_IDENT +op_parm : mathmaticalexpr + | booleanexpr + | type_expr + ; +mathmaticalexpr : type_expr + | mathmaticalexpr TOKEN_ADD mathmaticalexpr + | mathmaticalexpr TOKEN_SUB mathmaticalexpr + | mathmaticalexpr TOKEN_MULTIPLY mathmaticalexpr + | mathmaticalexpr TOKEN_DIVIDE mathmaticalexpr + | mathmaticalexpr TOKEN_EXPONENTIAL mathmaticalexpr + | container_expr + | type_expr container_expr + ; +container_expr : TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS + ; +booleanexpr : mathmaticalexpr TOKEN_LESS_THAN mathmaticalexpr + | mathmaticalexpr TOKEN_GREATER_THAN mathmaticalexpr + | mathmaticalexpr TOKEN_EQUAL mathmaticalexpr + ; +type_expr : TOKEN_IDENT | TOKEN_INTEGER | TOKEN_STRING | TOKEN_SPACE - ; -function : op_parms + | TOKEN_SUB TOKEN_IDENT ; ext_function : TOKEN_KEYWORD_FUNCTION TOKEN_IDENT TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; cbl_function : TOKEN_DISPLAY | TOKEN_MOVE | TOKEN_KEYWORD_COMPUTE - | TOKEN_PERFORM ; -if_branch : TOKEN_IF op_parms - | TOKEN_ELSE_IF op_parms - | TOKEN_ELSE statement - | TOKEN_END_IF +if_branch : if_start statements else_parts + ; +if_start : TOKEN_IF booleanexpr + ; +else_parts : + | TOKEN_ELSE_IF booleanexpr statements else_parts + | TOKEN_ELSE statements ; perform_stmt : TOKEN_PERFORM TOKEN_VARYING TOKEN_IDENT TOKEN_KEYWORD_FROM TOKEN_INTEGER TOKEN_KEYWORD_BY TOKEN_INTEGER TOKEN_UNTIL op_parms | TOKEN_END_PERFORM diff --git a/lab-4/samples/quadratic-snippet.cbl b/lab-4/samples/quadratic-snippet.cbl index ab76e40..7f27b4d 100644 --- a/lab-4/samples/quadratic-snippet.cbl +++ b/lab-4/samples/quadratic-snippet.cbl @@ -24,12 +24,10 @@ 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-4/samples/sorting-snippet.cbl b/lab-4/samples/sorting-snippet.cbl index 8324e47..07b2ebd 100644 --- a/lab-4/samples/sorting-snippet.cbl +++ b/lab-4/samples/sorting-snippet.cbl @@ -39,7 +39,6 @@ PROCEDURE DIVISION. 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