Token buffer datastructure

 /******************************************************************************

                              Online C++ Compiler.
               Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/
// Dynamic token buffer, can be easily resized using realloc
#include <iostream>

enum 
{
    EXPR,
    COND,
    STMT,
    DECL
    
};

struct TLV
{
    int token;   // atrribute
    int len_tok_value; // length
    char *tok_value; // value  
};

typedef struct token_buffer // TLV concept
{
    int type_of_language_elem; // type of expr, cond, decl etc. used in generator
    int no_of_elem; // define tlv size
    struct TLV *tlv;  
    
}tok_buffer;

void token_insert_back(struct tok_buffer* tbuff, int lang_elem,int tok,char *tok_value)
{
    static int t_size = 1;
    struct *TLV type_len_val= tbuff->tlv;
    if(type_len_val == NULL)
    {
        type_len_val = (struct TLV*) malloc(sizeof(struct TLV));
    }
    else
    {
        type_len_val = (struct TLV*) realloc(type_len_val,(t_size + 1 )*sizeof(TLV) );
    }
    tbuff->type_of_language_elem = lang_elem;
    tbuff->no_of_elem = t_size++;
    type_len_val->token = tok;
    type_len_val->len_tok_value = strlen(tok_value);
    type_len_val->tok_value = (char *) malloc(strlen(tok_value));
    strncpy(tbuff->tok_value, tok_value,strlen(tok_value) );
}
void token_read_front(struct tok_buffer* tbuff, int *lang_elem,int *tok,char *tok_value)
{
    int len_tlv = tbuff->no_of_elem;
    static int i=0;
    struct *TLV type_len_val= tbuff->tlv;
    if(i < len_tlv)
    {
       *tok = type_len_val[i].token;
       tok_value = (char*) malloc( sizeof(type_len_val[i].len_tok_value));
       strncpy(tok_value, type_len_val[i].tok_value, strlen(type_len_val[i].len_tok_value));
       i++;
    }
}

struct tokenbuff_list
{
    int size;
    struct token_buffer *list_of_tokbuff;
};
struct tokenbuff_list *list;
void save_token_buffer_list(struct token_buffer* single_elem)
{
    list->size++;
    if(list->list_of_tokbuff == NULL)
    {
        list->list_of_tokbuff = malloc(sizeof(struct token_buffer));
    }
    else
    {
        list->list_of_tokbuff = realloc(list->list_of_tokbuff, list->size*sizeof(struct token_buffer) );
    }
    memcpy( list->list_of_tokbuff[list->size-1], single_elem);
}
void fetch_token_buffer_list(struct token_buffer* single_elem)
{
    memcpy( single_elem, list->list_of_tokbuff[list->size-1]);
    list->size--;
     if(list->size == 0)
     {
        free(list);
        return;
     }
    list->list_of_tokbuff = realloc(list->list_of_tokbuff, list->size*sizeof(struct token_buffer) );
}
void token_delete_all()
{
    free(tbuff);
}

int main()
{

    return 0;
}

Comments