C:未定义的引用错误

时间:2016-03-30 04:18:40

标签: c gcc

我正在构建一个递归语言,但我遇到了一个未定义的引用错误。 (该计划仍未完成)

如何构建我的头文件,以便修复此错误?

错误

/tmp/cc4CBg4R.o: In function `morestmts':
main.c:(.text+0x197): undefined reference to `stmtlist'
collect2: error: ld returned 1 exit status

head.h

#include "stdio.h"
#include "string.h"
#include "stdbool.h"

char file[4096][20];
int cur = 0;
int lines = 0;
bool test = true;

void digit();
void variable();
void expr();
void testexpr();
void whilestmt();
void ifstmt();
void assign();
void morestmts();
void stmt();
void stmlist();
void block();
void program();
int singleCharCheck(char check);
void trim(char * s);

的main.c

#include "head.h"

int main(){
 int x=0;
 while(fgets(file[x], 20, stdin) && x < 4096){
  trim(file[x]);
  x++;
  if(test){printf("%d = %s\n",x,file[x-1]);}
 }
 lines = x;

 program();

 return 0;
}

void digit(){ //1 | 2 | 3
}

void variable(){ //a | b | c
}

void expr(){ //+ [expr] [expr] | * [expr] [expr]
 //operator
 expr();
 expr();
}

void testexpr(){ //[variable] <= [expr]
 variable();
 // <=
 expr();
}



void whilestmt(){ //while [testexpr] do [stmt]
 //while
 testexpr();
 //do
 stmt();
}

void ifstmt(){ //if [testexpr] then [stmt] else [stmt]
 // if
 testexpr();
 // then
 stmt();
 //else
 stmt();
}

void assign(){ //[variable] = [expr]
 variable();
 // =
 expr();
}

void stmt(){ //[assign] | [ifstmt] | [whilestmt]
 assign();
 ifstmt();
 whilestmt();
}

void morestmts(){ //; [stmtlist] |
 // ;
 stmtlist();
 // |
}

void stmlist(){ //[stmt] [morestmts]
 stmt();
 morestmts();
}

void block(){ //begin [stmlist] end
 if(strcmp(file[cur],"begin") == 0){
  cur++;
  stmlist();
  if(strcmp(file[cur],"end") == 0){
  } else {
   printf("%d ERROR: no \"end\" on your block\n",cur);
  }
 } else {
  printf("%d ERROR: no \"begin\" where there should be\n",cur);
 }
}

void program(){ // program : program [block] .
 if(strcmp(file[cur],"program") == 0){
  cur++;
  block();
  if(singleCharCheck('.') == 1){
  } else {
   printf("%d ERROR: symbol \".\" missing at end of program\n");
  }
 } else {
  printf("\n%d ERROR: keyword \"program\" not located\n",cur);
 }
}

int singleCharCheck(char check){
 int i;
 for(i = 0; i < strlen(file[cur]); i++){
  if(file[cur][i] == check){
   return 1;
  }
 }
 return 0;
}

void trim(char * s) {
    char * p = s;
    int l = strlen(p);

    while(isspace(p[l - 1])) p[--l] = 0;
    while(* p && isspace(* p)) ++p, --l;

    memmove(s, p, l + 1);
}

1 个答案:

答案 0 :(得分:0)

您输错了stmtlist()。它应该是stmlist()