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) */
/* The Translators - Spring 2025 */
/* (referenced Bison manual for file boilerplate [3.1]) */
// Prologue
%{
@ -9,12 +9,13 @@
void yyerror(const char *err);
extern char* yytext;
extern int yychar;
SymbolTable * st;
extern SymbolTable * cur;
//char* cur_value;
//char* cur_type;
int token_tracker;
extern int line_number;
extern int column_number;
extern FILE * yyin;
%}
%union {
@ -22,28 +23,15 @@
char * words;
}
//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
%left LESS_THAN
%left EQUAL_TO
%left AND
%left OR
%left ASSIGN
%type <words> id_or_types
%type <words> types
%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 <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
@ -69,21 +57,32 @@
%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 ADD 601
%token LESS_THAN 606
%token EQUAL_TO 607
%token ASSIGN 608
%token NOT 609
%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 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 EQUAL_TO
%left AND
%left OR
%left ASSIGN
%%
@ -103,15 +102,19 @@ prototype:
definition:
TYPE ID COLON dblock
| TYPE ID COLON constant ARROW ID
| TYPE ID COLON types ARROW ID
| FUNCTION ID COLON ID
| TYPE ID COLON ID 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
;
parameter:
L_PAREN ID R_PAREN
| AS L_PAREN idlist R_PAREN
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
;
idlist:
@ -120,8 +123,8 @@ idlist:
;
sblock:
L_BRACE {st = CreateScope(st,2,2);} statement_list {st = getParent(st);} 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);} 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
;
dblock:
@ -130,21 +133,21 @@ dblock:
declaration_list:
declaration
{printf(
"declaration list a rule encountered");
//CreateEntry(st,cur_type,cur_value);
"found a decleration of form dec;dec_list\n");
//CreateEntry(cur,cur_type,cur_value);
}
SEMI_COLON declaration_list
| declaration
{printf(
"declaration rule b encountered");
//CreateEntry(st,cur_type,cur_value);
"found a decleration of form dec\n");
//CreateEntry(cur,cur_type,cur_value);
}
;
declaration:
ID COLON ID {
id_or_types COLON ID {
CreateEntry(st,strdup($1),strdup($3));
CreateEntry(cur,$1,$3);
// printf("declaration rule encountered");
// if(cur_value != NULL){
// char* delete1 = cur_value;
@ -167,9 +170,12 @@ declaration:
// strcpy(cur_type, $3);
// 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:
compound_statement statement_list
| compound_statement
@ -178,31 +184,53 @@ statement_list:
;
compound_statement:
WHILE L_PAREN expression R_PAREN sblock
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
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
| sblock
;
// Ref Compilers Principles, Techniques, & Tools
simple_statement:
assignable ASSIGN expression
| RETURN expression
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:
ID
| assignable ablock
| assignable DOT ID
| assignable rec_op ID
;
rec_op :
DOT
expression:
constant
| UnaryOperator expression
| SUB_OR_NEG expression %prec UMINUS
| NOT expression
| expression ADD expression
| expression SUB_OR_NEG expression
| 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
//| constant binaryOperator expression
//| ID binaryOperator expression
| L_PAREN expression R_PAREN
| memOp assignable
;
ablock:
L_PAREN argument_list R_PAREN;
@ -211,27 +239,12 @@ argument_list:
| expression
;
UnaryOperator:
SUB_OR_NEG
| NOT
;
memOp:
RESERVE
| RELEASE
;
binaryOperator:
ADD
| SUB_OR_NEG
| MUL
| DIV
| REM
| AND
| OR
| LESS_THAN
| EQUAL_TO
;
constant:
C_STRING
@ -255,22 +268,26 @@ types:
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() {
//char *str = strdup("taco");
//cur_value = NULL;
//cur_type = NULL;
/*
int main(int argc, char * argv[]) {
token_tracker = 1;
st=CreateScope(NULL,1,1);
cur=CreateScope(NULL,1,1);
//int a;
FILE * fp;
if(argc > 1){
fp = fopen(argv[1], "r");
yyin = fp;
} else {
fp = stdin;
yyin = fp;
}
yyparse();
//while ((a = yyparse() != EOF){
// 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(st),f);
print_symbol_table(getAncestor(cur),f);
fclose(f);
// break;
//}

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

View File

@ -31,7 +31,7 @@ extern FILE *yyin;
int arg;
SymbolTable *top;
SymbolTable *curr;
SymbolTable *cur;
// int main(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; }