it looks like this should work. t#29
This commit is contained in:
20
Makefile
20
Makefile
@ -1,21 +1,20 @@
|
||||
CC := gcc
|
||||
FLEX := flex
|
||||
LEX := lexicalStructure.lex
|
||||
EXE := runner
|
||||
EXE := alpha
|
||||
CFLAGS := -std=c99 -Wall
|
||||
CPPFLAGS :=
|
||||
|
||||
runner: flex.o runner.o
|
||||
$(CC) -o runner runner.o flex.o
|
||||
build: lex.yy.c runner.o runner
|
||||
|
||||
lex.yy.c: lexicalStructure.lex
|
||||
$(FLEX) -o lex.yy.c $(LEX)
|
||||
|
||||
runner.o: runner.c runner.h flex.h
|
||||
$(CC) $(CFLAGS) -o runner.o -c runner.c
|
||||
|
||||
flex.o: lex.yy.c typedefs.h
|
||||
$(CC) $(CFLAGS) -o flex.o -c lex.yy.c
|
||||
|
||||
lex.yy.c: lexicalStructure.lex
|
||||
$(FLEX) -o lex.yy.c $(LEX)
|
||||
runner: lex.yy.c runner.o
|
||||
$(CC) -o $(EXE) runner.o lex.yy.c
|
||||
|
||||
debug: CFLAGS += -DDEBUG=1
|
||||
debug: clean runner
|
||||
@ -39,9 +38,14 @@ test:
|
||||
./$(EXE) -tok ./tests/test_simpleLiterals.alpha
|
||||
./$(EXE) -tok ./tests/test_real_alpha_file.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:
|
||||
rm -f *.o
|
||||
rm -f lex.yy.c
|
||||
rm -f $(EXE)
|
||||
rm -f flex.h
|
||||
rm -f *.tok
|
||||
rm -f *.st
|
||||
|
46
grammar.y
Normal file
46
grammar.y
Normal 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
|
@ -41,7 +41,6 @@ void print_symbol_table(SymbolTable *table, FILE *file_ptr){
|
||||
}
|
||||
/*
|
||||
|
||||
*/
|
||||
int main(void){
|
||||
char *prim = strdup("primitive");
|
||||
char *inte = strdup("integer");
|
||||
@ -51,25 +50,41 @@ int main(void){
|
||||
char *str = strdup("string");
|
||||
char *arg = strdup("arg");
|
||||
// Value* v = calloc(1, sizeof(Value));
|
||||
CreateEntry(parant, prim, boole, NULL, 0);
|
||||
CreateEntry(parant, prim, chare, NULL, 0);
|
||||
CreateEntry(parant, prim, inte, NULL, 0);
|
||||
CreateEntry(parant, &"1 -> character", str, NULL, 0);
|
||||
CreateEntry(parant, &"integer -> integer", &"int2int", NULL, 0);
|
||||
CreateEntry(parant, &"string -> integer", &"string2int", NULL, 0);
|
||||
CreateEntry(parant, &"int2int", &"square", NULL, 0);
|
||||
CreateEntry(parant, &"string2int", &"entry", NULL, 0);
|
||||
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", NULL, 0);
|
||||
CreateEntry(child, inte, &"x");
|
||||
SymbolTable * second = CreateScope(parant, 21,15);
|
||||
CreateEntry(second, str, arg, NULL, 0);
|
||||
CreateEntry(second, inte, &"input", NULL, 0);
|
||||
CreateEntry(second, inte, &"expected", NULL, 0);
|
||||
CreateEntry(second, inte, &"actual", NULL, 0);
|
||||
CreateEntry(second, &"$_undefined_type", &"result", NULL, 0);
|
||||
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, NULL, 0);
|
||||
CreateEntry(third, &"YAZOO", &"input", NULL, 0);
|
||||
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);
|
||||
@ -79,3 +94,5 @@ int main(void){
|
||||
free(arg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
*/
|
||||
|
17
runner.c
17
runner.c
@ -1,6 +1,7 @@
|
||||
#include "symbol_table.h"
|
||||
#include "symbol_table.c"
|
||||
#include "runner.h"
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *check_input;
|
||||
int token;
|
||||
@ -84,3 +85,17 @@ int is_alpha_file(char *file, int file_len) {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
4
runner.h
4
runner.h
@ -20,7 +20,11 @@ extern int line_number, column_number;
|
||||
extern char *yytext;
|
||||
extern FILE *yyin;
|
||||
int arg;
|
||||
SymbolTable * top;
|
||||
SymbolTable * curr;
|
||||
|
||||
int main(int argc, char* argv[]);
|
||||
char *is_tok(int argc, char* argv[]);
|
||||
int is_alpha_file(char *file, int file_len);
|
||||
void enter_scope(int, int);
|
||||
void exit_scope(void);
|
||||
|
@ -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));
|
||||
newEntry->theType = typeOf;
|
||||
newEntry->theName = id;
|
||||
newEntry->value = value;
|
||||
newEntry->StrLength = StringLength;
|
||||
if(table->entries == NULL){
|
||||
table->entries = newEntry;
|
||||
return newEntry;
|
||||
@ -84,18 +82,35 @@ TableNode* CreateEntry(SymbolTable* table, char* typeOf, char* id, Value* value,
|
||||
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";
|
||||
Value* ofX = (Value*)malloc(sizeof(Value));
|
||||
ofX->value_of_char = 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,ofX,-1);
|
||||
printf("The value of the first entry is %s\n",First_Entry->value->value_of_char);
|
||||
TableNode* First_Entry = CreateEntry(Second,String,X);
|
||||
printf("The value of the first entry is %s\n",First_Entry->theType);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef struct ListOfTable{
|
||||
struct SymbolTable* table;
|
||||
@ -34,5 +35,5 @@ typedef struct 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);
|
||||
|
Reference in New Issue
Block a user