From 4c58e94a4a0a7185bcfb8697163200466a7ab95a Mon Sep 17 00:00:00 2001 From: Hizenberg469 Date: Sun, 12 Jan 2025 23:08:11 +0530 Subject: [PATCH] Data structures ready!! --- Makefile | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ include/fsm.h | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 Makefile create mode 100644 include/fsm.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..fba10d8 --- /dev/null +++ b/Makefile @@ -0,0 +1,75 @@ +#Setting a default configuration if invoked with just "make": +CFG ?= debug + +# Config specific settings +ifeq ($(CFG),debug) +CFLAGS += -DDEBUG +else +CFLAGS += -O3 -DNDEBUG +endif + +inform: +ifneq ($(CFG),release) +ifneq ($(CFG),debug) + @echo "Invalid configuration "$(CFG)" specified." + @echo + @echo "Possible choices for configuration are 'CFG=release' and 'CFG=debug'" + @exit 1 +endif +endif + @echo "Configuration "$(CFG) + @echo "--------------------------------------------------" + +rule : dep | inform + +BUILD_DIR = build/$(CFG) +OBJS_DIR = $(BUILD_DIR)/objs +#-------------------------------------------------- + + +BINARY=$(BUILD_DIR)/fsm +CODEDIRS=. ./src +INCDIRS=. ./include/ # can be list + +CC=gcc +OPT=-O0 +#generate files that encode make rules for the .h dependencies +DEPFLAGS=-MP -MD +#automatically add the -I onto each include directory +CFLAGS=-Wall -Wextra -g $(foreach D,$(INCDIRS),-I$(D)) $(OPT) $(DEPFLAGS) + +# for-style iteration (foreach) and regular expression completions (wildcard) +CFILES=$(foreach D, $(CODEDIRS),$(wildcard $(D)/*.c)) +# regular expression replacement +OBJECTS=$(patsubst %.c,$(OBJS_DIR)/%.o,$(CFILES)) +DEPFILES=$(patsubst %.c,$(OBJS_DIR)/%.d,$(CFILES)) + +all: $(BINARY) + +$(BINARY): $(OBJECTS) + $(CC) -o $@ $^ + +# only want the .c file dependency here, thus $< instead of $^. +# +%.o:%.c + $(CC) $(CFLAGS) -c -o $@ $< + +clean: + rm -rf $(BINARY) $(OBJECTS) $(DEPFILES) + +# shell commands are a set of keystrokes away +distribute: clean + tar zcvf dist.tgz * + +# @ silences the printing of the command +# $(info ...) prints output +diff: + $(info The status of the repository, and the volume of per-file changes:) + @git status + @git diff --stat + +# include the dependencies +-include $(DEPFILES) + +# add .PHONY so that the non-targetfile - rules work even if a file with the same name exists. +.PHONY: all clean distribute diff \ No newline at end of file diff --git a/include/fsm.h b/include/fsm.h new file mode 100644 index 0000000..15ca01c --- /dev/null +++ b/include/fsm.h @@ -0,0 +1,77 @@ +#ifndef __FSM__ +#define __FSM__ + +typedef struct fsm_ fsm_t; +typedef struct state_ state_t; + +#define MAX_INP_BUFFER_SIZE 128 +#define MAX_TRANSITION_TABLE_SIZE 128 +#define MAX_STATE_NAME_SIZE 32 +#define MAX_FSM_NAME_SIZE 32 +#define MAX_TRANSITION_KEY_SIZE 64 + +/*Custom-defined datatype +* to define boolean value +* for FSM*/ +typedef enum{ + + FSM_FALSE, + FSM_TRUE + +}fsm_bool_t; + + +/*This data structure act similar +* to the behaviour of transition +* function.*/ +typedef struct tt_entry_{ + + /* The input symbol */ + char transition_key[MAX_TRANSITION_KEY_SIZE]; + + /* Size of the input symbol */ + unsigned int transition_key_size; + + /* Next state: The state which is the output + * of transition function, i.e. + * f(state_i, input_symbol) = (state_i+1)*/ + state_t *next_state; + +}tt_entry_t; + +typedef struct tt_{ + tt_entry_t tt_entry[MAX_TRANSITION_TABLE_SIZE]; +}tt_t; + +struct state_{ + + /*Name/identifier of the state. + * This should be unique*/ + char state_name[MAX_STATE_NAME_SIZE]; + + /*Transition table of the state*/ + tt_t state_trans_table; + + /*Boolean value to distinguish b/w + * accept/final state or vice-versa*/ + fsm_bool_t is_final; +}; + +struct fsm_{ + + /*Initial state of FSM to start with*/ + state_t *initial_state; + + /*Name of FSM*/ + char fsm_name[MAX_FSM_NAME_SIZE]; + + /*Input data fed by application to parse + * by FSM library*/ + unsigned int input_buffer_size; + + /*Pointer which point to the current input + * from input buffer*/ + unsigned int input_buffer_cursor; +}; + +#endif \ No newline at end of file