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; }