c - Lex 的问题

标签 c linux lex

我正在用 C 语言编写一个软件。为此我使用 lex。我用C写了一段代码来创建一个symbol table并管理它。因此,每当 lex 找到新符号时,它就会将其放入符号表中。问题是,当我尝试打印符号表中的所有结果时,我得到了意想不到的输出。 例如,如果输入文件是:

int main(){}

输出应该是:

int
main
(
)
{
}

但是输出是:

int main(){}
main(){}
(){}
...

等等。 用于打印的函数是这样的

void print_entries(struct symtab *start) {
   struct symtab *s = start;
   while(s != NULL) {
      printf("%s\n", s->name);
      s = s->next;
   }
}

以下是添加新符号的代码:

void add_entry(char* name, int type, struct symtab *start)
{
   struct symtab *new;
   new = malloc(sizeof(struct symtab));
   last_entry(start)->next = new;
   new->name = name;
   new->type = type;
   new->next = NULL;
}

有什么想法吗?

最佳答案

您需要将符号名称复制到符号表条目中。如果由于某种特殊原因您的系统还没有 strdup(),则使用:

#include <string.h>
#include <stdlib.h>

char *strdup(const char *str)
{
   size_t len = strlen(str) + 1;
   char *dup = malloc(len);
   if (dup != 0)
       memmove(dup, str, len);
   return dup;
}

(在这种情况下,我可以安全地使用 memcpy();我使用 memmove() 因为它总是有效,并且 memcpy()不会。我使用 memmove() 因为我确切地知道字符串有多长,因此副本不需要测试每个字符是否为空。)

手边有strdup():

void add_entry(char* name, int type, struct symtab *start)
{
   struct symtab *sym;
   sym = malloc(sizeof(struct symtab));
   last_entry(start)->next = sym;
   sym->name = strdup(name);
   sym->type = type;
   sym->next = NULL;
}

请注意,这仍然忽略了两次内存分配的错误检查,这不是一个好习惯。我将其修改为使用 sym 而不是 new,因为后者是 C++ 关键字,我避免使用它们作为标识符,即使在 C 代码中也是如此。

关于c - Lex 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6171973/

相关文章:

parsing - 我应该如何在 yacc/bison 和 lex 中制定递归规则?

c++ - extern "C"和简单的 extern 之间的区别

linux - 从 Linux 命令行向 CMake 服务器发送请求

c - 在 lex 中只接受来自键盘的一个输入

linux - 如何通过命令行查找ubuntu操作系统中不同目录下的两个文本文件之间的差异

c++ - 在 Ubuntu 上的 Qt Creator 中运行 C++ 代码

bison - 从单独的程序调用 lex/yacc

c - 整数验证给出无限循环

c++ - 字典式查找对应于一小组值的数百万个键?

c - C中的指针赋值