finish lab-4

This commit is contained in:
vel 2024-11-10 16:51:30 -08:00
parent 571cb86484
commit afcc0310c8
Signed by: velvox
GPG Key ID: 59D9762F674151DF
3 changed files with 31 additions and 47 deletions

View File

@ -16,7 +16,7 @@ UTEST_MAIN();
UTEST(parser, math) { UTEST(parser, math) {
// Must include the null character to terminate input // 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)); YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string));
yylineno = 1; yylineno = 1;
@ -28,20 +28,6 @@ UTEST(parser, math) {
ASSERT_EQ(result, 0); 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) { UTEST(parser, hello) {
// Read sample file as input // Read sample file as input
yyin = fopen("samples/hello-world.cbl", "r"); yyin = fopen("samples/hello-world.cbl", "r");
@ -71,7 +57,7 @@ UTEST(parser, print) {
UTEST(parser, branching) { UTEST(parser, branching) {
// Must include the null character to terminate input // 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)); YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string));
yylineno = 1; yylineno = 1;
@ -84,7 +70,20 @@ UTEST(parser, branching) {
} }
UTEST(parser, looping) { 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)); YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string));
yylineno = 1; yylineno = 1;
@ -121,17 +120,3 @@ UTEST(parser, quadratic) {
// Assert the result to test correctness // Assert the result to test correctness
ASSERT_EQ(result, 0); 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);
}

View File

@ -86,21 +86,16 @@ type : TOKEN_KEYWORD_IDENTIFICATION
| TOKEN_KEYWORD_DATA | TOKEN_KEYWORD_DATA
; ;
simple_stmt : cbl_function simple_stmt : cbl_function
| cbl_function param | cbl_function op_parms
| cbl_function assignment_stmt | cbl_function assignment_stmt
| cbl_function param assignment_stmt | cbl_function op_parms assignment_stmt
| cbl_function TOKEN_IDENT assignment_stmt
| if_branch | if_branch
| perform_stmt
; ;
expression : op_parms
| bool
;
bool : op_parms TOKEN_EQUAL op_parms
; ;
assignment_stmt : TOKEN_EQUAL ext_function assignment_stmt : TOKEN_EQUAL ext_function
| TOKEN_EQUAL function | TOKEN_EQUAL function
| TOKEN_KEYWORD_TO TOKEN_IDENT | TOKEN_KEYWORD_TO op_parms
| TOKEN_KEYWORD_TO TOKEN_IDENT categry_contain
; ;
op_parms : op_parms TOKEN_ADD op_parms op_parms : op_parms TOKEN_ADD op_parms
| op_parms TOKEN_SUB 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_EXPONENTIAL op_parms
| op_parms TOKEN_LESS_THAN op_parms | op_parms TOKEN_LESS_THAN op_parms
| op_parms TOKEN_GREATER_THAN op_parms | op_parms TOKEN_GREATER_THAN op_parms
| op_parms TOKEN_EQUAL op_parms
| TOKEN_SUB op_parms | TOKEN_SUB op_parms
| TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS | TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS
| TOKEN_IDENT | TOKEN_IDENT
| TOKEN_INTEGER | TOKEN_INTEGER
;
param : TOKEN_IDENT
| TOKEN_STRING | TOKEN_STRING
| param param | TOKEN_SPACE
| op_parms op_parms
; ;
function : op_parms function : op_parms
; ;
@ -127,11 +122,14 @@ cbl_function : TOKEN_DISPLAY
| TOKEN_KEYWORD_COMPUTE | TOKEN_KEYWORD_COMPUTE
| TOKEN_PERFORM | TOKEN_PERFORM
; ;
if_branch : TOKEN_IF expression if_branch : TOKEN_IF op_parms
| TOKEN_ELSE_IF expression | TOKEN_ELSE_IF op_parms
| TOKEN_ELSE statement | TOKEN_ELSE statement
| TOKEN_END_IF | 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 data_space : TOKEN_WORKING_STORAGE
| TOKEN_KEYWORD_SECTION | TOKEN_KEYWORD_SECTION
| TOKEN_DOT | TOKEN_DOT
@ -142,6 +140,7 @@ data_category : TOKEN_ALPHANUMERIC
| TOKEN_IMPLIED_DECIMAL | TOKEN_IMPLIED_DECIMAL
; ;
categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS
| TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS
; ;
complete_category: complete_category complete_category complete_category: complete_category complete_category
| data_category categry_contain | data_category categry_contain

View File

@ -6,7 +6,7 @@ DIGIT [0-9]+
%% %%
(" "|\t|\n) /* skip whitespace */ (" "|\t|\n) /* skip whitespace */
\*>\ ?.* { return TOKEN_COMMENT; } \*>\ ?.*
IDENTIFICATION { return TOKEN_KEYWORD_IDENTIFICATION; } IDENTIFICATION { return TOKEN_KEYWORD_IDENTIFICATION; }
DIVISION { return TOKEN_KEYWORD_DIVISION; } DIVISION { return TOKEN_KEYWORD_DIVISION; }
PROGRAM-ID { return TOKEN_PROGRAM_ID; } PROGRAM-ID { return TOKEN_PROGRAM_ID; }