From 74faae27436c3e26e09624aa8f2e436f760dfda6 Mon Sep 17 00:00:00 2001 From: Riley Smith Date: Wed, 13 Nov 2024 20:06:55 -0800 Subject: [PATCH] move over lab-5 --- lab-5/main.c | 20 +++++++----- lab-5/parser.bison | 78 +++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/lab-5/main.c b/lab-5/main.c index 63208e9..e464500 100644 --- a/lab-5/main.c +++ b/lab-5/main.c @@ -7,23 +7,27 @@ Simply invoke the parser generated by bison, and then display the output. #include /* Clunky: Declare the parse function generated from parser.bison */ -extern int yyparse(); +extern int yyparse(struct stmt *parser_result); /* Clunky: Declare the result of the parser from parser.bison */ -extern struct stmt *parser_result; + +struct stmt *parser_result; int main(int argc, char *argv[]) { printf("Lab 6 Example Interpreter Compiler\n"); printf( "Enter an infix expression using the operators +-*/() ending with ;\n\n"); - if (yyparse() == 0) { + if (yyparse(parser_result) == 0) { printf("Parse successful: "); - stmt_print(parser_result); - printf("\n"); - printf("Running the program, results in: "); - stmt_evaluate(parser_result); - printf("\n"); + if (parser_result != NULL) { + stmt_print(parser_result); + printf("\n"); + printf("Running the program, results in: "); + stmt_evaluate(parser_result); + printf("\n"); + } + printf("parser_result is %p\n", parser_result); return 0; } else { printf("Parse failed!\n"); diff --git a/lab-5/parser.bison b/lab-5/parser.bison index 999a159..4e91288 100644 --- a/lab-5/parser.bison +++ b/lab-5/parser.bison @@ -11,7 +11,7 @@ YYSTYPE is the lexical value returned by each rule in a bison grammar. By default, it is an integer. In this example, we are returning a pointer to an expression. */ -#define YYSTYPE struct expr * +#define YYSTYPE struct stmt * /* Clunky: Manually declare the interface to the scanner generated by flex. @@ -19,19 +19,14 @@ Clunky: Manually declare the interface to the scanner generated by flex. extern char *yytext; extern int yylex(); -void yyerror(const char*); - -/* -Clunky: Keep the final result of the parse in a global variable, -so that it can be retrieved by main(). -*/ - -struct expr * parser_result = 0; +void yyerror(struct stmt *parser_result, const char*); extern int yylineno; %} +%parse-param {struct stmt *parser_result} + %debug %define parse.error detailed @@ -89,9 +84,7 @@ extern int yylineno; %% -program : statements - { parser_result = $1; return 0;} - ; +file : statements statements : statement_list ; statement_list : statement_list statement @@ -116,15 +109,15 @@ type : TOKEN_KEYWORD_IDENTIFICATION ; simple_stmt : cbl_func_stmt | if_branch + | else_parts | perform_stmt ; cbl_func_stmt : cbl_function - | cbl_function op_parms + | cbl_function op_parms | cbl_function assignment_stmt | cbl_function op_parm assignment_stmt ; -assignment_stmt : TOKEN_EQUAL ext_function - | TOKEN_EQUAL op_parms +assignment_stmt : TOKEN_EQUAL op_parms | TOKEN_KEYWORD_TO op_parms ; op_parms : op_parm @@ -132,28 +125,32 @@ op_parms : op_parm ; op_parm : mathmaticalexpr | booleanexpr - | type_expr + ; +term : mathmaticalexpr + ; +math_op : TOKEN_ADD + | TOKEN_SUB + | TOKEN_MULTIPLY + | TOKEN_DIVIDE + | TOKEN_EXPONENTIAL ; mathmaticalexpr : type_expr - | mathmaticalexpr TOKEN_ADD mathmaticalexpr - | mathmaticalexpr TOKEN_SUB mathmaticalexpr - | mathmaticalexpr TOKEN_MULTIPLY mathmaticalexpr - | mathmaticalexpr TOKEN_DIVIDE mathmaticalexpr - | mathmaticalexpr TOKEN_EXPONENTIAL mathmaticalexpr + | mathmaticalexpr math_op term | container_expr | type_expr container_expr ; -container_expr : TOKEN_LEFT_PARENTHESIS op_parms TOKEN_RIGHT_PARENTHESIS +container_expr : TOKEN_LEFT_PARENTHESIS mathmaticalexpr TOKEN_RIGHT_PARENTHESIS ; -booleanexpr : mathmaticalexpr TOKEN_LESS_THAN mathmaticalexpr - | mathmaticalexpr TOKEN_GREATER_THAN mathmaticalexpr - | mathmaticalexpr TOKEN_EQUAL mathmaticalexpr +booleanexpr : mathmaticalexpr TOKEN_LESS_THAN term + | mathmaticalexpr TOKEN_GREATER_THAN term + | mathmaticalexpr TOKEN_EQUAL term ; type_expr : TOKEN_IDENT | TOKEN_INTEGER | TOKEN_STRING | TOKEN_SPACE | TOKEN_SUB TOKEN_IDENT + | ext_function ; ext_function : TOKEN_KEYWORD_FUNCTION TOKEN_IDENT TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; @@ -161,20 +158,16 @@ cbl_function : TOKEN_DISPLAY | TOKEN_MOVE | TOKEN_KEYWORD_COMPUTE ; -if_branch : if_start statements else_parts +if_branch : TOKEN_IF booleanexpr ; -if_start : TOKEN_IF booleanexpr - ; -else_parts : - | TOKEN_ELSE_IF booleanexpr statements else_parts - | TOKEN_ELSE statements +else_parts : TOKEN_ELSE_IF booleanexpr simple_stmt + | TOKEN_ELSE simple_stmt + | 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 +data_space : TOKEN_WORKING_STORAGE TOKEN_KEYWORD_SECTION TOKEN_DOT ; data_category : TOKEN_ALPHANUMERIC | TOKEN_NUMERIC @@ -184,8 +177,8 @@ data_category : TOKEN_ALPHANUMERIC categry_contain : TOKEN_LEFT_PARENTHESIS TOKEN_INTEGER TOKEN_RIGHT_PARENTHESIS | TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS ; -complete_ctgry : data_category categry_contain - | data_category categry_contain complete_ctgry +complete_category: data_category categry_contain + | data_category categry_contain complete_category ; data_clause : TOKEN_COMPUTATION_LEVEL_0 | TOKEN_COMPUTATION_LEVEL_1 @@ -194,24 +187,23 @@ data_clause : TOKEN_COMPUTATION_LEVEL_0 | TOKEN_KEYWORD_VALUE | TOKEN_KEYWORD_OCCURS ; -full_data_clause: data_clause - | data_clause full_data_clause +full_data_clause: data_clause data_clause + | data_clause ; simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT ; complex_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE category_spec TOKEN_DOT ; -category_spec : complete_ctgry - | complete_ctgry data_clauses +category_spec : complete_category + | complete_category data_clauses ; data_clauses : full_data_clause | full_data_clause TOKEN_INTEGER ; data_declaration: simple_decl - | complex_decl`` + | complex_decl ; - %% -void yyerror(const char* msg) { +void yyerror(struct stmt *parser_result, const char* msg) { fprintf(stderr, "Error | Line: %d\n%s\n",yylineno,msg); }