runner and parser together add entries ok

This commit is contained in:
Partho Bhattacharya
2025-03-05 15:02:30 -05:00
parent c2f28b6836
commit f819a68ef7
5 changed files with 211 additions and 194 deletions

View File

@ -1,5 +1,5 @@
/* Syntax Analyzer with Bison (3.8.1) */ /* Syntax Analyzer with Bison (3.8.1) */
/* The Translators - Spring 2025 */ /* (referenced Bison manual for file boilerplate [3.1]) */
// Prologue // Prologue
%{ %{
@ -9,148 +9,151 @@
void yyerror(const char *err); void yyerror(const char *err);
extern char* yytext; extern char* yytext;
extern int yychar; extern int yychar;
SymbolTable * st; extern SymbolTable * cur;
//char* cur_value; //char* cur_value;
//char* cur_type; //char* cur_type;
int token_tracker; int token_tracker;
extern int line_number; extern int line_number;
extern int column_number; extern int column_number;
extern FILE * yyin;
%} %}
%union { %union {
int integ; int integ;
char * words; char * words;
} }
//precedence order //precedence order
%precedence RESERVE
%precedence RELEASE
%precedence DOT
%precedence SUB_OR_NEG
%precedence NOT
%left MUL
%left DIV
%left REM
%left ADD
//need subtraction only here //need subtraction only here
%type <words> id_or_types
%type <words> types
%token <words> ID 101
%token <words> T_INTEGER 201
%token <words> T_ADDRESS 202
%token <words> T_BOOLEAN 203
%token <words> T_CHARACTER 204
%token <words> T_STRING 205
%token C_INTEGER 301
%token C_NULL 302
%token C_CHARACTER 303
%token C_STRING 304
%token C_TRUE 305
%token C_FALSE 306
%token WHILE 401
%token IF 402
%token THEN 403
%token ELSE 404
%token TYPE 405
%token FUNCTION 406
%token RETURN 407
%token EXTERNAL 408
%token AS 409
%token L_PAREN 501
%token R_PAREN 502
%token L_BRACKET 503
%token R_BRACKET 504
%token L_BRACE 505
%token R_BRACE 506
%token SEMI_COLON 507
%token COLON 508
%token COMMA 509
%token ARROW 510
%token MUL 603
%token DIV 604
%token REM 605
%token ADD 601
%token LESS_THAN 606
%token EQUAL_TO 607
%token AND 610
%token OR 611
%token ASSIGN 608
%token SUB_OR_NEG 602
%token NOT 609
%token DOT 612
%token RESERVE 613
%token RELEASE 614
%token COMMENT 700
%precedence RESERVE RELEASE
%precedence DOT
%precedence UMINUS
%precedence NOT
%left MUL DIV REM
%left ADD SUB_OR_NEG
%left LESS_THAN %left LESS_THAN
%left EQUAL_TO %left EQUAL_TO
%left AND %left AND
%left OR %left OR
%left ASSIGN %left ASSIGN
%token <words> ID 101
%token T_INTEGER 201
%token T_ADDRESS 202
%token T_BOOLEAN 203
%token T_CHARACTER 204
%token T_STRING 205
%token C_INTEGER 301
%token C_NULL 302
%token C_CHARACTER 303
%token C_STRING 304
%token C_TRUE 305
%token C_FALSE 306
%token WHILE 401
%token IF 402
%token THEN 403
%token ELSE 404
%token TYPE 405
%token FUNCTION 406
%token RETURN 407
%token EXTERNAL 408
%token AS 409
%token L_PAREN 501
%token R_PAREN 502
%token L_BRACKET 503
%token R_BRACKET 504
%token L_BRACE 505
%token R_BRACE 506
%token SEMI_COLON 507
%token COLON 508
%token COMMA 509
%token ARROW 510
/* %token ADD 601
%token SUB_OR_NEG 602
%token MUL 603
%token DIV 604
%token REM 605
%token LESS_THAN 606
%token EQUAL_TO 607
%token ASSIGN 608
%token NOT 609
%token AND 610
%token OR 611
%token DOT 612
%token RESERVE 613
%token RELEASE 614 */
%token COMMENT 700
%% %%
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 //| TYPE ID COLON types ARROW ID
| FUNCTION ID COLON ID | function_declaration
| TYPE ID COLON ID ARROW ID | TYPE ID COLON id_or_types ARROW id_or_types
| ID parameter ASSIGN sblock | 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
;
parameter: parameter:
L_PAREN ID R_PAREN L_PAREN { printf("found a param def\n"); } ID R_PAREN
| AS L_PAREN idlist R_PAREN | AS{ printf("found a param_as_list def\n"); } L_PAREN idlist R_PAREN
; ;
idlist: idlist:
ID COMMA idlist ID COMMA idlist
|ID |ID
; ;
sblock: sblock:
L_BRACE {st = CreateScope(st,2,2);} statement_list {st = getParent(st);} R_BRACE L_BRACE {printf("open brace\n"); cur = CreateScope(cur,2,2);} statement_list {printf("close brace\n"); cur = getParent(cur);} R_BRACE
| L_BRACE {st = CreateScope(st,2,2);} dblock statement_list {st = getParent(st);} 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
; ;
dblock: dblock:
L_BRACKET declaration_list R_BRACKET; L_BRACKET declaration_list R_BRACKET;
declaration_list: declaration_list:
declaration declaration
{printf( {printf(
"declaration list a rule encountered"); "found a decleration of form dec;dec_list\n");
//CreateEntry(st,cur_type,cur_value); //CreateEntry(cur,cur_type,cur_value);
} }
SEMI_COLON declaration_list SEMI_COLON declaration_list
| declaration | declaration
{printf( {printf(
"declaration rule b encountered"); "found a decleration of form dec\n");
//CreateEntry(st,cur_type,cur_value); //CreateEntry(cur,cur_type,cur_value);
} }
; ;
declaration: declaration:
ID COLON ID { id_or_types COLON ID {
CreateEntry(st,strdup($1),strdup($3)); CreateEntry(cur,$1,$3);
// printf("declaration rule encountered"); // printf("declaration rule encountered");
// if(cur_value != NULL){ // if(cur_value != NULL){
// char* delete1 = cur_value; // char* delete1 = cur_value;
// printf("delete1 var assigned to cur_value"); // printf("delete1 var assigned to cur_value");
// free(delete1); // free(delete1);
// printf("delete1 var freed"); // printf("delete1 var freed");
// } // }
// if(cur_type != NULL){ // if(cur_type != NULL){
// char* delete2 = cur_type; // char* delete2 = cur_type;
@ -161,119 +164,133 @@ declaration:
// printf("space allocated"); // printf("space allocated");
// strcpy(cur_value, $1); // strcpy(cur_value, $1);
// printf("string copied over"); // printf("string copied over");
// len = strlen($3); // len = strlen($3);
// cur_type = malloc(len + 1); // cur_type = malloc(len + 1);
// strcpy(cur_type, $3); // strcpy(cur_type, $3);
// printf("value var is %s type var is %s\n",cur_value,cur_type); // printf("value var is %s type var is %s\n",cur_value,cur_type);
} }
| types COLON ID // | types COLON ID
; ;
id_or_types:
ID
| 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 L_PAREN expression R_PAREN sblock WHILE {printf("found an while statment\n"); } L_PAREN expression R_PAREN sblock
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock | IF {printf("found an if statment\n"); }L_PAREN expression R_PAREN THEN sblock ELSE sblock
| sblock | sblock
; ;
// Ref Compilers Principles, Techniques, & Tools
simple_statement: simple_statement:
assignable ASSIGN expression assignable ASSIGN {printf("found an assignment statment\n"); } expression
| RETURN 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: assignable:
ID ID
| assignable ablock | assignable ablock
| assignable DOT ID | assignable rec_op ID
; ;
rec_op :
DOT
expression: expression:
constant constant
| UnaryOperator expression | SUB_OR_NEG expression %prec UMINUS
| assignable | NOT expression
| constant binaryOperator expression | expression ADD expression
| L_PAREN expression R_PAREN | expression SUB_OR_NEG expression
| memOp assignable | expression MUL expression
; | expression DIV expression
| expression REM expression
| expression AND expression
| expression OR expression
| expression LESS_THAN expression
| expression EQUAL_TO expression
| assignable
//| constant binaryOperator expression
//| ID binaryOperator expression
| L_PAREN expression R_PAREN
| 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
; ;
UnaryOperator:
SUB_OR_NEG
| NOT
;
memOp: memOp:
RESERVE RESERVE
| RELEASE | RELEASE
; ;
binaryOperator:
ADD
| SUB_OR_NEG
| MUL
| DIV
| REM
| AND
| OR
| LESS_THAN
| EQUAL_TO
;
constant: constant:
C_STRING C_STRING
| C_INTEGER | C_INTEGER
| C_NULL | C_NULL
| C_CHARACTER | C_CHARACTER
| C_TRUE | C_TRUE
| C_FALSE | C_FALSE
; ;
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 main(int argc, char * argv[]) {
//char *str = strdup("taco"); token_tracker = 1;
//cur_value = NULL; cur=CreateScope(NULL,1,1);
//cur_type = NULL; //int a;
FILE * fp;
token_tracker = 1; if(argc > 1){
st=CreateScope(NULL,1,1); fp = fopen(argv[1], "r");
//int a; yyin = fp;
yyparse(); } else {
//while ((a = yyparse() != EOF){ fp = stdin;
// token_tracker++; yyin = fp;
//printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker); }
//if(yytext[0] == '\n'){ yyparse();
FILE* f = fdopen(1,"w"); //while ((a = yyparse() != EOF){
print_symbol_table(getAncestor(st),f); // token_tracker++;
fclose(f); //printf("%d = a: yytext = %s: yychar = %d, token number: %d\n", a, yytext, yychar,token_tracker);
// break; //if(yytext[0] == '\n'){
//} FILE* f = fdopen(1,"w");
//} print_symbol_table(getAncestor(cur),f);
return 0; fclose(f);
// break;
//}
//}
return 0;
} */ } */

