Fixed some of the spacing and added a harder test t#36

This commit is contained in:
Meyer Simon
2025-03-05 15:31:01 -05:00
parent f819a68ef7
commit f99dfc9b54

View File

@ -1,7 +1,3 @@
/* Syntax Analyzer with Bison (3.8.1) */
/* (referenced Bison manual for file boilerplate [3.1]) */
// Prologue
%{ %{
#include <stdio.h> #include <stdio.h>
#include "../src/symbol_table.c" #include "../src/symbol_table.c"
@ -22,7 +18,7 @@
int integ; int integ;
char * words; char * words;
} }
//precedence order
//need subtraction only here //need subtraction only here
%type <words> id_or_types %type <words> id_or_types
%type <words> types %type <words> types
@ -72,6 +68,8 @@
%token RESERVE 613 %token RESERVE 613
%token RELEASE 614 %token RELEASE 614
%token COMMENT 700 %token COMMENT 700
//precedence order
%precedence RESERVE RELEASE %precedence RESERVE RELEASE
%precedence DOT %precedence DOT
%precedence UMINUS %precedence UMINUS
@ -84,120 +82,83 @@
%left OR %left OR
%left ASSIGN %left ASSIGN
%% %%
program: program:
prototype_or_definition_list; prototype_or_definition_list
;
prototype_or_definition_list: prototype_or_definition_list:
prototype prototype_or_definition_list prototype prototype_or_definition_list
| definition prototype_or_definition_list | definition prototype_or_definition_list
| prototype | prototype
| definition | definition
; ;
prototype: prototype:
L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID; L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID;
definition: definition:
TYPE ID COLON dblock TYPE ID COLON dblock
| TYPE ID COLON constant ARROW ID | TYPE ID COLON constant ARROW ID
//| TYPE ID COLON types ARROW ID
| function_declaration | function_declaration
| TYPE ID COLON id_or_types ARROW id_or_types | TYPE ID COLON id_or_types ARROW id_or_types
| ID parameter ASSIGN sblock | ID parameter ASSIGN sblock
; ;
function_declaration: function_declaration:
FUNCTION { printf("found a function def\n"); }ID COLON ID FUNCTION ID COLON ID
| EXTERNAL { printf("found an external function def\n"); }FUNCTION ID COLON ID | EXTERNAL FUNCTION ID COLON ID
; ;
parameter: parameter:
L_PAREN { printf("found a param def\n"); } ID R_PAREN L_PAREN ID R_PAREN
| AS{ printf("found a param_as_list def\n"); } L_PAREN idlist R_PAREN | AS L_PAREN idlist R_PAREN
; ;
idlist: idlist:
ID COMMA idlist ID COMMA idlist
|ID | ID
; ;
sblock: sblock:
L_BRACE {printf("open brace\n"); cur = CreateScope(cur,2,2);} statement_list {printf("close brace\n"); cur = getParent(cur);} R_BRACE L_BRACE {cur = CreateScope(cur,2,2);} statement_list {cur = getParent(cur);} R_BRACE
| L_BRACE {printf("open brace\n"); cur = CreateScope(cur,2,2);} dblock statement_list {printf("close brace\n"); cur = getParent(cur);} R_BRACE | L_BRACE {cur = CreateScope(cur,2,2);} dblock statement_list {cur = getParent(cur);} R_BRACE
; ;
dblock: dblock:
L_BRACKET declaration_list R_BRACKET; L_BRACKET declaration_list R_BRACKET;
declaration_list: declaration_list:
declaration declaration SEMI_COLON declaration_list
{printf(
"found a decleration of form dec;dec_list\n");
//CreateEntry(cur,cur_type,cur_value);
}
SEMI_COLON declaration_list
| declaration | declaration
{printf(
"found a decleration of form dec\n");
//CreateEntry(cur,cur_type,cur_value);
}
; ;
declaration: declaration:
id_or_types COLON ID { id_or_types COLON ID {CreateEntry(cur,$1,$3); }
CreateEntry(cur,$1,$3);
// printf("declaration rule encountered");
// if(cur_value != NULL){
// char* delete1 = cur_value;
// printf("delete1 var assigned to cur_value");
// free(delete1);
// printf("delete1 var freed");
// }
// if(cur_type != NULL){
// char* delete2 = cur_type;
// free(delete2);}
// int len = strlen($1);
// printf("length determined");
// cur_value = malloc(len + 1);
// printf("space allocated");
// strcpy(cur_value, $1);
// printf("string copied over");
// len = strlen($3);
// cur_type = malloc(len + 1);
// strcpy(cur_type, $3);
// printf("value var is %s type var is %s\n",cur_value,cur_type);
}
// | types COLON ID
; ;
id_or_types: id_or_types:
ID ID
| types | types
; ;
statement_list: statement_list:
compound_statement statement_list compound_statement statement_list
| compound_statement | compound_statement
| simple_statement SEMI_COLON statement_list | simple_statement SEMI_COLON statement_list
| simple_statement SEMI_COLON | simple_statement SEMI_COLON
; ;
compound_statement: compound_statement:
WHILE {printf("found an while statment\n"); } L_PAREN expression R_PAREN sblock WHILE L_PAREN expression R_PAREN sblock
| IF {printf("found an if statment\n"); }L_PAREN expression R_PAREN THEN sblock ELSE sblock | IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
| sblock | sblock
; ;
// Ref Compilers Principles, Techniques, & Tools
simple_statement: simple_statement:
assignable ASSIGN {printf("found an assignment statment\n"); } expression assignable ASSIGN expression
// I think that we need to add memOp stuff here | assignable ASSIGN expression {printf("found an assignment statment\n"); } | RETURN expression
| RETURN {printf("found an return statment\n"); } expression
; ;
assignable: assignable:
@ -207,7 +168,7 @@ assignable:
; ;
rec_op : rec_op :
DOT DOT
expression: expression:
constant constant
@ -223,31 +184,28 @@ expression:
| expression LESS_THAN expression | expression LESS_THAN expression
| expression EQUAL_TO expression | expression EQUAL_TO expression
| assignable | assignable
//| constant binaryOperator expression
//| ID binaryOperator expression
| L_PAREN expression R_PAREN | L_PAREN expression R_PAREN
| memOp assignable | memOp assignable
; ;
ablock: ablock:
L_PAREN argument_list R_PAREN; L_PAREN argument_list R_PAREN
;
argument_list: argument_list:
expression COMMA argument_list expression COMMA argument_list
| expression | expression
; ;
memOp: memOp:
RESERVE RESERVE
| RELEASE | RELEASE
; ;
constant: constant:
C_STRING C_STRING
| C_INTEGER | C_INTEGER
| C_NULL | C_NULL
| C_CHARACTER | C_CHARACTER
@ -256,19 +214,19 @@ constant:
; ;
types: types:
T_STRING T_STRING
| T_INTEGER | T_INTEGER
| T_ADDRESS | T_ADDRESS
| T_CHARACTER | T_CHARACTER
| T_BOOLEAN | T_BOOLEAN
; ;
//
%% %%
void yyerror(const char *err) { void yyerror(const char *err) {
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,line_number,column_number); fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,line_number,column_number);
} }
/* /*
int main(int argc, char * argv[]) { int main(int argc, char * argv[]) {
token_tracker = 1; token_tracker = 1;
cur=CreateScope(NULL,1,1); cur=CreateScope(NULL,1,1);
@ -283,13 +241,13 @@ int main(int argc, char * argv[]) {
} }
yyparse(); yyparse();
//while ((a = yyparse() != EOF){ //while ((a = yyparse() != EOF){
// token_tracker++; // token_tracker++;
//printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker); //printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker);
//if(yytext[0] == '\n'){ //if(yytext[0] == '\n'){
FILE* f = fdopen(1,"w"); FILE* f = fdopen(1,"w");
print_symbol_table(getAncestor(cur),f); print_symbol_table(getAncestor(cur),f);
fclose(f); fclose(f);
// break; // break;
//} //}
//} //}
return 0; return 0;