Merge branch 'Dev' into Sprint2-Infrastructure-FE-t#30
This commit is contained in:
4
Makefile
4
Makefile
@ -40,6 +40,9 @@ test:
|
|||||||
./$(EXE) -tok ./tests/test_simpleLiterals.alpha
|
./$(EXE) -tok ./tests/test_simpleLiterals.alpha
|
||||||
./$(EXE) -tok ./tests/test_real_alpha_file.alpha
|
./$(EXE) -tok ./tests/test_real_alpha_file.alpha
|
||||||
./$(EXE) -tok ./tests/test_real_alpha_2.alpha
|
./$(EXE) -tok ./tests/test_real_alpha_2.alpha
|
||||||
|
./$(EXE) -tok -st ./tests/test_real_alpha_2.alpha
|
||||||
|
./$(EXE) -st -tok ./tests/test_operators.alpha
|
||||||
|
./$(EXE) -st ./tests/test_keywords.alpha
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o
|
rm -f *.o
|
||||||
@ -49,3 +52,4 @@ clean:
|
|||||||
rm -f *.tok
|
rm -f *.tok
|
||||||
rm -f grammar.tab.c
|
rm -f grammar.tab.c
|
||||||
rm -f grammar.tab.h
|
rm -f grammar.tab.h
|
||||||
|
rm -f *.st
|
57
grammar.y
57
grammar.y
@ -6,13 +6,52 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
%}
|
%}
|
||||||
|
|
||||||
// Declarations
|
%token ID 101
|
||||||
|
%token T_INTEGER 201
|
||||||
|
%token T_ADDRESS 202
|
||||||
// Grammar Rules
|
%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
|
||||||
|
%%
|
||||||
|
|
||||||
%%
|
%%
|
||||||
semicolon.opt:
|
|
||||||
%empty
|
|
||||||
| ";"
|
|
||||||
;
|
|
||||||
%%
|
|
@ -81,6 +81,8 @@ SCHAR \\n|\\t|\\\"|[^\"\n\\]
|
|||||||
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {return ID;}}
|
{ID} {if(DEBUG) {printf( "ID: %s (%d)\n", yytext, ID);} else {return ID;}}
|
||||||
|
|
||||||
\n {line_number++; column_number = 1;}
|
\n {line_number++; column_number = 1;}
|
||||||
. {column_number++;}
|
\t {column_number++;}
|
||||||
|
" " {column_number++;}
|
||||||
|
. {column_number++; return 1999;}
|
||||||
|
|
||||||
%%
|
%%
|
||||||
|
98
print_symbol_table.c
Normal file
98
print_symbol_table.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include "symbol_table.h"
|
||||||
|
#include "symbol_table.c"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void print_symbol_table(SymbolTable *table, FILE *file_ptr){
|
||||||
|
if(table->Parent_Scope == NULL){
|
||||||
|
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME", "SCOPE", "PARENT", "TYPE", "Extra annotation");
|
||||||
|
}
|
||||||
|
TableNode * entrie = table->entries;
|
||||||
|
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
|
||||||
|
int parant_scope = 0;
|
||||||
|
int current_scope = 0;
|
||||||
|
if(table->Parent_Scope != NULL){
|
||||||
|
parant_scope = table->Parent_Scope->Line_Number*1000 + table->Parent_Scope->Column_Number;
|
||||||
|
current_scope = table->Line_Number*1000 + table->Column_Number;
|
||||||
|
} else {
|
||||||
|
current_scope = 1001;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(; entrie != NULL; entrie = entrie->next){
|
||||||
|
if (parant_scope == 0){
|
||||||
|
fprintf(file_ptr, "%-17s: %06d : : %-21s: %-28s\n",
|
||||||
|
entrie->theName, current_scope,
|
||||||
|
entrie->theType, "Extra annotation");
|
||||||
|
} else {
|
||||||
|
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n",
|
||||||
|
entrie->theName, current_scope, parant_scope,
|
||||||
|
entrie->theType, "Extra annotation");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (table->Children_Scope != NULL){
|
||||||
|
ListOfTable* node = table->Children_Scope;
|
||||||
|
for(; node != NULL; node = node->next){
|
||||||
|
print_symbol_table(node->table, file_ptr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (table->Parent_Scope == NULL) {
|
||||||
|
fprintf(file_ptr, "-----------------:--------:--------:----------------------:-----------------------------\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
|
||||||
|
int main(void){
|
||||||
|
char *prim = strdup("primitive");
|
||||||
|
char *inte = strdup("integer");
|
||||||
|
SymbolTable * parant = CreateScope(NULL, 1,1);
|
||||||
|
char *boole = strdup("Boolean");
|
||||||
|
char *chare = strdup("character");
|
||||||
|
char *str = strdup("string");
|
||||||
|
char *arg = strdup("arg");
|
||||||
|
// Value* v = calloc(1, sizeof(Value));
|
||||||
|
CreateEntry(parant, prim, boole);
|
||||||
|
CreateEntry(parant, prim, chare);
|
||||||
|
CreateEntry(parant, prim, inte);
|
||||||
|
CreateEntry(parant, &"1 -> character", str);
|
||||||
|
CreateEntry(parant, &"integer -> integer", &"int2int");
|
||||||
|
CreateEntry(parant, &"string -> integer", &"string2int");
|
||||||
|
CreateEntry(parant, &"int2int", &"square");
|
||||||
|
CreateEntry(parant, &"string2int", &"entry");
|
||||||
|
SymbolTable * child = CreateScope(parant, 14,14);
|
||||||
|
CreateEntry(child, inte, &"x");
|
||||||
|
SymbolTable * second = CreateScope(parant, 21,15);
|
||||||
|
CreateEntry(second, str, arg);
|
||||||
|
CreateEntry(second, inte, &"input");
|
||||||
|
CreateEntry(second, inte, &"expected");
|
||||||
|
CreateEntry(second, inte, &"actual");
|
||||||
|
CreateEntry(second, &"$_undefined_type", &"result");
|
||||||
|
SymbolTable * third = CreateScope(second, 33,44);
|
||||||
|
CreateEntry(third, &"BOO", arg);
|
||||||
|
CreateEntry(third, &"YAZOO", &"input");
|
||||||
|
|
||||||
|
TableNode *ret = table_lookup(third, "arg");
|
||||||
|
printf("%s == %s\n", ret->theName, "arg");
|
||||||
|
|
||||||
|
ret = table_lookup(third, "hello");
|
||||||
|
printf("This should be nil %p != %s\n", ret, "BOO");
|
||||||
|
|
||||||
|
ret = look_up(second, "input");
|
||||||
|
printf("%s == %s\n", ret->theName, "input");
|
||||||
|
|
||||||
|
ret = look_up(second, "square");
|
||||||
|
printf("%s == %s\n", ret->theName, "square");
|
||||||
|
|
||||||
|
ret = look_up(second, "spuare");
|
||||||
|
printf("This should be nil %p == %s\n", ret, "square");
|
||||||
|
|
||||||
|
print_symbol_table(parant, stderr);
|
||||||
|
free(inte);
|
||||||
|
free(boole);
|
||||||
|
free(prim);
|
||||||
|
free(str);
|
||||||
|
free(chare);
|
||||||
|
free(arg);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
195
runner.c
195
runner.c
@ -1,99 +1,172 @@
|
|||||||
|
//#include "symbol_table.h"
|
||||||
|
#include "symbol_table.c"
|
||||||
#include "runner.h"
|
#include "runner.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
char *check_input;
|
if (argc == 1) {
|
||||||
int token;
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs \n");
|
||||||
FILE *output;
|
return -1;
|
||||||
|
|
||||||
if (argc == 1 || argc > 3) {
|
|
||||||
fprintf(stderr, "invalid input with 1, >3, or non .alpha arg \n");
|
|
||||||
return -1; //no alpha file or too many args
|
|
||||||
} else if (argc == 2) {
|
} else if (argc == 2) {
|
||||||
|
//can be either -help or .alpha file
|
||||||
if (is_help(argv[1])) {
|
if (is_help(argv[1])) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (is_alpha_file(argv[1], strlen(argv[1])) != 0) {
|
} else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) {
|
||||||
|
//run flex for now
|
||||||
|
no_flag = SET_FLAG; //no argument but valid input
|
||||||
|
alpha_file = fopen(argv[1], "r");
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file or use -help for more inputs\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
arg = NO_ARG; //no argument but valid input
|
|
||||||
yyin = fopen(argv[1], "r");
|
|
||||||
} else {
|
} else {
|
||||||
check_input = is_tok(argc, argv);
|
//last input must be .alpha
|
||||||
if (strcmp(CHECK_OTHER, check_input) == 0 || strcmp(INVALID_ARG, check_input) == 0) {
|
if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) {
|
||||||
fprintf(stderr, "check_other or invalid_arg \n");
|
fprintf(stderr, "INVALID INPUT: Include a .alpha file at end of input or use -help for more inputs \n");
|
||||||
return -1; //invalid argument (need to update as we add more valid arguments)
|
return -1;
|
||||||
|
} else {
|
||||||
|
//now check that other args are valid (flags will not be null if flag is present)
|
||||||
|
for (int i = 1; i < argc - 1; i++) {
|
||||||
|
if (check_flag(argv[i], argv[argc - 1]) != 0) {
|
||||||
|
fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
alpha_file = fopen(argv[argc - 1], "r");
|
||||||
}
|
}
|
||||||
output = fopen(check_input, "w");
|
|
||||||
arg = TOK_ARG; //it is a -tok arg with a valid alpha file at argv[2]
|
|
||||||
yyin = fopen(argv[2], "r");
|
|
||||||
}
|
}
|
||||||
while (0 != (token = yylex())) {
|
return run(alpha_file);
|
||||||
if (arg == TOK_ARG) {
|
}
|
||||||
fprintf(output, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
|
||||||
|
int check_flag(char *arg, char* alpha) {
|
||||||
|
if (strcmp("-tok", arg) == 0) {
|
||||||
|
if (tok_flag == NULL) {
|
||||||
|
return new_file(arg, alpha);
|
||||||
}
|
}
|
||||||
if(token == COMMENT){
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
for (int i = 0; i < yyleng; i++) {
|
return -1;
|
||||||
if(yytext[i] == '\n'){
|
} else if (strcmp("-st", arg) == 0) {
|
||||||
line_number++;
|
if (st_flag == NULL) {
|
||||||
column_number = 0;
|
return new_file(arg, alpha);
|
||||||
}
|
}
|
||||||
column_number++;
|
fprintf(stderr, "FLAGS REPEAT\n");
|
||||||
}
|
return -1;
|
||||||
continue;
|
} else {
|
||||||
|
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int run(FILE *alpha) {
|
||||||
|
int token;
|
||||||
|
//check that file exists
|
||||||
|
if (alpha == NULL) {
|
||||||
|
fprintf(stderr, "INPUT FILE NOT FOUND\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
yyin = alpha;
|
||||||
|
while (0 != (token = yylex())) {
|
||||||
|
if (tok_flag != NULL) {
|
||||||
|
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, token, yytext);
|
||||||
|
}
|
||||||
|
if(token == COMMENT){
|
||||||
|
for (int i = 0; i < yyleng; i++) {
|
||||||
|
if(yytext[i] == '\n'){
|
||||||
|
line_number++;
|
||||||
|
column_number = 0;
|
||||||
|
}
|
||||||
|
column_number++;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
if(token == 1999){
|
||||||
|
printf("On line number %d and column number %d we have an invalid character:%s\n",line_number,column_number,yytext);
|
||||||
|
}
|
||||||
column_number += yyleng;
|
column_number += yyleng;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (st_flag != NULL) {
|
||||||
|
//output symbol table, file pointer is
|
||||||
|
print_symbol_table(curr,st_flag);
|
||||||
|
}
|
||||||
|
|
||||||
if (yyin != NULL) {
|
if (yyin != NULL) {
|
||||||
fclose(yyin);
|
fclose(yyin);
|
||||||
}
|
}
|
||||||
if (output != NULL && arg == TOK_ARG) {
|
|
||||||
fclose(output);
|
if (tok_flag != NULL) {
|
||||||
|
fclose(tok_flag);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool is_help(char * input) {
|
bool is_help(char * input) {
|
||||||
if (strcmp(input, "-help") == 0) {
|
if (strcmp(input, "-help") == 0) {
|
||||||
printf("HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n");
|
printf("%s", HELP);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *is_tok(int argc, char *argv[]) {
|
int new_file(char *arg, char *alpha) {
|
||||||
if (argc == 3 && strcmp("-tok", argv[1]) == 0) {
|
int type_len;
|
||||||
char *input_prog = argv[2];
|
const char *basename = alpha;
|
||||||
int file_len = strlen(input_prog);
|
const char *slash = strchr(alpha, '/');
|
||||||
//check that input program is a .alpha file
|
while (slash != NULL) {
|
||||||
if (is_alpha_file(input_prog, file_len) != 0) {
|
basename = slash + 1;
|
||||||
return INVALID_ARG;
|
slash = strchr(basename, '/');
|
||||||
}
|
|
||||||
|
|
||||||
const char *basename = input_prog;
|
|
||||||
const char *slash = strrchr(input_prog, '/');
|
|
||||||
if (slash != NULL) {
|
|
||||||
basename = slash + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate lengths
|
|
||||||
int basename_len = strlen(basename);
|
|
||||||
char* FILE_tok = calloc(basename_len - ALPHA_OFFSET + TOK_LEN + 2, sizeof(char));
|
|
||||||
|
|
||||||
// Copy filename and add .tok extension
|
|
||||||
strncpy(FILE_tok, basename, basename_len - ALPHA_OFFSET);
|
|
||||||
//fprintf(stderr, "hello");
|
|
||||||
strcat(FILE_tok, ".tok");
|
|
||||||
|
|
||||||
return FILE_tok;
|
|
||||||
}
|
}
|
||||||
return CHECK_OTHER;
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
|
type_len = TOK_LEN;
|
||||||
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
|
type_len = ST_LEN;
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "INVALID FLAG: Use -help to view valid inputs\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// calculate lengths
|
||||||
|
int basename_len = strlen(basename);
|
||||||
|
char *file_name = calloc(basename_len - ALPHA_OFFSET + type_len + 2, sizeof(char));
|
||||||
|
|
||||||
|
//coy filename and add extension
|
||||||
|
strncpy(file_name, basename, basename_len - ALPHA_OFFSET);
|
||||||
|
strcat(file_name, ".");
|
||||||
|
strcat(file_name, arg + 1);
|
||||||
|
|
||||||
|
|
||||||
|
if (strcmp(arg, "-tok") == 0) {
|
||||||
|
tok_flag = fopen(file_name, "w");
|
||||||
|
} else if (strcmp(arg, "-st") == 0) {
|
||||||
|
st_flag = fopen(file_name, "w");
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_alpha_file(char *file, int file_len) {
|
|
||||||
if (strcmp(".alpha", file + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
int is_alpha_file(char *alpha, int file_len) {
|
||||||
|
if (strcmp(".alpha", alpha + sizeof(char) * (file_len - ALPHA_OFFSET)) != 0) {
|
||||||
return -1; //not alpha file
|
return -1; //not alpha file
|
||||||
}
|
}
|
||||||
return 0; //is alpha file
|
return 0; //is alpha file
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void enter_scope(int line, int column){
|
||||||
|
curr = CreateScope(curr, line, column);
|
||||||
|
}
|
||||||
|
void exit_scope() {
|
||||||
|
if(curr->Parent_Scope == NULL){
|
||||||
|
printf("Can't close top");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
curr = curr->Parent_Scope;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
33
runner.h
33
runner.h
@ -1,14 +1,9 @@
|
|||||||
#define ALPHA_OFFSET 6
|
#define ALPHA_OFFSET 6
|
||||||
#define TOK_LEN 3
|
#define TOK_LEN 3
|
||||||
|
#define ST_LEN 2
|
||||||
//returns for is_tok
|
#define HELP "HELP:\nHow to run the alpha compiler:\n./alpha [options] program\nValid options:\n-tok output the token number, token, line number, and column number for each of the tokens to the .tok file\n-st output the symbol table for the program to the .st file\n-help print this message and exit the alpha compiler\n"
|
||||||
#define INVALID_ARG "invalid"
|
//use to set flags for arg types
|
||||||
#define CHECK_OTHER "diff"
|
#define SET_FLAG 1
|
||||||
|
|
||||||
//argument type in main
|
|
||||||
#define NO_ARG 0
|
|
||||||
#define DIFF_ARG 1
|
|
||||||
#define TOK_ARG 2
|
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -22,7 +17,23 @@ extern char *yytext;
|
|||||||
extern FILE *yyin;
|
extern FILE *yyin;
|
||||||
int arg;
|
int arg;
|
||||||
|
|
||||||
int main(int argc, char* argv[]);
|
SymbolTable * top;
|
||||||
|
SymbolTable * curr;
|
||||||
|
|
||||||
|
// int main(int argc, char* argv[]);
|
||||||
char *is_tok(int argc, char* argv[]);
|
char *is_tok(int argc, char* argv[]);
|
||||||
int is_alpha_file(char *file, int file_len);
|
// int is_alpha_file(char *file, int file_len);
|
||||||
|
void enter_scope(int, int);
|
||||||
|
void exit_scope(void);
|
||||||
|
|
||||||
|
FILE *alpha_file;
|
||||||
|
FILE *tok_flag = NULL;
|
||||||
|
FILE *st_flag = NULL;
|
||||||
|
int no_flag = 0;
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]);
|
||||||
|
int new_file(char *arg, char *alpha);
|
||||||
|
int is_alpha_file(char *alpha, int file_len);
|
||||||
bool is_help(char * input);
|
bool is_help(char * input);
|
||||||
|
int run(FILE*alpha);
|
||||||
|
int check_flag(char * arg, char* alpha);
|
||||||
|
83
symbol_table.c
Normal file
83
symbol_table.c
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
//Defining a symbol table
|
||||||
|
//Using a Linked List Structure. Head of linked List points to parent scope (if one exists)
|
||||||
|
|
||||||
|
//Tail of Linked List points to a List of child scopes
|
||||||
|
|
||||||
|
|
||||||
|
#include "symbol_table.h"
|
||||||
|
|
||||||
|
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){
|
||||||
|
SymbolTable* table = (SymbolTable*)malloc(sizeof(SymbolTable));
|
||||||
|
table->Line_Number = Line;
|
||||||
|
table->Column_Number = Column;
|
||||||
|
table->Parent_Scope = ParentScope;
|
||||||
|
table->Children_Scope = NULL;
|
||||||
|
table->entries = NULL;
|
||||||
|
if(ParentScope != NULL){
|
||||||
|
if(ParentScope->Children_Scope == NULL){
|
||||||
|
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
||||||
|
newEntry->next = NULL;
|
||||||
|
//newEntry->prev = NULL;
|
||||||
|
newEntry->table = table;
|
||||||
|
ParentScope->Children_Scope = newEntry;
|
||||||
|
} else{
|
||||||
|
ListOfTable* newEntry = (ListOfTable*)malloc(sizeof(ListOfTable));
|
||||||
|
//newEntry->prev = NULL;
|
||||||
|
newEntry->table= table;
|
||||||
|
ListOfTable* oldEntry = ParentScope->Children_Scope;
|
||||||
|
ParentScope->Children_Scope = newEntry;
|
||||||
|
newEntry->next = oldEntry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return table;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){
|
||||||
|
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
|
||||||
|
newEntry->theType = typeOf;
|
||||||
|
newEntry->theName = id;
|
||||||
|
if(table->entries == NULL){
|
||||||
|
table->entries = newEntry;
|
||||||
|
return newEntry;
|
||||||
|
} else{
|
||||||
|
TableNode* oldEntry = table->entries;
|
||||||
|
table->entries = newEntry;
|
||||||
|
newEntry->next = oldEntry;
|
||||||
|
return newEntry;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TableNode * table_lookup(SymbolTable * table, char * x){
|
||||||
|
TableNode * entrie = table->entries;
|
||||||
|
for(; entrie != NULL; entrie = entrie->next){
|
||||||
|
if (!strcmp(entrie->theName, x)){
|
||||||
|
return entrie;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
TableNode * look_up(SymbolTable * table, char * x){
|
||||||
|
if(table == NULL){
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
TableNode * ret = table_lookup(table, x);
|
||||||
|
if (ret != NULL){
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
return look_up(table->Parent_Scope, x);
|
||||||
|
}
|
||||||
|
|
||||||
|
//uncomment the below main function along with the headers above for a simple standalone test of table and entry creation
|
||||||
|
|
||||||
|
/*
|
||||||
|
int main(){
|
||||||
|
char* String = "STRING";
|
||||||
|
char* X = "X";
|
||||||
|
SymbolTable* Second = CreateScope(NULL, 2,2);
|
||||||
|
printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number);
|
||||||
|
TableNode* First_Entry = CreateEntry(Second,String,X);
|
||||||
|
|
||||||
|
printf("The type of the first entry is %s\n",First_Entry->theType);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
*/
|
29
symbol_table.h
Normal file
29
symbol_table.h
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct ListOfTable{
|
||||||
|
struct SymbolTable* table;
|
||||||
|
//struct ListOfTable* prev;
|
||||||
|
struct ListOfTable* next;
|
||||||
|
|
||||||
|
}ListOfTable;
|
||||||
|
|
||||||
|
typedef struct TableNode{
|
||||||
|
char* theType;
|
||||||
|
char* theName;
|
||||||
|
struct TableNode* next;
|
||||||
|
}TableNode;
|
||||||
|
|
||||||
|
typedef struct SymbolTable{
|
||||||
|
TableNode* entries;
|
||||||
|
struct SymbolTable* Parent_Scope;
|
||||||
|
struct ListOfTable* Children_Scope;
|
||||||
|
int Line_Number;
|
||||||
|
int Column_Number;
|
||||||
|
}SymbolTable;
|
||||||
|
|
||||||
|
|
||||||
|
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
|
||||||
|
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);
|
0
tests/test_comment_issues.tok
Normal file
0
tests/test_comment_issues.tok
Normal file
0
tests/test_wrong_type.txt
Normal file
0
tests/test_wrong_type.txt
Normal file
Reference in New Issue
Block a user