符号表与索引生成器:从基础到 C 语言交叉引用
1. 引言
在许多flex或bison程序中,符号表是一个关键组件,用于跟踪输入中使用的名称。本文将从一个简单的索引生成程序开始,逐步引导到一个更复杂的 C 语言交叉引用程序。
2. 索引生成器
2.1 符号表管理
符号表在编译器领域是一个重要的概念,在索引生成器中,符号表用于跟踪每个单词及其出现的文件和行号。以下是索引生成器的声明部分代码:
/* fb2-4 text concordance */ %option noyywrap nodefault yylineno case-insensitive /* the symbol table */ %{ struct symbol { /* a word */ char *name; struct ref *reflist; }; struct ref { struct ref *next; char *filename; int flags; int lineno; }; /* simple symtab of fixed size */ #define NHASH 9997 struct symbol symtab[NHASH]; struct symbol *lookup(char*); void addref(int, char*, char*,int); char *curfilename;