sort of works, needs to handle reallocation issue
This commit is contained in:
parent
94d90611e1
commit
fcb082a9de
|
@ -201,10 +201,10 @@ void print_token(Token t){
|
|||
printf("SEOF_T\n" );
|
||||
break;
|
||||
case AVID_T:
|
||||
printf("AVID_T\t\t%s\n",t.attribute.vid_lex);
|
||||
printf("AVID_T\t\t%d\n",t.attribute.vid_offset);
|
||||
break;
|
||||
case SVID_T:
|
||||
printf("SVID_T\t\t%s\n",t.attribute.vid_lex);
|
||||
printf("SVID_T\t\t%d\n",t.attribute.vid_offset);
|
||||
break;
|
||||
case FPL_T:
|
||||
printf("FPL_T\t\t%f\n",t.attribute.flt_value);
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include "buffer.h"
|
||||
#include "token.h"
|
||||
#include "stable.h" /*Do not remove this line. SiR */
|
||||
#include "stable.h" /*Do not remove this line. SiR */
|
||||
|
||||
/* constant definitions */
|
||||
/* Input buffer parameters */
|
||||
|
|
33
scanner.c
33
scanner.c
|
@ -32,8 +32,8 @@
|
|||
/* project header files */
|
||||
#include "buffer.h"
|
||||
#include "token.h"
|
||||
#include "table.h"
|
||||
#include "stable.h"
|
||||
#include "table.h"
|
||||
|
||||
#define DEBUG /* for conditional processing */
|
||||
#undef DEBUG
|
||||
|
@ -375,7 +375,7 @@ Token aa_func02(char lexeme[]) {
|
|||
unsigned kw_idx, offset; /* Variable to contain keyword table index */
|
||||
Token t;
|
||||
char v_type;
|
||||
char* temp_str;
|
||||
/*char* temp_str;*/
|
||||
|
||||
#ifdef DEBUG
|
||||
printf("Lexeme: '%s'\n", lexeme);
|
||||
|
@ -390,11 +390,11 @@ Token aa_func02(char lexeme[]) {
|
|||
|
||||
/* Not a keyword? Must be AVID*/
|
||||
t.code = AVID_T;
|
||||
if ((temp_str = (char*)calloc(VID_LEN + 1, sizeof(char))) == NULL) {
|
||||
/*if ((temp_str = (char*)calloc(VID_LEN + 1, sizeof(char))) == NULL) {
|
||||
return aa_table[ES]("RUN TIME ERROR: ");
|
||||
}
|
||||
|
||||
strncpy(temp_str, lexeme, VID_LEN);
|
||||
strncpy(temp_str, lexeme, VID_LEN);*/
|
||||
|
||||
switch (lexeme[0]) { /* Read first character of lexeme for implicit type (not used yet?)*/
|
||||
case 'i':
|
||||
|
@ -409,14 +409,14 @@ Token aa_func02(char lexeme[]) {
|
|||
v_type = 'F';
|
||||
break;
|
||||
}
|
||||
if ((offset = st_install(sym_table, temp_str, v_type, line)) == -1){
|
||||
if ((offset = st_install(sym_table, lexeme, v_type, line)) == -1){
|
||||
printf("Error: Install failed - Symbol Table is full.\n");
|
||||
st_store(sym_table);
|
||||
free(temp_str);
|
||||
free(lexeme);
|
||||
exit(1);
|
||||
}
|
||||
t.attribute.vid_offset = offset;
|
||||
free(temp_str);
|
||||
/*free(temp_str);*/
|
||||
|
||||
return t;
|
||||
}
|
||||
|
@ -431,24 +431,24 @@ Token aa_func02(char lexeme[]) {
|
|||
*/
|
||||
Token aa_func03(char lexeme[]) {
|
||||
Token t;
|
||||
unsigned offset;
|
||||
unsigned offset;/*
|
||||
char* temp_str;
|
||||
if ((temp_str = (char*)calloc(VID_LEN + 2, sizeof(char))) == NULL) {
|
||||
return aa_table[ES]("RUN TIME ERROR: ");
|
||||
}
|
||||
}*/
|
||||
|
||||
strncpy(temp_str, lexeme, VID_LEN);
|
||||
temp_str[strlen(temp_str) - 1] = '#'; /* Add# to end of the SVID */
|
||||
/*strncpy(temp_str, lexeme, VID_LEN);*/
|
||||
lexeme[strlen(lexeme) - 1] = '#'; /* Add # to end of the SVID */
|
||||
|
||||
if ((offset = st_install(sym_table, temp_str, 'S', line)) == -1){
|
||||
if ((offset = st_install(sym_table, lexeme, 'S', line)) == -1){
|
||||
printf("Error: Install failed - Symbol Table is full.\n");
|
||||
st_store(sym_table);
|
||||
free(temp_str);
|
||||
free(lexeme);
|
||||
exit(1);
|
||||
}
|
||||
t.code = SVID_T;
|
||||
t.attribute.vid_offset = offset;
|
||||
free(temp_str);
|
||||
/*free(temp_str);*/
|
||||
|
||||
return t;
|
||||
}
|
||||
|
@ -557,11 +557,12 @@ Token aa_func12(char lexeme[]) {
|
|||
*/
|
||||
Token aa_func13(char lexeme[]) {
|
||||
Token t;
|
||||
unsigned int i;
|
||||
unsigned int i = strlen(lexeme);
|
||||
t.code = ERR_T;
|
||||
/*
|
||||
for (i = 0; i < (ERR_LEN) && i < strlen(lexeme); i++)
|
||||
t.attribute.err_lex[i] = lexeme[i];
|
||||
|
||||
*/
|
||||
if (strlen(lexeme) > ERR_LEN) {
|
||||
t.attribute.err_lex[i - 1] = '.';
|
||||
t.attribute.err_lex[i - 2] = '.';
|
||||
|
|
42
stable.c
42
stable.c
|
@ -29,16 +29,16 @@ STD st_create(int st_size){
|
|||
|
||||
new_stable.plsBD = NULL;
|
||||
new_stable.pstvr = (STVR*)malloc((size_t)st_size * sizeof(STVR));
|
||||
if (st_size <= 0 || (new_stable.pstvr == NULL))
|
||||
if (st_size <= 0 || (new_stable.pstvr == NULL)) {
|
||||
new_stable.st_size = 0;
|
||||
|
||||
}
|
||||
new_stable.plsBD = b_create(100, 1, 'a');
|
||||
if (new_stable.plsBD == NULL) {
|
||||
free(new_stable.plsBD);
|
||||
new_stable.st_size = 0;
|
||||
}
|
||||
|
||||
new_stable.st_size = 0;
|
||||
new_stable.st_size = st_size;
|
||||
new_stable.st_offset = 0;
|
||||
|
||||
return new_stable;
|
||||
|
@ -66,22 +66,23 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
|||
return -1;
|
||||
|
||||
/* Look for an existing entry in the symbol table */
|
||||
if ((offset = st_lookup(s_table, lexeme)) > -1)
|
||||
offset = st_lookup(s_table, lexeme);
|
||||
if (offset > -1)
|
||||
return offset;
|
||||
|
||||
/* Set proper pointer values on symbol table */
|
||||
s_table.pstvr[s_table.st_offset].plex = b_setmark(s_table.plsBD, b_size(s_table.plsBD));
|
||||
s_table.pstvr[s_table.st_offset].o_line = line;
|
||||
|
||||
/* Add lexeme to the symbol table's lexeme buffer */
|
||||
for (i = 0; i < strlen(lexeme); ++i){
|
||||
if (!b_addc(s_table.plsBD, lexeme[i]))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!b_addc(s_table.plsBD, '\0'))
|
||||
return -1;
|
||||
|
||||
/* Set proper pointer values on symbol table */
|
||||
s_table.pstvr[s_table.st_offset].plex = b_setmark(s_table.plsBD, b_size(s_table.plsBD));
|
||||
s_table.pstvr[s_table.st_offset].o_line = line;
|
||||
|
||||
|
||||
/* Set the default mask before setting the rest of the masks */
|
||||
s_table.pstvr[s_table.st_offset].status_field = DFT_MASK;
|
||||
|
||||
|
@ -102,11 +103,11 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
|||
return -1; /* Not supposed to fall into here */
|
||||
}
|
||||
|
||||
/* TODO: Handle reallocation flag behaviour
|
||||
/* TODO: Handle reallocation flag behaviour*/
|
||||
if (b_rflag(s_table.plsBD) == SET_R_FLAG){
|
||||
|
||||
printf("memory got tossed around\n");
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
/* Increment the symbol table offset */
|
||||
st_incoffset();
|
||||
|
@ -126,16 +127,17 @@ int st_install(STD s_table, char *lexeme, char type, int line){
|
|||
-1 if no element was found
|
||||
*/
|
||||
int st_lookup(STD s_table, char *lexeme){
|
||||
int idx, i; /* idx: index locatio for symbol table, i: increment counter*/
|
||||
char *head;
|
||||
int idx, i; /* idx: index location for symbol table, i: increment counter*/
|
||||
char *store;
|
||||
|
||||
head = b_setmark(s_table.plsBD, 0); /*Get head of the lexeme storage */
|
||||
store = b_setmark(s_table.plsBD, 0); /* Get head of the lexeme storage */
|
||||
for (i = 0, idx = 0; i < s_table.st_offset; ++i) {
|
||||
if (strcmp(lexeme, head + idx) == 0)
|
||||
if (strncmp(lexeme, store + idx, strlen(lexeme)) == 0)
|
||||
return i;
|
||||
else
|
||||
idx+= (short)(strlen(head + idx) + 1);
|
||||
idx+= (short)(strlen(store + idx) + 1);
|
||||
}
|
||||
printf("found nothing\n");
|
||||
return -1; /* Found nothing */
|
||||
}
|
||||
|
||||
|
@ -266,9 +268,9 @@ int st_print(STD s_table){
|
|||
|
||||
if (s_table.st_size <=0) return -1;
|
||||
|
||||
printf("Symbol Table\n____________\nLine Number\tVariable Identifier");
|
||||
printf("Symbol Table\n____________\nLine Number\tVariable Identifier\n");
|
||||
for(i = 0; i < s_table.st_offset; ++i)
|
||||
printf("%2d\t%s\n", s_table.pstvr[i].o_line, s_table.pstvr[i].plex);
|
||||
printf("%2d\t\t%s\n", s_table.pstvr[i].o_line, s_table.pstvr[i].plex);
|
||||
|
||||
return i;
|
||||
}
|
||||
|
@ -294,7 +296,7 @@ int st_store(STD s_table){
|
|||
* this works, too.
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
if(fopen_s(&out, ST_FILE_NAME, "w+") != 0)
|
||||
if (fopen_s(&out, ST_FILE_NAME, "w+") != 0 || out == NULL)
|
||||
#else
|
||||
if ((out = fopen(ST_FILE_NAME, "w+")) == NULL)
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue