Posts

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...

XML Namespace removal

std::string namespace_removal(const char* str) { std::string xml_str = str; size_t sz = xml_str.length(); int counter_startbrack = 0; int counter_endbrack = 0; int prev_sbrack= 0; std::cout << "String formatter before str process:" << str <<std::endl; int sbrack_pos = xml_str.find_first_of('<'); if(sbrack_pos != -1) counter_startbrack++;  int ebrack_pos = xml_str.find_first_of('>'); if(ebrack_pos != -1) counter_endbrack++; int slash = xml_str.find('/'); int prev_ebrack = 0; for(int i=sbrack_pos; i< sz; ) { std::cout << "I variable is:"<< i <<std::endl; /* int colon_pos = xml_str.find(':',i); if(colon_pos != -1 && sbrack_pos < colon_pos && sbrack_pos < ebrack_pos && colon_pos < ebrack_pos) { if( slash == sbrack_pos+1 && xml_str.at(sbrack_pos+1) == '/' ) { std::cout << "Slash...

Generic Stack

 #include <assert.h> #define Stack( stack_type, STACK_SIZE, STACK_OBJ )         \ struct st_node_##STACK_OBJ {                                                          \    stack_type stack[ STACK_SIZE ];         \    int top_element;                   \  };                                                       \ struct st_node_##STACK_OBJ STACK_OBJ;                      \ int STACK_OBJ##_is_empty( void )                           \ {    ...

Magic Table (to eliminate pointers)

 #include <stdio.h> #define TABLE_LIMIT 128 #define NULLP -32625 #define L_VAL 0 #define R_VAL 1 #define TYPE 2 int magic_table[3][TABLE_LIMIT]; typedef int pointer_t ; enum Types {     INT,     ADDR     }; int genAddr() {     static int count =0;     int base = 111000;          return base + count++; } int init_magic_table() {     int i;     for(i=0; i< TABLE_LIMIT; i++)     {         magic_table[L_VAL][i] = NULLP;          magic_table[R_VAL][i] = NULLP;     }        } void fill_magic_table(int n) {     int i;     for(i=0; i< n; i++)     {                  magic_table[L_VAL][i] = genAddr();        ...

Bitwise +,-,/*

 long long bitwise_div(long long dividend, long long int divisor) {     long long sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;     dividend = abs(dividend);     divisor = abs(divisor);     long long quotient = 0;     while (dividend >= divisor)     {         dividend -= divisor;         ++quotient;     }     return quotient * sign; } int bitwise_multi(int a, int b) {    int result = 0;    while (b > 0) {       if (b & 1) {          result += a;          }       a = a << 1;       b = b >> 1;    }    return result; } int bitwise_sub(int x, int y) {     while (y != 0)     {         int borrow = (~x) & y;         x = x ^ y;     ...

Expression resolution for assembly output with multi-character variable or digits

    /******************************************************************************       Expression Resolver v1.1         Designed by Daipayan Bhowal        Using Priority linked list to solve expression evaluation *******************************************************************************/ #include <stdio.h> #include <ctype.h> #include <stdlib.h> #include <string.h> #define OPERAND 255 #define OPERATOR -255 #define CONST 256 //#define INT 33 #define INIT_PRIORITY -1 long long bitwise_div(long long dividend, long long int divisor) {     long long sign = ((dividend < 0) ^ (divisor < 0)) ? -1 : 1;     dividend = abs(dividend);     divisor = abs(divisor);     long long quotient = 0;     while (dividend >= divisor)     {         dividend -= divisor;         ++quotient;     }  ...