From f899a5fcbc4653d39f18efb30aa1092cc66c3cdf Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 31 Oct 2024 11:46:29 -0700 Subject: [PATCH 1/7] added parser text --- lab-4/parser.bison | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lab-4/parser.bison b/lab-4/parser.bison index e4c4b6f..95eab98 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -68,6 +68,7 @@ statements : statements statement statement : section | sect_data | simple_stmt + | data_space ; section : type TOKEN_KEYWORD_DIVISION TOKEN_DOT | type TOKEN_RUN TOKEN_DOT @@ -80,11 +81,28 @@ type : TOKEN_IDENTIFICATION | TOKEN_KEYWORD_DATA ; simple_stmt : function + | TOKEN_KEYWORD_FUNCTION cbl_function + ; +cbl_function : TOKEN_KEYWORD_COMPUTE parms ; function : TOKEN_DISPLAY parms + | function simple_stmt + | if_branch ; parms : TOKEN_STRING ; +if_branch : if_branch parms if_branch + | TOKEN_IF + | TOKEN_ELSE_IF + | TOKEN_ELSE + | TOKEN_END_IF + ; +data_space : TOKEN_WORKING_STORAGE TOKEN_KEYWORD_SECTION TOKEN_DOT + ; + + + + %% From 243faa8e152accb6764905edb18ee5d9c711a6b4 Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Tue, 5 Nov 2024 15:59:58 -0800 Subject: [PATCH 2/7] variables now parse --- lab-4/parser.bison | 35 ++++++++++++++++- lab-4/samples/only-variables.cbl | 24 ++++++++++++ lab-4/token.h | 64 ++++++++++++++++---------------- 3 files changed, 91 insertions(+), 32 deletions(-) create mode 100644 lab-4/samples/only-variables.cbl diff --git a/lab-4/parser.bison b/lab-4/parser.bison index 90ac98b..794cbe1 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -71,6 +71,7 @@ statement : section | sect_data | simple_stmt | data_space + | data_declaration ; section : type TOKEN_KEYWORD_DIVISION TOKEN_DOT | type TOKEN_RUN TOKEN_DOT @@ -99,9 +100,41 @@ if_branch : if_branch parms if_branch | TOKEN_ELSE | TOKEN_END_IF ; -data_space : TOKEN_WORKING_STORAGE TOKEN_KEYWORD_SECTION TOKEN_DOT +data_space : TOKEN_WORKING_STORAGE + | TOKEN_KEYWORD_SECTION + | TOKEN_DOT + ; +data_category : TOKEN_ALPHANUMERIC + | TOKEN_NUMERIC + | TOKEN_SIGNED_NUMERIC + | TOKEN_IMPLIED_DECIMAL + ; +categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS ; +complete_category: complete_category complete_category + | data_category categry_contain + ; +data_clause : TOKEN_COMPUTATION_LEVEL_0 + | TOKEN_COMPUTATION_LEVEL_1 + | TOKEN_COMPUTATION_LEVEL_2 + | TOKEN_COMPUTATION_LEVEL_3 + | TOKEN_KEYWORD_VALUE + | TOKEN_KEYWORD_OCCURS + ; +full_data_clause: data_clause data_clause + | data_clause + ; + +data_declaration: simple_decl + | full_decl + ; +simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT + ; +full_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category TOKEN_DOT + | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_DOT + | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_INTEGER TOKEN_DOT + ; diff --git a/lab-4/samples/only-variables.cbl b/lab-4/samples/only-variables.cbl new file mode 100644 index 0000000..41685e8 --- /dev/null +++ b/lab-4/samples/only-variables.cbl @@ -0,0 +1,24 @@ +IDENTIFICATION DIVISION. +PROGRAM-ID. variables. +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. + 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. + DISPLAY 'Hello World!' + STOP RUN. \ No newline at end of file diff --git a/lab-4/token.h b/lab-4/token.h index fc7b161..ec55e23 100644 --- a/lab-4/token.h +++ b/lab-4/token.h @@ -73,37 +73,39 @@ extern int yydebug; TOKEN_UNTIL = 274, /* TOKEN_UNTIL */ TOKEN_END_PERFORM = 275, /* TOKEN_END_PERFORM */ TOKEN_IF = 276, /* TOKEN_IF */ - TOKEN_END_IF = 277, /* TOKEN_END_IF */ - TOKEN_SPACE = 278, /* TOKEN_SPACE */ - TOKEN_KEYWORD_OCCURS = 279, /* TOKEN_KEYWORD_OCCURS */ - TOKEN_KEYWORD_VALUE = 280, /* TOKEN_KEYWORD_VALUE */ - TOKEN_KEYWORD_COMPUTE = 281, /* TOKEN_KEYWORD_COMPUTE */ - TOKEN_KEYWORD_FUNCTION = 282, /* TOKEN_KEYWORD_FUNCTION */ - TOKEN_IDENT = 283, /* TOKEN_IDENT */ - TOKEN_STRING = 284, /* TOKEN_STRING */ - TOKEN_INTEGER = 285, /* TOKEN_INTEGER */ - TOKEN_PICTURE = 286, /* TOKEN_PICTURE */ - TOKEN_ALPHANUMERIC = 287, /* TOKEN_ALPHANUMERIC */ - TOKEN_NUMERIC = 288, /* TOKEN_NUMERIC */ - TOKEN_SIGNED_NUMERIC = 289, /* TOKEN_SIGNED_NUMERIC */ - TOKEN_IMPLIED_DECIMAL = 290, /* TOKEN_IMPLIED_DECIMAL */ - TOKEN_COMPUTATION_LEVEL_0 = 291, /* TOKEN_COMPUTATION_LEVEL_0 */ - TOKEN_COMPUTATION_LEVEL_1 = 292, /* TOKEN_COMPUTATION_LEVEL_1 */ - TOKEN_COMPUTATION_LEVEL_2 = 293, /* TOKEN_COMPUTATION_LEVEL_2 */ - TOKEN_COMPUTATION_LEVEL_3 = 294, /* TOKEN_COMPUTATION_LEVEL_3 */ - TOKEN_LEFT_PARENTHESIS = 295, /* TOKEN_LEFT_PARENTHESIS */ - TOKEN_RIGHT_PARENTHESIS = 296, /* TOKEN_RIGHT_PARENTHESIS */ - TOKEN_DOT = 297, /* TOKEN_DOT */ - TOKEN_COMMENT = 298, /* TOKEN_COMMENT */ - TOKEN_ADD = 299, /* TOKEN_ADD */ - TOKEN_SUB = 300, /* TOKEN_SUB */ - TOKEN_MULTIPLY = 301, /* TOKEN_MULTIPLY */ - TOKEN_DIVIDE = 302, /* TOKEN_DIVIDE */ - TOKEN_EQUAL = 303, /* TOKEN_EQUAL */ - TOKEN_GREATER_THAN = 304, /* TOKEN_GREATER_THAN */ - TOKEN_LESS_THAN = 305, /* TOKEN_LESS_THAN */ - TOKEN_EXPONENTIAL = 306, /* TOKEN_EXPONENTIAL */ - TOKEN_DISPLAY = 307 /* TOKEN_DISPLAY */ + TOKEN_ELSE = 277, /* TOKEN_ELSE */ + TOKEN_ELSE_IF = 278, /* TOKEN_ELSE_IF */ + TOKEN_END_IF = 279, /* TOKEN_END_IF */ + TOKEN_SPACE = 280, /* TOKEN_SPACE */ + TOKEN_KEYWORD_OCCURS = 281, /* TOKEN_KEYWORD_OCCURS */ + TOKEN_KEYWORD_VALUE = 282, /* TOKEN_KEYWORD_VALUE */ + TOKEN_KEYWORD_COMPUTE = 283, /* TOKEN_KEYWORD_COMPUTE */ + TOKEN_KEYWORD_FUNCTION = 284, /* TOKEN_KEYWORD_FUNCTION */ + TOKEN_IDENT = 285, /* TOKEN_IDENT */ + TOKEN_STRING = 286, /* TOKEN_STRING */ + TOKEN_INTEGER = 287, /* TOKEN_INTEGER */ + TOKEN_PICTURE = 288, /* TOKEN_PICTURE */ + TOKEN_ALPHANUMERIC = 289, /* TOKEN_ALPHANUMERIC */ + TOKEN_NUMERIC = 290, /* TOKEN_NUMERIC */ + TOKEN_SIGNED_NUMERIC = 291, /* TOKEN_SIGNED_NUMERIC */ + TOKEN_IMPLIED_DECIMAL = 292, /* TOKEN_IMPLIED_DECIMAL */ + TOKEN_COMPUTATION_LEVEL_0 = 293, /* TOKEN_COMPUTATION_LEVEL_0 */ + TOKEN_COMPUTATION_LEVEL_1 = 294, /* TOKEN_COMPUTATION_LEVEL_1 */ + TOKEN_COMPUTATION_LEVEL_2 = 295, /* TOKEN_COMPUTATION_LEVEL_2 */ + TOKEN_COMPUTATION_LEVEL_3 = 296, /* TOKEN_COMPUTATION_LEVEL_3 */ + TOKEN_LEFT_PARENTHESIS = 297, /* TOKEN_LEFT_PARENTHESIS */ + TOKEN_RIGHT_PARENTHESIS = 298, /* TOKEN_RIGHT_PARENTHESIS */ + TOKEN_DOT = 299, /* TOKEN_DOT */ + TOKEN_COMMENT = 300, /* TOKEN_COMMENT */ + TOKEN_ADD = 301, /* TOKEN_ADD */ + TOKEN_SUB = 302, /* TOKEN_SUB */ + TOKEN_MULTIPLY = 303, /* TOKEN_MULTIPLY */ + TOKEN_DIVIDE = 304, /* TOKEN_DIVIDE */ + TOKEN_EQUAL = 305, /* TOKEN_EQUAL */ + TOKEN_GREATER_THAN = 306, /* TOKEN_GREATER_THAN */ + TOKEN_LESS_THAN = 307, /* TOKEN_LESS_THAN */ + TOKEN_EXPONENTIAL = 308, /* TOKEN_EXPONENTIAL */ + TOKEN_DISPLAY = 309 /* TOKEN_DISPLAY */ }; typedef enum yytokentype yytoken_kind_t; #endif From 363f816531bc4045957a73bbd7a24d7471dc4c85 Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Thu, 7 Nov 2024 10:44:19 -0800 Subject: [PATCH 3/7] fix some identification --- lab-4/parser.bison | 40 ++++++++++++++++++++++------------------ lab-4/scanner.flex | 2 +- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lab-4/parser.bison b/lab-4/parser.bison index 794cbe1..d2110b3 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -10,7 +10,7 @@ int yylex(); %define parse.error detailed %token TOKEN_EOF -%token TOKEN_IDENTIFICATION +%token TOKEN_KEYWORD_IDENTIFICATION %token TOKEN_KEYWORD_DIVISION %token TOKEN_KEYWORD_DATA %token TOKEN_KEYWORD_SECTION @@ -73,26 +73,35 @@ statement : section | data_space | data_declaration ; + section : type TOKEN_KEYWORD_DIVISION TOKEN_DOT | type TOKEN_RUN TOKEN_DOT ; -sect_data : TOKEN_PROGRAM_ID TOKEN_DOT TOKEN_IDENT TOKEN_DOT +sect_data : TOKEN_PROGRAM_ID TOKEN_DOT TOKEN_IDENT TOKEN_DOT ; -type : TOKEN_IDENTIFICATION +type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_PROCEDURE | TOKEN_STOP | TOKEN_KEYWORD_DATA ; -simple_stmt : function - | TOKEN_KEYWORD_FUNCTION cbl_function +simple_stmt : cbl_function + | cbl_function params + | cbl_function assignment_stmt + | cbl_function params assignment_stmt ; -cbl_function : TOKEN_KEYWORD_COMPUTE parms +assignment_stmt : TOKEN_EQUAL ext_function + | TOKEN_KEYWORD_TO TOKEN_IDENT + | TOKEN_KEYWORD_TO TOKEN_IDENT categry_contain ; -function : TOKEN_DISPLAY parms - | function simple_stmt - | if_branch +params: : TOKEN_IDENT + | TOKEN_STRING ; -parms : TOKEN_STRING +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 : if_branch parms if_branch | TOKEN_IF @@ -111,7 +120,6 @@ data_category : TOKEN_ALPHANUMERIC ; categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS ; - complete_category: complete_category complete_category | data_category categry_contain ; @@ -125,19 +133,15 @@ data_clause : TOKEN_COMPUTATION_LEVEL_0 full_data_clause: data_clause data_clause | data_clause ; - -data_declaration: simple_decl - | full_decl - ; simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT ; full_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category TOKEN_DOT | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_DOT | TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE complete_category full_data_clause TOKEN_INTEGER TOKEN_DOT ; - - - +data_declaration: simple_decl + | full_decl + ; %% diff --git a/lab-4/scanner.flex b/lab-4/scanner.flex index b6843eb..ae317d3 100644 --- a/lab-4/scanner.flex +++ b/lab-4/scanner.flex @@ -7,7 +7,7 @@ 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; } From ba839f10a99d47c08d3f04cb7a013489aeaf004b Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Thu, 7 Nov 2024 10:51:51 -0800 Subject: [PATCH 4/7] fix params? --- lab-4/parser.bison | 10 +++++----- lab-4/token.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lab-4/parser.bison b/lab-4/parser.bison index d2110b3..07757af 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -85,25 +85,25 @@ type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_KEYWORD_DATA ; simple_stmt : cbl_function - | cbl_function params + | cbl_function param | cbl_function assignment_stmt - | cbl_function params assignment_stmt + | cbl_function param assignment_stmt ; assignment_stmt : TOKEN_EQUAL ext_function | TOKEN_KEYWORD_TO TOKEN_IDENT | TOKEN_KEYWORD_TO TOKEN_IDENT categry_contain ; -params: : TOKEN_IDENT +param : TOKEN_IDENT | TOKEN_STRING ; -ext_function: : TOKEN_KEYWORD_FUNCTION TOKEN_IDENT TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS +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 : if_branch parms if_branch +if_branch : if_branch param if_branch | TOKEN_IF | TOKEN_ELSE_IF | TOKEN_ELSE diff --git a/lab-4/token.h b/lab-4/token.h index ec55e23..ca9bbfb 100644 --- a/lab-4/token.h +++ b/lab-4/token.h @@ -55,7 +55,7 @@ extern int yydebug; YYerror = 256, /* error */ YYUNDEF = 257, /* "invalid token" */ TOKEN_EOF = 258, /* TOKEN_EOF */ - TOKEN_IDENTIFICATION = 259, /* TOKEN_IDENTIFICATION */ + TOKEN_KEYWORD_IDENTIFICATION = 259, /* TOKEN_KEYWORD_IDENTIFICATION */ TOKEN_KEYWORD_DIVISION = 260, /* TOKEN_KEYWORD_DIVISION */ TOKEN_KEYWORD_DATA = 261, /* TOKEN_KEYWORD_DATA */ TOKEN_KEYWORD_SECTION = 262, /* TOKEN_KEYWORD_SECTION */ From 8e76fdca913ccfe06212761787c00128753bc283 Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 7 Nov 2024 18:18:21 -0800 Subject: [PATCH 5/7] parse quadratic --- lab-4/parser.bison | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/lab-4/parser.bison b/lab-4/parser.bison index 458d394..4e21a07 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -28,6 +28,8 @@ int yylex(); %token TOKEN_UNTIL %token TOKEN_END_PERFORM %token TOKEN_IF +%token TOKEN_ELSE_IF +%token TOKEN_ELSE %token TOKEN_END_IF %token TOKEN_SPACE %token TOKEN_KEYWORD_OCCURS @@ -60,6 +62,7 @@ int yylex(); %token TOKEN_EXPONENTIAL %token TOKEN_DISPLAY + %% file : statements statements : statements statement @@ -83,16 +86,39 @@ type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_KEYWORD_DATA ; simple_stmt : cbl_function - | cbl_function param + | cbl_function param | cbl_function assignment_stmt | cbl_function param assignment_stmt + | cbl_function TOKEN_IDENT assignment_stmt + | if_branch + ; +expression : op_parms + | bool + ; +bool : op_parms TOKEN_EQUAL op_parms ; assignment_stmt : TOKEN_EQUAL ext_function + | TOKEN_EQUAL function | TOKEN_KEYWORD_TO TOKEN_IDENT | TOKEN_KEYWORD_TO TOKEN_IDENT categry_contain ; +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 + | TOKEN_SUB op_parms + | TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS + | TOKEN_IDENT + | TOKEN_INTEGER + ; param : TOKEN_IDENT | TOKEN_STRING + | param param + ; +function : op_parms ; ext_function : TOKEN_KEYWORD_FUNCTION TOKEN_IDENT TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; @@ -101,10 +127,9 @@ cbl_function : TOKEN_DISPLAY | TOKEN_KEYWORD_COMPUTE | TOKEN_PERFORM ; -if_branch : if_branch param if_branch - | TOKEN_IF - | TOKEN_ELSE_IF - | TOKEN_ELSE +if_branch : TOKEN_IF expression + | TOKEN_ELSE_IF expression + | TOKEN_ELSE statement | TOKEN_END_IF ; data_space : TOKEN_WORKING_STORAGE From 3b3cdbc59246ad7e3aca3d63a11d0176a19bab99 Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Sun, 10 Nov 2024 15:19:49 -0800 Subject: [PATCH 6/7] clean up code --- lab-4/samples/only-variables.cbl | 2 +- lab-4/token.h | 127 ------------------------------- 2 files changed, 1 insertion(+), 128 deletions(-) delete mode 100644 lab-4/token.h diff --git a/lab-4/samples/only-variables.cbl b/lab-4/samples/only-variables.cbl index 41685e8..1a900a5 100644 --- a/lab-4/samples/only-variables.cbl +++ b/lab-4/samples/only-variables.cbl @@ -4,7 +4,7 @@ DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-SORT-AREA. 05 WS-SORT-TABLE. - 10 WS-SORT-ROW PIC X(10) OCCURS 100. + 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. diff --git a/lab-4/token.h b/lab-4/token.h deleted file mode 100644 index ca9bbfb..0000000 --- a/lab-4/token.h +++ /dev/null @@ -1,127 +0,0 @@ -/* A Bison parser, made by GNU Bison 3.8.2. */ - -/* Bison interface for Yacc-like parsers in C - - Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation, - Inc. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . */ - -/* As a special exception, you may create a larger work that contains - part or all of the Bison parser skeleton and distribute that work - under terms of your choice, so long as that work isn't itself a - parser generator using the skeleton or a modified version thereof - as a parser skeleton. Alternatively, if you modify or redistribute - the parser skeleton itself, you may (at your option) remove this - special exception, which will cause the skeleton and the resulting - Bison output files to be licensed under the GNU General Public - License without this special exception. - - This special exception was added by the Free Software Foundation in - version 2.2 of Bison. */ - -/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, - especially those whose name start with YY_ or yy_. They are - private implementation details that can be changed or removed. */ - -#ifndef YY_YY_TOKEN_H_INCLUDED -# define YY_YY_TOKEN_H_INCLUDED -/* Debug traces. */ -#ifndef YYDEBUG -# define YYDEBUG 1 -#endif -#if YYDEBUG -extern int yydebug; -#endif - -/* Token kinds. */ -#ifndef YYTOKENTYPE -# define YYTOKENTYPE - enum yytokentype - { - YYEMPTY = -2, - YYEOF = 0, /* "end of file" */ - YYerror = 256, /* error */ - YYUNDEF = 257, /* "invalid token" */ - TOKEN_EOF = 258, /* TOKEN_EOF */ - TOKEN_KEYWORD_IDENTIFICATION = 259, /* TOKEN_KEYWORD_IDENTIFICATION */ - TOKEN_KEYWORD_DIVISION = 260, /* TOKEN_KEYWORD_DIVISION */ - TOKEN_KEYWORD_DATA = 261, /* TOKEN_KEYWORD_DATA */ - TOKEN_KEYWORD_SECTION = 262, /* TOKEN_KEYWORD_SECTION */ - TOKEN_PROGRAM_ID = 263, /* TOKEN_PROGRAM_ID */ - TOKEN_WORKING_STORAGE = 264, /* TOKEN_WORKING_STORAGE */ - TOKEN_PROCEDURE = 265, /* TOKEN_PROCEDURE */ - TOKEN_STOP = 266, /* TOKEN_STOP */ - TOKEN_RUN = 267, /* TOKEN_RUN */ - TOKEN_MOVE = 268, /* TOKEN_MOVE */ - TOKEN_KEYWORD_TO = 269, /* TOKEN_KEYWORD_TO */ - TOKEN_PERFORM = 270, /* TOKEN_PERFORM */ - TOKEN_VARYING = 271, /* TOKEN_VARYING */ - TOKEN_KEYWORD_FROM = 272, /* TOKEN_KEYWORD_FROM */ - TOKEN_KEYWORD_BY = 273, /* TOKEN_KEYWORD_BY */ - TOKEN_UNTIL = 274, /* TOKEN_UNTIL */ - TOKEN_END_PERFORM = 275, /* TOKEN_END_PERFORM */ - TOKEN_IF = 276, /* TOKEN_IF */ - TOKEN_ELSE = 277, /* TOKEN_ELSE */ - TOKEN_ELSE_IF = 278, /* TOKEN_ELSE_IF */ - TOKEN_END_IF = 279, /* TOKEN_END_IF */ - TOKEN_SPACE = 280, /* TOKEN_SPACE */ - TOKEN_KEYWORD_OCCURS = 281, /* TOKEN_KEYWORD_OCCURS */ - TOKEN_KEYWORD_VALUE = 282, /* TOKEN_KEYWORD_VALUE */ - TOKEN_KEYWORD_COMPUTE = 283, /* TOKEN_KEYWORD_COMPUTE */ - TOKEN_KEYWORD_FUNCTION = 284, /* TOKEN_KEYWORD_FUNCTION */ - TOKEN_IDENT = 285, /* TOKEN_IDENT */ - TOKEN_STRING = 286, /* TOKEN_STRING */ - TOKEN_INTEGER = 287, /* TOKEN_INTEGER */ - TOKEN_PICTURE = 288, /* TOKEN_PICTURE */ - TOKEN_ALPHANUMERIC = 289, /* TOKEN_ALPHANUMERIC */ - TOKEN_NUMERIC = 290, /* TOKEN_NUMERIC */ - TOKEN_SIGNED_NUMERIC = 291, /* TOKEN_SIGNED_NUMERIC */ - TOKEN_IMPLIED_DECIMAL = 292, /* TOKEN_IMPLIED_DECIMAL */ - TOKEN_COMPUTATION_LEVEL_0 = 293, /* TOKEN_COMPUTATION_LEVEL_0 */ - TOKEN_COMPUTATION_LEVEL_1 = 294, /* TOKEN_COMPUTATION_LEVEL_1 */ - TOKEN_COMPUTATION_LEVEL_2 = 295, /* TOKEN_COMPUTATION_LEVEL_2 */ - TOKEN_COMPUTATION_LEVEL_3 = 296, /* TOKEN_COMPUTATION_LEVEL_3 */ - TOKEN_LEFT_PARENTHESIS = 297, /* TOKEN_LEFT_PARENTHESIS */ - TOKEN_RIGHT_PARENTHESIS = 298, /* TOKEN_RIGHT_PARENTHESIS */ - TOKEN_DOT = 299, /* TOKEN_DOT */ - TOKEN_COMMENT = 300, /* TOKEN_COMMENT */ - TOKEN_ADD = 301, /* TOKEN_ADD */ - TOKEN_SUB = 302, /* TOKEN_SUB */ - TOKEN_MULTIPLY = 303, /* TOKEN_MULTIPLY */ - TOKEN_DIVIDE = 304, /* TOKEN_DIVIDE */ - TOKEN_EQUAL = 305, /* TOKEN_EQUAL */ - TOKEN_GREATER_THAN = 306, /* TOKEN_GREATER_THAN */ - TOKEN_LESS_THAN = 307, /* TOKEN_LESS_THAN */ - TOKEN_EXPONENTIAL = 308, /* TOKEN_EXPONENTIAL */ - TOKEN_DISPLAY = 309 /* TOKEN_DISPLAY */ - }; - typedef enum yytokentype yytoken_kind_t; -#endif - -/* Value type. */ -#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED -typedef int YYSTYPE; -# define YYSTYPE_IS_TRIVIAL 1 -# define YYSTYPE_IS_DECLARED 1 -#endif - - -extern YYSTYPE yylval; - - -int yyparse (void); - - -#endif /* !YY_YY_TOKEN_H_INCLUDED */ From afcc0310c8d289fea211e952d4df755dc5810ac5 Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Sun, 10 Nov 2024 16:51:30 -0800 Subject: [PATCH 7/7] finish lab-4 --- lab-4/main_test.c | 49 ++++++++++++++++------------------------------ lab-4/parser.bison | 27 ++++++++++++------------- lab-4/scanner.flex | 2 +- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/lab-4/main_test.c b/lab-4/main_test.c index d82f3eb..2a8af21 100644 --- a/lab-4/main_test.c +++ b/lab-4/main_test.c @@ -16,7 +16,7 @@ UTEST_MAIN(); UTEST(parser, math) { // Must include the null character to terminate input - char string[] = "1+8/4-3;\0"; + char string[] = "COMPUTE A = (b ** 2) - (4 * a * c)\0"; YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); yylineno = 1; @@ -28,20 +28,6 @@ UTEST(parser, math) { ASSERT_EQ(result, 0); } -UTEST(parser, missing_semi_colon) { - // Must include the null character to terminate input - char string[] = "1+8/4-3\0"; - YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); - - yylineno = 1; - int result = yyparse(); - - yy_delete_buffer(buffer); - - // Assert the result to test correctness - ASSERT_EQ(result, 1); -} - UTEST(parser, hello) { // Read sample file as input yyin = fopen("samples/hello-world.cbl", "r"); @@ -71,7 +57,7 @@ UTEST(parser, print) { UTEST(parser, branching) { // Must include the null character to terminate input - char string[] = "IF A > B THEN DISPLAY 'A is greater than B' ELSE DISPLAY 'B is greater than A'\0"; + char string[] = "IF A > B DISPLAY 'A is greater than B' ELSE DISPLAY 'B is greater than A'\0"; YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); yylineno = 1; @@ -81,10 +67,23 @@ UTEST(parser, branching) { // Assert the result to test correctness ASSERT_EQ(result, 0); -} +} UTEST(parser, looping) { - char string[] = "PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10 MOVE I TO A(I)\0"; + char string[] = "PERFORM VARYING I FROM 1 BY 1 UNTIL I > 10\0"; + YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); + + yylineno = 1; + int result = yyparse(); + + yy_delete_buffer(buffer); + + // Assert the result to test correctness + ASSERT_EQ(result, 0); +} + +UTEST(parser, assignment) { + char string[] = "MOVE I TO A(I) MOVE 1 TO I COMPUTE discriminant = (b ** 2) - (4 * a * c)\0"; YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); yylineno = 1; @@ -121,17 +120,3 @@ UTEST(parser, quadratic) { // Assert the result to test correctness ASSERT_EQ(result, 0); } - -UTEST(parser, boolean) { - // Must include the null character to terminate input - char string[] = "IF A > B THEN Var = TRUE ELSE Var = FALSE\0"; - YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); - - yylineno = 1; - int result = yyparse(); - - yy_delete_buffer(buffer); - - // Assert the result to test correctness - ASSERT_EQ(result, 0); -} diff --git a/lab-4/parser.bison b/lab-4/parser.bison index 4e21a07..babfbd9 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -86,21 +86,16 @@ type : TOKEN_KEYWORD_IDENTIFICATION | TOKEN_KEYWORD_DATA ; simple_stmt : cbl_function - | cbl_function param + | cbl_function op_parms | cbl_function assignment_stmt - | cbl_function param assignment_stmt - | cbl_function TOKEN_IDENT assignment_stmt + | cbl_function op_parms assignment_stmt | if_branch + | perform_stmt ; -expression : op_parms - | bool - ; -bool : op_parms TOKEN_EQUAL op_parms ; assignment_stmt : TOKEN_EQUAL ext_function | TOKEN_EQUAL function - | TOKEN_KEYWORD_TO TOKEN_IDENT - | TOKEN_KEYWORD_TO TOKEN_IDENT categry_contain + | TOKEN_KEYWORD_TO op_parms ; op_parms : op_parms TOKEN_ADD op_parms | op_parms TOKEN_SUB op_parms @@ -109,14 +104,14 @@ op_parms : op_parms TOKEN_ADD 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 | TOKEN_IDENT | TOKEN_INTEGER - ; -param : TOKEN_IDENT | TOKEN_STRING - | param param + | TOKEN_SPACE + | op_parms op_parms ; function : op_parms ; @@ -127,11 +122,14 @@ cbl_function : TOKEN_DISPLAY | TOKEN_KEYWORD_COMPUTE | TOKEN_PERFORM ; -if_branch : TOKEN_IF expression - | TOKEN_ELSE_IF expression +if_branch : TOKEN_IF op_parms + | TOKEN_ELSE_IF op_parms | TOKEN_ELSE statement | TOKEN_END_IF ; +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 + ; data_space : TOKEN_WORKING_STORAGE | TOKEN_KEYWORD_SECTION | TOKEN_DOT @@ -142,6 +140,7 @@ data_category : TOKEN_ALPHANUMERIC | TOKEN_IMPLIED_DECIMAL ; categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS + | TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; complete_category: complete_category complete_category | data_category categry_contain diff --git a/lab-4/scanner.flex b/lab-4/scanner.flex index 5c97694..4b7a148 100644 --- a/lab-4/scanner.flex +++ b/lab-4/scanner.flex @@ -6,7 +6,7 @@ DIGIT [0-9]+ %% (" "|\t|\n) /* skip whitespace */ -\*>\ ?.* { return TOKEN_COMMENT; } +\*>\ ?.* IDENTIFICATION { return TOKEN_KEYWORD_IDENTIFICATION; } DIVISION { return TOKEN_KEYWORD_DIVISION; } PROGRAM-ID { return TOKEN_PROGRAM_ID; }