From f99dfc9b5454caac34c754eaec068c696f66669b Mon Sep 17 00:00:00 2001 From: Meyer Simon Date: Wed, 5 Mar 2025 15:31:01 -0500 Subject: [PATCH] Fixed some of the spacing and added a harder test t#36 --- src/grammar.y | 120 ++++++++++++++++---------------------------------- 1 file changed, 39 insertions(+), 81 deletions(-) diff --git a/src/grammar.y b/src/grammar.y index 3b5359f..d275f94 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1,7 +1,3 @@ -/* Syntax Analyzer with Bison (3.8.1) */ -/* (referenced Bison manual for file boilerplate [3.1]) */ - -// Prologue %{ #include #include "../src/symbol_table.c" @@ -22,7 +18,7 @@ int integ; char * words; } -//precedence order + //need subtraction only here %type id_or_types %type types @@ -72,6 +68,8 @@ %token RESERVE 613 %token RELEASE 614 %token COMMENT 700 + +//precedence order %precedence RESERVE RELEASE %precedence DOT %precedence UMINUS @@ -84,120 +82,83 @@ %left OR %left ASSIGN - %% program: - 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 | prototype | definition ; + prototype: - L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID; + L_PAREN EXTERNAL R_PAREN FUNCTION ID COLON ID; definition: TYPE ID COLON dblock | TYPE ID COLON constant ARROW ID - //| TYPE ID COLON types ARROW ID | function_declaration | TYPE ID COLON id_or_types ARROW id_or_types | ID parameter ASSIGN sblock ; + function_declaration: - FUNCTION { printf("found a function def\n"); }ID COLON ID - | EXTERNAL { printf("found an external function def\n"); }FUNCTION ID COLON ID + FUNCTION ID COLON ID + | EXTERNAL FUNCTION ID COLON ID ; parameter: - L_PAREN { printf("found a param def\n"); } ID R_PAREN - | AS{ printf("found a param_as_list def\n"); } L_PAREN idlist R_PAREN + L_PAREN ID R_PAREN + | AS L_PAREN idlist R_PAREN ; idlist: - ID COMMA idlist - |ID + ID COMMA idlist + | ID ; 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 {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);} statement_list {cur = getParent(cur);} R_BRACE + | L_BRACE {cur = CreateScope(cur,2,2);} dblock statement_list {cur = getParent(cur);} R_BRACE ; dblock: - L_BRACKET declaration_list R_BRACKET; + L_BRACKET declaration_list R_BRACKET; declaration_list: - declaration - {printf( - "found a decleration of form dec;dec_list\n"); - //CreateEntry(cur,cur_type,cur_value); - } - SEMI_COLON declaration_list + declaration SEMI_COLON declaration_list | declaration - {printf( - "found a decleration of form dec\n"); - //CreateEntry(cur,cur_type,cur_value); - } ; declaration: - id_or_types COLON ID { - - 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 COLON ID {CreateEntry(cur,$1,$3); } ; + id_or_types: - ID + ID | types ; + statement_list: - compound_statement statement_list + compound_statement statement_list | compound_statement | simple_statement SEMI_COLON statement_list | simple_statement SEMI_COLON ; compound_statement: - WHILE {printf("found an while statment\n"); } L_PAREN expression R_PAREN sblock - | IF {printf("found an if statment\n"); }L_PAREN expression R_PAREN THEN sblock ELSE sblock + WHILE L_PAREN expression R_PAREN sblock + | IF L_PAREN expression R_PAREN THEN sblock ELSE sblock | sblock ; -// Ref Compilers Principles, Techniques, & Tools - - - - simple_statement: - assignable ASSIGN {printf("found an assignment statment\n"); } expression -// I think that we need to add memOp stuff here | assignable ASSIGN expression {printf("found an assignment statment\n"); } - | RETURN {printf("found an return statment\n"); } expression + assignable ASSIGN expression + | RETURN expression ; assignable: @@ -207,7 +168,7 @@ assignable: ; rec_op : - DOT + DOT expression: constant @@ -223,31 +184,28 @@ expression: | expression LESS_THAN expression | expression EQUAL_TO expression | assignable - //| constant binaryOperator expression - //| ID binaryOperator expression | L_PAREN expression R_PAREN | memOp assignable ; - - ablock: - L_PAREN argument_list R_PAREN; + L_PAREN argument_list R_PAREN + ; argument_list: - expression COMMA argument_list + expression COMMA argument_list | expression ; memOp: - RESERVE + RESERVE | RELEASE ; constant: - C_STRING + C_STRING | C_INTEGER | C_NULL | C_CHARACTER @@ -256,19 +214,19 @@ constant: ; types: - T_STRING + T_STRING | T_INTEGER | T_ADDRESS | T_CHARACTER | T_BOOLEAN ; -// + %% 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); } -/* +/* int main(int argc, char * argv[]) { token_tracker = 1; cur=CreateScope(NULL,1,1); @@ -283,13 +241,13 @@ int main(int argc, char * argv[]) { } yyparse(); //while ((a = yyparse() != EOF){ - // token_tracker++; + // token_tracker++; //printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker); //if(yytext[0] == '\n'){ FILE* f = fdopen(1,"w"); print_symbol_table(getAncestor(cur),f); fclose(f); - // break; + // break; //} //} return 0;