it looks like this should work. t#29

This commit is contained in:
Meyer Simon
2025-02-21 14:30:14 -05:00
parent ad5da96857
commit fcb66c125d
7 changed files with 136 additions and 34 deletions

View File

@ -1,21 +1,20 @@
CC := gcc CC := gcc
FLEX := flex FLEX := flex
LEX := lexicalStructure.lex LEX := lexicalStructure.lex
EXE := runner EXE := alpha
CFLAGS := -std=c99 -Wall CFLAGS := -std=c99 -Wall
CPPFLAGS := CPPFLAGS :=
runner: flex.o runner.o build: lex.yy.c runner.o runner
$(CC) -o runner runner.o flex.o
lex.yy.c: lexicalStructure.lex
$(FLEX) -o lex.yy.c $(LEX)
runner.o: runner.c runner.h flex.h runner.o: runner.c runner.h flex.h
$(CC) $(CFLAGS) -o runner.o -c runner.c $(CC) $(CFLAGS) -o runner.o -c runner.c
flex.o: lex.yy.c typedefs.h runner: lex.yy.c runner.o
$(CC) $(CFLAGS) -o flex.o -c lex.yy.c $(CC) -o $(EXE) runner.o lex.yy.c
lex.yy.c: lexicalStructure.lex
$(FLEX) -o lex.yy.c $(LEX)
debug: CFLAGS += -DDEBUG=1 debug: CFLAGS += -DDEBUG=1
debug: clean runner debug: clean runner
@ -39,9 +38,14 @@ 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
rm -f lex.yy.c rm -f lex.yy.c
rm -f $(EXE) rm -f $(EXE)
rm -f flex.h rm -f flex.h
rm -f *.tok rm -f *.tok
rm -f *.st

46
grammar.y Normal file
View File

@ -0,0 +1,46 @@
%token 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

View File

@ -41,7 +41,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){
} }
/* /*
*/
int main(void){ int main(void){
char *prim = strdup("primitive"); char *prim = strdup("primitive");
char *inte = strdup("integer"); char *inte = strdup("integer");
@ -51,25 +50,41 @@ int main(void){
char *str = strdup("string"); char *str = strdup("string");
char *arg = strdup("arg"); char *arg = strdup("arg");
// Value* v = calloc(1, sizeof(Value)); // Value* v = calloc(1, sizeof(Value));
CreateEntry(parant, prim, boole, NULL, 0); CreateEntry(parant, prim, boole);
CreateEntry(parant, prim, chare, NULL, 0); CreateEntry(parant, prim, chare);
CreateEntry(parant, prim, inte, NULL, 0); CreateEntry(parant, prim, inte);
CreateEntry(parant, &"1 -> character", str, NULL, 0); CreateEntry(parant, &"1 -> character", str);
CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0); CreateEntry(parant, &"integer -> integer", &"int2int");
CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0); CreateEntry(parant, &"string -> integer", &"string2int");
CreateEntry(parant, &"int2int", &"square", NULL, 0); CreateEntry(parant, &"int2int", &"square");
CreateEntry(parant, &"string2int", &"entry", NULL, 0); CreateEntry(parant, &"string2int", &"entry");
SymbolTable * child = CreateScope(parant, 14,14); SymbolTable * child = CreateScope(parant, 14,14);
CreateEntry(child, inte, &"x", NULL, 0); CreateEntry(child, inte, &"x");
SymbolTable * second = CreateScope(parant, 21,15); SymbolTable * second = CreateScope(parant, 21,15);
CreateEntry(second, str, arg, NULL, 0); CreateEntry(second, str, arg);
CreateEntry(second, inte, &"input", NULL, 0); CreateEntry(second, inte, &"input");
CreateEntry(second, inte, &"expected", NULL, 0); CreateEntry(second, inte, &"expected");
CreateEntry(second, inte, &"actual", NULL, 0); CreateEntry(second, inte, &"actual");
CreateEntry(second, &"$_undefined_type", &"result", NULL, 0); CreateEntry(second, &"$_undefined_type", &"result");
SymbolTable * third = CreateScope(second, 33,44); SymbolTable * third = CreateScope(second, 33,44);
CreateEntry(third, &"BOO", arg, NULL, 0); CreateEntry(third, &"BOO", arg);
CreateEntry(third, &"YAZOO", &"input", NULL, 0); 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); print_symbol_table(parant, stderr);
free(inte); free(inte);
free(boole); free(boole);
@ -79,3 +94,5 @@ int main(void){
free(arg); free(arg);
return 0; return 0;
} }
*/

View File

@ -1,6 +1,7 @@
#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; char *check_input;
int token; int token;
@ -84,3 +85,17 @@ int is_alpha_file(char *file, int file_len) {
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;
}

View File

@ -20,7 +20,11 @@ extern int line_number, column_number;
extern char *yytext; extern char *yytext;
extern FILE *yyin; extern FILE *yyin;
int arg; int arg;
SymbolTable * top;
SymbolTable * curr;
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[]);
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);

View File

@ -68,12 +68,10 @@ SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column){
} }
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength){ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id){
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode)); TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
newEntry->theType = typeOf; newEntry->theType = typeOf;
newEntry->theName = id; newEntry->theName = id;
newEntry->value = value;
newEntry->StrLength = StringLength;
if(table->entries == NULL){ if(table->entries == NULL){
table->entries = newEntry; table->entries = newEntry;
return newEntry; return newEntry;
@ -84,18 +82,35 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value,
return newEntry; 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 //uncomment the below main function along with the headers above for a simple standalone test of table and entry creation
/* /*
int main(){ int main(){
char* String = "STRING"; char* String = "STRING";
char* X = "X"; char* X = "X";
Value* ofX = (Value*)malloc(sizeof(Value));
ofX->value_of_char = X;
SymbolTable* Second = CreateScope(NULL, 2,2); SymbolTable* Second = CreateScope(NULL, 2,2);
printf("Line number is %d, Column number of scope is %d\n",Second->Line_Number,Second->Column_Number); 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,ofX,-1); TableNode* First_Entry = CreateEntry(Second,String,X);
printf("The value of the first entry is %s\n",First_Entry->value->value_of_char); printf("The value of the first entry is %s\n",First_Entry->theType);
return 0; return 0;
} }
*/ */

View File

@ -1,6 +1,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
typedef struct ListOfTable{ typedef struct ListOfTable{
struct SymbolTable* table; struct SymbolTable* table;
@ -34,5 +35,5 @@ typedef struct SymbolTable{
}SymbolTable; }SymbolTable;
TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value, int StringLength); TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id);
SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column); SymbolTable* CreateScope(SymbolTable* ParentScope, int Line, int Column);