275 lines
7.6 KiB
Plaintext
275 lines
7.6 KiB
Plaintext
/* Syntax Analyzer with Bison (3.8.2) */
|
|
/* 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* integ;
|
|
extern TableNode* addr;
|
|
extern TableNode* chara;
|
|
extern TableNode* stri;
|
|
extern TableNode* boo;
|
|
%}
|
|
//%define api.location.type {location_t}
|
|
%locations
|
|
%union {
|
|
int integ;
|
|
char * words;
|
|
}
|
|
|
|
|
|
%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 <words> C_INTEGER 301
|
|
%token <words> C_NULL 302
|
|
%token <words> C_CHARACTER 303
|
|
%token <words> C_STRING 304
|
|
%token <words> C_TRUE 305
|
|
%token <words> 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 order
|
|
%left ASSIGN
|
|
%left OR
|
|
%left AND
|
|
%left EQUAL_TO
|
|
%left LESS_THAN
|
|
%left ADD SUB_OR_NEG
|
|
%left MUL DIV REM
|
|
%precedence NOT
|
|
%precedence UMINUS
|
|
%precedence DOT
|
|
%precedence RESERVE RELEASE
|
|
|
|
|
|
|
|
%%
|
|
|
|
program:
|
|
prototype_or_definition_list
|
|
;
|
|
|
|
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;
|
|
|
|
definition:
|
|
TYPE ID COLON dblock
|
|
| TYPE ID COLON constant ARROW ID
|
|
| function_declaration
|
|
| TYPE ID COLON id_or_types ARROW id_or_types
|
|
| ID parameter ASSIGN sblock
|
|
;
|
|
|
|
function_declaration:
|
|
FUNCTION ID COLON ID
|
|
| EXTERNAL FUNCTION ID COLON ID
|
|
;
|
|
|
|
parameter:
|
|
L_PAREN ID R_PAREN
|
|
| AS L_PAREN idlist R_PAREN
|
|
;
|
|
|
|
idlist:
|
|
ID COMMA idlist
|
|
| ID
|
|
;
|
|
|
|
sblock:
|
|
L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);} statement_list {cur = getParent(cur);} R_BRACE
|
|
| L_BRACE {cur = CreateScope(cur,@1.first_line,@1.first_column);} dblock {printf("seen sblock with dblock\n");} statement_list {cur = getParent(cur);} R_BRACE
|
|
;
|
|
|
|
dblock:
|
|
L_BRACKET declaration_list R_BRACKET;
|
|
|
|
declaration_list:
|
|
declaration SEMI_COLON declaration_list
|
|
| declaration
|
|
;
|
|
|
|
declaration:
|
|
id_or_types COLON ID {CreateEntry(cur,$<words>1,$<words>3); }
|
|
;
|
|
|
|
id_or_types:
|
|
ID {printf("string of id in id_or_type is %s\n",$<words>1);} {$$ = $<words>1;}
|
|
| types {printf("string of type in id_or_type is %s\n",$<words>1);} {$$ = $<words>1;}
|
|
;
|
|
|
|
statement_list:
|
|
compound_statement statement_list
|
|
| compound_statement
|
|
| simple_statement SEMI_COLON statement_list
|
|
| simple_statement SEMI_COLON
|
|
;
|
|
|
|
compound_statement:
|
|
WHILE L_PAREN expression R_PAREN sblock
|
|
| IF L_PAREN expression R_PAREN THEN sblock ELSE sblock
|
|
| sblock //{printf("seen a compound statement rule\n");}
|
|
;
|
|
|
|
simple_statement:
|
|
assignable ASSIGN expression
|
|
| RETURN expression
|
|
;
|
|
|
|
assignable:
|
|
ID
|
|
| assignable ablock
|
|
| assignable rec_op ID
|
|
;
|
|
|
|
rec_op :
|
|
DOT
|
|
|
|
expression:
|
|
|
|
constant {printf("constant expression\n");} {$$ = $<words>1;}
|
|
| SUB_OR_NEG expression {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);}} %prec UMINUS {printf("negative expression\n");}
|
|
|
|
| NOT expression {printf("not expression\n");}
|
|
| expression ADD expression {printf("add expression\n");}
|
|
| expression SUB_OR_NEG expression {printf("subtract expression\n");}
|
|
| expression MUL expression {printf("multiply expression\n");}
|
|
| expression DIV expression {printf("division expression\n");}
|
|
| expression REM expression {printf("remainder expression\n");}
|
|
| expression AND expression {printf("and expression\n");}
|
|
| expression OR expression {printf("or expression\n");}
|
|
| expression LESS_THAN expression {printf("less than expression\n");}
|
|
| expression EQUAL_TO expression {printf("equals check expression\n");}
|
|
| assignable {printf("assignable expression\n");}
|
|
| L_PAREN expression R_PAREN {printf("paren expression\n");}
|
|
| memOp assignable
|
|
;
|
|
|
|
|
|
ablock:
|
|
L_PAREN argument_list R_PAREN
|
|
;
|
|
|
|
argument_list:
|
|
expression COMMA argument_list
|
|
| expression
|
|
;
|
|
|
|
|
|
memOp:
|
|
RESERVE {printf("reserve expression\n");}
|
|
| RELEASE {printf("release expression\n");}
|
|
;
|
|
|
|
|
|
constant:
|
|
C_STRING {$$ = $<words>1;}
|
|
| C_INTEGER {$$ = $<words>1;}
|
|
| C_NULL {$$ = $<words>1;}
|
|
| C_CHARACTER {$$ = $<words>1;}
|
|
| C_TRUE {$$ = $<words>1;}
|
|
| C_FALSE {$$ = $<words>1;}
|
|
;
|
|
|
|
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;}
|
|
| 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;}
|
|
;
|
|
|
|
%%
|
|
|
|
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);
|
|
}
|
|
/*
|
|
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;
|
|
} */
|