From 052152c251cc1e5a183d43b7b0a898125d1e1322 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 23 Oct 2024 20:44:51 -0700 Subject: [PATCH 1/2] adding hello_test --- lab-3/main_test.c | 73 ++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 39 deletions(-) diff --git a/lab-3/main_test.c b/lab-3/main_test.c index 32a4bbf..946a6bd 100644 --- a/lab-3/main_test.c +++ b/lab-3/main_test.c @@ -18,57 +18,52 @@ struct token_st { char *p; }; -UTEST(scanner, identifier) { - token_t t; - // Must include the null character to terminate input - char string[] = "test\0"; - YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); +// UTEST(scanner, identifier) { +// token_t t; +// // Must include the null character to terminate input +// char string[] = "test\0"; +// YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); - ASSERT_EQ(TOKEN_IDENT, (t = yylex())); - ASSERT_STREQ("test", yytext); +// ASSERT_EQ(TOKEN_EOF, (t = yylex())); +// ASSERT_STREQ("", yytext); - ASSERT_EQ(TOKEN_EOF, (t = yylex())); - ASSERT_STREQ("", yytext); +// yy_delete_buffer(buffer); +// } - yy_delete_buffer(buffer); -} +// UTEST(scanner, assignment) { +// token_t t; +// // Must include the null character to terminate input +// char string[] = "=\0"; +// YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); -UTEST(scanner, assignment) { - token_t t; - // Must include the null character to terminate input - char string[] = "=\0"; - YY_BUFFER_STATE buffer = yy_scan_buffer(string, sizeof(string)); +// ASSERT_EQ(TOKEN_EOF, (t = yylex())); +// ASSERT_STREQ("", yytext); - ASSERT_EQ(TOKEN_ASSIGNMENT, (t = yylex())); - ASSERT_STREQ("=", yytext); - - ASSERT_EQ(TOKEN_EOF, (t = yylex())); - ASSERT_STREQ("", yytext); - - yy_delete_buffer(buffer); -} +// yy_delete_buffer(buffer); +// } UTEST(scanner, hello) { struct token_st tokens[] = { {TOKEN_IDENTIFICATION, "IDENTIFICATION"}, - {TOKEN_PROGRAM_ID, "PROGRAM-ID. HELLO-WORLD."} - {TOKEN_PROCEDURE_DIVISION, "PROCEDURE DIVISION."}, - {TOKEN_STRING, "Hello World!"}, - {TOKEN_KEYWORD_PRINT, "DISPLAY"}, - {TOKEN_EOF, "STOP RUN."}, + {TOKEN_KEYWORD_DIVISION, "DIVISION"}, + {TOKEN_DOT, "."}, + {TOKEN_PROGRAM_ID, "PROGRAM-ID"}, + {TOKEN_DOT, "."}, + {TOKEN_IDENT, "HELLO-WORLD"}, + {TOKEN_DOT, "."}, + {TOKEN_PROCEDURE, "PROCEDURE"}, + {TOKEN_KEYWORD_DIVISION, "DIVISION"}, + {TOKEN_DOT, "."}, + {TOKEN_DISPLAY, "DISPLAY"}, + {TOKEN_STRING, "'Hello World!'"}, + {TOKEN_STOP, "STOP"}, + {TOKEN_RUN, "RUN"}, + {TOKEN_DOT, "."}, + {TOKEN_EOF, ""}, }; -UTEST(scanner, sample) { - struct token_st tokens[] = { - {TOKEN_IDENT, "answer"}, - {TOKEN_ASSIGNMENT, "="}, - {TOKEN_NUMBER, "2020"}, - {TOKEN_ADD, "+"}, - {TOKEN_NUMBER, "4"}, - {TOKEN_EOF, ""} - }; - yyin = fopen("samples/program.c", "r"); + yyin = fopen("samples/hello-world.cbl", "r"); yyrestart(yyin); ASSERT_TRUE(yyin); From 3a36da2f5bba47a7483f7684a53a1a2d0fd53752 Mon Sep 17 00:00:00 2001 From: Josh Date: Thu, 24 Oct 2024 15:28:07 -0700 Subject: [PATCH 2/2] added scanner for hello world and fixed flex --- lab-4/parser.bison | 25 +++++++++++++++- lab-4/scanner.flex | 75 +++++++++++++++++++++++++++++++++++----------- 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/lab-4/parser.bison b/lab-4/parser.bison index 451e66c..ddbc8b4 100644 --- a/lab-4/parser.bison +++ b/lab-4/parser.bison @@ -58,9 +58,32 @@ int yylex(); %token TOKEN_GREATER_THAN %token TOKEN_LESS_THAN %token TOKEN_EXPONENTIAL +%token TOKEN_DISPLAY %% - +file : statements +statements : statements statement + | statement + ; +statement : section + | sect_data + | simple_stmt + ; +section : type TOKEN_KEYWORD_DIVISION TOKEN_DOT + | type TOKEN_RUN TOKEN_DOT + ; +sect_data : TOKEN_PROGRAM_ID TOKEN_DOT TOKEN_IDENT TOKEN_DOT + ; +type : TOKEN_IDENTIFICATION + | TOKEN_PROCEDURE + | TOKEN_STOP + ; +simple_stmt : function + ; +function : TOKEN_DISPLAY parms + ; +parms : TOKEN_STRING + ; %% diff --git a/lab-4/scanner.flex b/lab-4/scanner.flex index 718dee7..4a3e7e4 100644 --- a/lab-4/scanner.flex +++ b/lab-4/scanner.flex @@ -1,25 +1,64 @@ %{ #include "token.h" %} - -%option nounput -%option noinput -%option yylineno - -DIGIT [0-9] -LETTER [a-zA-Z] - +NAME [a-zA-Z]([a-zA-Z0-9_-]*[a-zA-Z0-9])? +DIGIT [0-9]+ %% + (" "|\t|\n) /* skip whitespace */ -\+ { return TOKEN_PLUS; } -\- { return TOKEN_MINUS; } -\* { return TOKEN_MUL; } -\/ { return TOKEN_DIV; } -\( { return TOKEN_LPAREN; } -\) { return TOKEN_RPAREN; } -\; { return TOKEN_SEMI; } -{DIGIT}+ { return TOKEN_INT; } -. { return TOKEN_ERROR; } -%% +\*>\ ?.* { return TOKEN_COMMENT; } +IDENTIFICATION { return TOKEN_IDENTIFICATION; } +DIVISION { return TOKEN_KEYWORD_DIVISION; } +PROGRAM-ID { return TOKEN_PROGRAM_ID; } +PROCEDURE { return TOKEN_PROCEDURE; } +DATA { return TOKEN_KEYWORD_DATA; } +SECTION { return TOKEN_KEYWORD_SECTION; } +WORKING-STORAGE { return TOKEN_WORKING_STORAGE; } +DISPLAY { return TOKEN_DISPLAY; } +STOP { return TOKEN_STOP; } +RUN { return TOKEN_RUN; } +MOVE { return TOKEN_MOVE; } +TO { return TOKEN_KEYWORD_TO; } +VARYING { return TOKEN_VARYING; } +FROM { return TOKEN_KEYWORD_FROM; } +BY { return TOKEN_KEYWORD_BY; } +UNTIL { return TOKEN_UNTIL; } +PERFORM { return TOKEN_PERFORM; } +END-PERFORM { return TOKEN_END_PERFORM; } +IF { return TOKEN_IF; } +END-IF { return TOKEN_END_IF; } +SPACE { return TOKEN_SPACE; } +PIC { return TOKEN_PICTURE; } +OCCURS { return TOKEN_KEYWORD_OCCURS; } +VALUE { return TOKEN_KEYWORD_VALUE; } +COMPUTE { return TOKEN_KEYWORD_COMPUTE; } +FUNCTION { return TOKEN_KEYWORD_FUNCTION; } +X { return TOKEN_ALPHANUMERIC; } +S9 { return TOKEN_SIGNED_NUMERIC; } +9 { return TOKEN_NUMERIC; } +V9 { return TOKEN_IMPLIED_DECIMAL; } +COMP { return TOKEN_COMPUTATION_LEVEL_0; } +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; } +\- { return TOKEN_SUB; } +\*\* { return TOKEN_EXPONENTIAL; } +\* { return TOKEN_MULTIPLY; } +\/ { return TOKEN_DIVIDE; } +\> { return TOKEN_GREATER_THAN; } +\< { return TOKEN_LESS_THAN; } +\= { return TOKEN_EQUAL;} + +"\""[^"]*"\"" { return TOKEN_STRING; } +"\'"[^']*"\'" { return TOKEN_STRING; } +"(" { return TOKEN_LEFT_PARENTHESIS; } +")" { return TOKEN_RIGHT_PARENTHESIS; } + +\. { return TOKEN_DOT; } + +%% int yywrap() { return 1; }