move over lab-5

This commit is contained in:
vel 2024-11-13 20:06:55 -08:00
parent f85d4c3599
commit 74faae2743
Signed by: velvox
GPG Key ID: 59D9762F674151DF
2 changed files with 47 additions and 51 deletions

View File

@ -7,23 +7,27 @@ Simply invoke the parser generated by bison, and then display the output.
#include <stdio.h> #include <stdio.h>
/* Clunky: Declare the parse function generated from parser.bison */ /* 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 */ /* 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[]) { int main(int argc, char *argv[]) {
printf("Lab 6 Example Interpreter Compiler\n"); printf("Lab 6 Example Interpreter Compiler\n");
printf( printf(
"Enter an infix expression using the operators +-*/() ending with ;\n\n"); "Enter an infix expression using the operators +-*/() ending with ;\n\n");
if (yyparse() == 0) { if (yyparse(parser_result) == 0) {
printf("Parse successful: "); printf("Parse successful: ");
stmt_print(parser_result); if (parser_result != NULL) {
printf("\n"); stmt_print(parser_result);
printf("Running the program, results in: "); printf("\n");
stmt_evaluate(parser_result); printf("Running the program, results in: ");
printf("\n"); stmt_evaluate(parser_result);
printf("\n");
}
printf("parser_result is %p\n", parser_result);
return 0; return 0;
} else { } else {
printf("Parse failed!\n"); printf("Parse failed!\n");

View File

@ -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. 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. 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 char *yytext;
extern int yylex(); extern int yylex();
void yyerror(const char*); void yyerror(struct stmt *parser_result, 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;
extern int yylineno; extern int yylineno;
%} %}
%parse-param {struct stmt *parser_result}
%debug %debug
%define parse.error detailed %define parse.error detailed
@ -89,9 +84,7 @@ extern int yylineno;
%% %%
program : statements file : statements
{ parser_result = $1; return 0;}
;
statements : statement_list statements : statement_list
; ;
statement_list : statement_list statement statement_list : statement_list statement
@ -116,15 +109,15 @@ type : TOKEN_KEYWORD_IDENTIFICATION
; ;
simple_stmt : cbl_func_stmt simple_stmt : cbl_func_stmt
| if_branch | if_branch
| else_parts
| perform_stmt | perform_stmt
; ;
cbl_func_stmt : cbl_function cbl_func_stmt : cbl_function
| cbl_function op_parms | cbl_function op_parms
| cbl_function assignment_stmt | cbl_function assignment_stmt
| cbl_function op_parm assignment_stmt | cbl_function op_parm assignment_stmt
; ;
assignment_stmt : TOKEN_EQUAL ext_function assignment_stmt : TOKEN_EQUAL op_parms
| TOKEN_EQUAL op_parms
| TOKEN_KEYWORD_TO op_parms | TOKEN_KEYWORD_TO op_parms
; ;
op_parms : op_parm op_parms : op_parm
@ -132,28 +125,32 @@ op_parms : op_parm
; ;
op_parm : mathmaticalexpr op_parm : mathmaticalexpr
| booleanexpr | booleanexpr
| type_expr ;
term : mathmaticalexpr
;
math_op : TOKEN_ADD
| TOKEN_SUB
| TOKEN_MULTIPLY
| TOKEN_DIVIDE
| TOKEN_EXPONENTIAL
; ;
mathmaticalexpr : type_expr mathmaticalexpr : type_expr
| mathmaticalexpr TOKEN_ADD mathmaticalexpr | mathmaticalexpr math_op term
| mathmaticalexpr TOKEN_SUB mathmaticalexpr
| mathmaticalexpr TOKEN_MULTIPLY mathmaticalexpr
| mathmaticalexpr TOKEN_DIVIDE mathmaticalexpr
| mathmaticalexpr TOKEN_EXPONENTIAL mathmaticalexpr
| container_expr | container_expr
| type_expr 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 booleanexpr : mathmaticalexpr TOKEN_LESS_THAN term
| mathmaticalexpr TOKEN_GREATER_THAN mathmaticalexpr | mathmaticalexpr TOKEN_GREATER_THAN term
| mathmaticalexpr TOKEN_EQUAL mathmaticalexpr | mathmaticalexpr TOKEN_EQUAL term
; ;
type_expr : TOKEN_IDENT type_expr : TOKEN_IDENT
| TOKEN_INTEGER | TOKEN_INTEGER
| TOKEN_STRING | TOKEN_STRING
| TOKEN_SPACE | TOKEN_SPACE
| TOKEN_SUB TOKEN_IDENT | TOKEN_SUB TOKEN_IDENT
| ext_function
; ;
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
; ;
@ -161,20 +158,16 @@ cbl_function : TOKEN_DISPLAY
| TOKEN_MOVE | TOKEN_MOVE
| TOKEN_KEYWORD_COMPUTE | 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 simple_stmt
; | TOKEN_ELSE simple_stmt
else_parts : | TOKEN_END_IF
| TOKEN_ELSE_IF booleanexpr statements else_parts
| TOKEN_ELSE statements
; ;
perform_stmt : TOKEN_PERFORM TOKEN_VARYING TOKEN_IDENT TOKEN_KEYWORD_FROM TOKEN_INTEGER TOKEN_KEYWORD_BY TOKEN_INTEGER TOKEN_UNTIL op_parms 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 | TOKEN_END_PERFORM
; ;
data_space : TOKEN_WORKING_STORAGE data_space : TOKEN_WORKING_STORAGE TOKEN_KEYWORD_SECTION TOKEN_DOT
| TOKEN_KEYWORD_SECTION
| TOKEN_DOT
; ;
data_category : TOKEN_ALPHANUMERIC data_category : TOKEN_ALPHANUMERIC
| TOKEN_NUMERIC | TOKEN_NUMERIC
@ -184,8 +177,8 @@ data_category : TOKEN_ALPHANUMERIC
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 | TOKEN_LEFT_PARENTHESIS TOKEN_IDENT TOKEN_RIGHT_PARENTHESIS
; ;
complete_ctgry : data_category categry_contain complete_category: data_category categry_contain
| data_category categry_contain complete_ctgry | data_category categry_contain complete_category
; ;
data_clause : TOKEN_COMPUTATION_LEVEL_0 data_clause : TOKEN_COMPUTATION_LEVEL_0
| TOKEN_COMPUTATION_LEVEL_1 | TOKEN_COMPUTATION_LEVEL_1
@ -194,24 +187,23 @@ data_clause : TOKEN_COMPUTATION_LEVEL_0
| TOKEN_KEYWORD_VALUE | TOKEN_KEYWORD_VALUE
| TOKEN_KEYWORD_OCCURS | TOKEN_KEYWORD_OCCURS
; ;
full_data_clause: data_clause full_data_clause: data_clause data_clause
| data_clause full_data_clause | data_clause
; ;
simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT simple_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_DOT
; ;
complex_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE category_spec TOKEN_DOT complex_decl : TOKEN_INTEGER TOKEN_IDENT TOKEN_PICTURE category_spec TOKEN_DOT
; ;
category_spec : complete_ctgry category_spec : complete_category
| complete_ctgry data_clauses | complete_category data_clauses
; ;
data_clauses : full_data_clause data_clauses : full_data_clause
| full_data_clause TOKEN_INTEGER | full_data_clause TOKEN_INTEGER
; ;
data_declaration: simple_decl 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); fprintf(stderr, "Error | Line: %d\n%s\n",yylineno,msg);
} }