I have started to learn C recently. I don't know what's wrong with my code. I keep getting:
undefined reference to 'fsm'
collect2: error: ld returned 1 exit status
Can you please help to fix it?
1) This is my fsm_main.c file.
#include <stdio.h>
#include <string.h>
#include "cstack.h"
#include "fsm.h"
int main(){
char string_input[100];
printf("Enter a string: ");
gets(string_input);
printf("string : %s\n",string_input);
if (fsm(string_input))
printf("All matched correctly\n");
return 0;
}
2) Boolean.h file
#ifndef BOOLEAN_H
#define BOOLEAN_H
typedef int boolean;
#define TRUE 1
#define FALSE 0
#endif
3) cstack.h file
#ifndef CSTACK_H
#define CSTACK_H
#include "boolean.h"
typedef struct cstacknode{
char data;
struct cstacknode *next;
}*cstack;
void init_cstack(cstack *);
void cpush(cstack*,char);
char cpop(cstack *);
boolean is_cfull(void);
boolean is_cempty(cstack);
void print_cstack(cstack);
#endif
4) cstack.c file
#include <stdio.h>
#include <stdlib.h>
#include "boolean.h"
#include "cstack.h"
void init_cstack(cstack *s){
(*s) = NULL;
}
void cpush(cstack*s, char x){
cstack temp;
temp= (cstack)malloc(sizeof(struct cstacknode));
temp -> data = x;
temp -> next = (*s);
(*s) = temp;
}
char cpop(cstack *s){
cstack temp;
char data_popped;
temp = *s;
data_popped =temp->data;
*s =temp->next;
free(temp);
return data_popped;
}
void print_cstack( cstack s){
if(!is_cempty(s)){
printf("%d\n", s->data);
print_cstack(s->next);
}
}
boolean is_cfull(void){
cstack temp;
temp = (cstack) malloc (sizeof(struct cstacknode));
if(temp == NULL)
return TRUE;
else {
free(temp);
return FALSE;
}
}
boolean is_cempty( cstack s){
if (s == NULL)
return TRUE;
else
return FALSE;
}
5) fsm.h file
#ifndef FSM_H
#define FSM_H
#include "boolean.h"
boolean fsm(char *);
boolean is_open(char);
boolean is_close(char);
boolean is_brother(char,char);
#endif
6) fsm.c file
#include <stdio.h>
#include <stdlib.h>
#include "fsm.h"
#include "cstack.h"
boolean is_open(char c) {
return ((c == '{') || (c == '[') || (c == '{'));
}
boolean is_close(char c) {
return ((c == '}') || (c == ']') || (c == '}'));
}
boolean is_brother(char op, char cl) {
return (((op == '{') && (cl == '}')) || ((op == '[') && (cl == ']')) || ((op == '{') && (cl == '}')));
}
boolean fsm(char my_input[]) {
int i = 1;
int nextstate = 0;
cstack top;
char o, c;
init_cstack( & top);
while (1) {
switch (nextstate) {
case 0:
i++;
if (is_open(my_input[i]))
nextstate = 1;
else if (is_close(my_input[i]))
nextstate = 2;
else if (my_input[i] == '0')
nextstate = 3;
else
nextstate = 4;
break;
case 1:
if (!is_cfull()) {
cpush( & top, my_input[i]);
nextstate = 0;
} else {
printf("Error! stack is full\n");
exit(0);
}
break;
case 2:
if (!is_cempty(top)) {
if (is_brother(cpop( & top), my_input[i])) {
nextstate = 0;
} else {
// printf("Error! open and close delimiter do not match\n");
printf("Error! open and close delimiters do not match\n");
return FALSE;
}
} else {
printf("Error! No open delimiter to compare to\n");
return FALSE;
}
break;
case 3:
if (is_cempty(top)) {
return TRUE;
}
else {
printf("Error! stack is full\n");
return FALSE;
}
break;
case 4:
nextstate = 0;
}
}
}