Quick formatting updates
This commit is contained in:
106
src/grammar.y
106
src/grammar.y
@ -2,30 +2,30 @@
|
||||
/* The Translators - Spring 2025 */
|
||||
|
||||
%{
|
||||
#include <stdio.h>
|
||||
#include "../src/symbol_table.c"
|
||||
#include <math.h>
|
||||
extern int yylex(void);
|
||||
void yyerror(const char *err);
|
||||
extern char* yytext;
|
||||
extern int yyleng;
|
||||
extern int yychar;
|
||||
extern SymbolTable * cur;
|
||||
//char* cur_value;
|
||||
//char* cur_type;
|
||||
int token_tracker;
|
||||
extern int line_number;
|
||||
extern int column_number;
|
||||
extern FILE * yyin;
|
||||
extern TableNode* funprime;
|
||||
extern TableNode* arrayprim;
|
||||
extern TableNode* recprime;
|
||||
extern TableNode* funtypeprime;
|
||||
extern TableNode* integ;
|
||||
extern TableNode* addr;
|
||||
extern TableNode* chara;
|
||||
extern TableNode* stri;
|
||||
extern TableNode* boo;
|
||||
#include <stdio.h>
|
||||
#include "../src/symbol_table.c"
|
||||
#include <math.h>
|
||||
extern int yylex(void);
|
||||
void yyerror(const char *err);
|
||||
extern char* yytext;
|
||||
extern int yyleng;
|
||||
extern int yychar;
|
||||
extern SymbolTable * cur;
|
||||
//char* cur_value;
|
||||
//char* cur_type;
|
||||
int token_tracker;
|
||||
extern int line_number;
|
||||
extern int column_number;
|
||||
extern FILE * yyin;
|
||||
extern TableNode* funprime;
|
||||
extern TableNode* arrayprim;
|
||||
extern TableNode* recprime;
|
||||
extern TableNode* funtypeprime;
|
||||
extern TableNode* integ;
|
||||
extern TableNode* addr;
|
||||
extern TableNode* chara;
|
||||
extern TableNode* stri;
|
||||
extern TableNode* boo;
|
||||
%}
|
||||
//%define api.location.type {location_t}
|
||||
%locations
|
||||
@ -123,7 +123,8 @@ definition:
|
||||
| TYPE ID COLON constant ARROW ID
|
||||
| function_declaration
|
||||
| TYPE ID COLON id_or_types ARROW id_or_types {
|
||||
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));}
|
||||
CreateEntry(cur,funtypeprime,$2,CreateFunctionTypeInfo(table_lookup(cur,$4),table_lookup(cur,$6)));
|
||||
}
|
||||
| ID parameter ASSIGN sblock
|
||||
;
|
||||
|
||||
@ -192,15 +193,12 @@ rec_op :
|
||||
DOT
|
||||
|
||||
expression:
|
||||
|
||||
constant {printf("constant expression\n");} {$$ = $<words>1;}
|
||||
| SUB_OR_NEG expression %prec UMINUS {printf("negative expression\n");if(strcmp($2,"integer") != 0)
|
||||
{printf("cant negate something not an integer at line %d and column %d\n",@2.first_line,@2.first_column);
|
||||
$$=strdup("undefined");}else{$$=$2;}}
|
||||
|
||||
{printf("cant negate something not an integer at line %d and column %d\n",@2.first_line,@2.first_column);
|
||||
$$=strdup("undefined");}else{$$=$2;}}
|
||||
| NOT expression {printf("not expression\n"); if(strcmp($2,"Boolean")==0){$$=$2;}else{$$=strdup("undefined");
|
||||
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
|
||||
|
||||
printf("mismatch at line %d and column %d\n",@1.first_line,@1.first_column);}}
|
||||
| expression ADD expression {printf("add expression\n");}
|
||||
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
|
||||
| expression MUL expression {printf("multiply expression\n");}
|
||||
@ -212,16 +210,16 @@ expression:
|
||||
| expression OR expression {printf("or expression\n");}
|
||||
|
||||
| expression LESS_THAN expression {printf("less than expression\n");if(strcmp($1,$3)==0 &&
|
||||
strcmp($1,"integer")==0){$$=strdup("Boolean");}else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
|
||||
$$=strdup("Boolean");$$=strdup("undefined");}}
|
||||
strcmp($1,"integer")==0){$$=strdup("Boolean");}else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);
|
||||
$$=strdup("Boolean");$$=strdup("undefined");}}
|
||||
| expression EQUAL_TO expression {printf("equals check expression\n");
|
||||
if(strcmp($1,$3)==0){$$=strdup("Boolean");}else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
|
||||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
|
||||
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
|
||||
if(strcmp($1,$3)==0){$$=strdup("Boolean");}else if((strcmp($1,"array")==0||strcmp($1,"record")==0||
|
||||
strcmp($1,"function type primitive")==0) && (strcmp($3,"address")==0)){$$=strdup("Boolean");}
|
||||
else{printf("mismatch at line %d and column %d\n",@2.first_line,@2.first_column);$$=strdup("undefined");}}
|
||||
| assignable {printf("assignable expression\n");$$=$1;}
|
||||
| L_PAREN expression R_PAREN {printf("paren expression\n");$$=$2;}
|
||||
| memOp assignable {$$ = strdup("address");}
|
||||
;
|
||||
;
|
||||
|
||||
|
||||
ablock:
|
||||
@ -250,9 +248,9 @@ constant:
|
||||
;
|
||||
|
||||
types:
|
||||
// Commented out T_String below
|
||||
// T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
// Commented out T_String below
|
||||
// T_STRING {printf("string of T_STRING in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
T_INTEGER {printf("string of T_INTEGER in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
| T_ADDRESS {printf("string of T_ADDRESS in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
| T_CHARACTER {printf("string of T_CHARACTER in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
| T_BOOLEAN {printf("string of T_BOOLEAN in types is %s\n",$<words>1);} {$$ = $<words>1;}
|
||||
@ -261,31 +259,5 @@ types:
|
||||
%%
|
||||
|
||||
void yyerror(const char *err) {
|
||||
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,yylloc.first_line,yylloc.first_column);
|
||||
fprintf(stderr, "ERROR: %s at token %s at line number %d,column number %d\n", err,yytext,yylloc.first_line,yylloc.first_column);
|
||||
}
|
||||
/*
|
||||
int main(int argc, char * argv[]) {
|
||||
token_tracker = 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(cur),f);
|
||||
fclose(f);
|
||||
// break;
|
||||
//}
|
||||
//}
|
||||
return 0;
|
||||
} */
|
||||
|
Reference in New Issue
Block a user