commons_old.h
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
enum keywords
{
TYPEDEF,
EXTERN,
STATIC,
AUTO,
REGISTER,
CONST,
VOLATILE,
VOID,
CHAR,
SHORT,
INT,
LONG,
FLOAT,
DOUBLE,
SIGNED,
UNSIGNED,
STRUCT,
UNION,
ID,
ICONST,
FCONST
};
int arr[15];
//char* arr[15];
int tok;
int block_level = 0;
static int i =0;
void nextToken()
{
tok = arr[i];
i++;
}
int lookupToken()
{
return arr[i+1];
}
int getTokenNumber()
{
return i;
}
typedef enum boolean
{
FALSE,
TRUE
}_bool;
typedef enum error_type
{
_FATAL_ERR,
_WARN_ERR,
_CONTINUE_ERR
} errtype;
struct err
{
char *err_msg;
errtype type;
int token_number;
int line;
char *func;
};
/**** error is not for logs, but for crashing the software at end, instead of crashing inside the parser ***/
/**** parser will try n combinations, after that it will decide whether to exit the software or not ***/
struct err **e;
int error_lines =0;
void init_error()
{
e = (struct err**) malloc(sizeof(struct err* ));
}
void make_error(char *error_message, errtype typ,int token_no, int line, const char *func)
{
char length_err = strlen(error_message);
char length_func = strlen(func);
e[error_lines] = (struct err*) malloc(sizeof(struct err));
e[error_lines]->err_msg =(char*) malloc(length_err);
strncpy(e[error_lines]->err_msg,error_message, length_err);
e[error_lines]->token_number = token_no;
e[error_lines]->type = typ;
e[error_lines]->line = line;
e[error_lines]->func =(char*) malloc(length_func);
strncpy(e[error_lines]->func,func, length_func);
error_lines++;
e = (struct err**) realloc( e , (error_lines + 1) * sizeof(struct err* ));
}
void show_error()
{
int k=0;
for(k=0; k < error_lines; k++)
{
printf("%s:%d %s token pos is %d", e[k]->func, e[k]->line, e[k]->err_msg, e[k]->token_number);
free( e[k]);
}
free(e);
}
Comments
Post a Comment