View File

@ -63,7 +63,7 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
{DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}} {DIGIT}+ {if(DEBUG) {printf( "C_INTEGER: %s (%d)\n", yytext, C_INTEGER);} else {return C_INTEGER;}}
'{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}} '{CHAR}' {if(DEBUG) {printf( "C_CHARACTER: %s (%d)\n", yytext, C_CHARACTER);} else {return C_CHARACTER;}}
\"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}} \"{SCHAR}*\" {if(DEBUG) {printf( "C_STRING: %s (%d)\n", yytext, C_STRING);} else {return C_STRING;}}
{COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {return COMMENT;}} {COMMENT} {if(DEBUG) {printf( "COMMENT: %s (%d)\n", yytext, COMMENT);} else {/*return COMMENT;*/}}
"(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}} "(" {if(DEBUG) {printf( "L_PAREN: %s (%d)\n", yytext, L_PAREN);} else {return L_PAREN;}}
")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}} ")" {if(DEBUG) {printf( "R_PAREN: %s (%d)\n", yytext, R_PAREN);} else {return R_PAREN;}}

View File

@ -59,7 +59,7 @@ int check_flag(char *arg, char *alpha) {
int run(FILE *alpha) { int run(FILE *alpha) {
int token; int token;
curr = CreateScope(NULL, 1, 1); cur = CreateScope(NULL, 1, 1);
// If file is not found // If file is not found
if (alpha == NULL) { if (alpha == NULL) {
@ -108,7 +108,7 @@ int run(FILE *alpha) {
yyparse(); yyparse();
FILE *f = fdopen(1, "w"); FILE *f = fdopen(1, "w");
print_symbol_table(getAncestor(curr), f); print_symbol_table(getAncestor(cur), f);
fclose(f); fclose(f);
if (yyin != NULL) { if (yyin != NULL) {
@ -179,12 +179,12 @@ int is_alpha_file(char *alpha, int file_len) {
} }
void enter_scope(int line, int column) { void enter_scope(int line, int column) {
curr = CreateScope(curr, line, column); cur = CreateScope(cur, line, column);
} }
void exit_scope() { void exit_scope() {
if (curr->Parent_Scope == NULL) { if (cur->Parent_Scope == NULL) {
printf("Can't close top"); printf("Can't close top");
return; return;
} }
curr = curr->Parent_Scope; cur = cur->Parent_Scope;
} }

View File

@ -31,7 +31,7 @@ extern FILE *yyin;
int arg; int arg;
SymbolTable *top; SymbolTable *top;
SymbolTable *curr; SymbolTable *cur;
// int main(int argc, char* argv[]); // int main(int argc, char* argv[]);
char *is_tok(int argc, char *argv[]); char *is_tok(int argc, char *argv[]);

View File

@ -1 +1 @@
type rec: [integer: x; integer: y] type T1: integer -> integer type T2: rec -> integer function foo : T1 function bar1 : T2 function bar2 : T2 foo(x) := { return x * x; } bar1(a) := { return a.x * a.y; } bar2 as (r,s) := { return r * s; } entry(arg) := { [ integer: result ; rec: w] result := foo(5); w := reserve(w); w.x := 5; w.y := 7; result := bar1(w); result := bar2(5,7); return 0; } type rec: [integer: x; integer: y] type T1: integer -> integer type T2: rec -> integer function foo : T1 function bar1 : T2 function bar2 : T2 foo(x) := { return x * x; } bar1(a) := { return a.x * a.y; } bar2 as (r,s) := { return r * s; } entry(arg) := { [ integer: result ; rec: w] result := foo(5); w := reserve w; w.x := 5; w.y := 7; result := bar1(w); result := bar2(5,7); return 0; }