linux c 符号表,C中的符号表
我目前正在開發一種執行模式匹配的靜態分析工具.我正在使用Flex生成詞法分析器,我編寫了代碼來管理符號表.我對C不太熟悉,所以我決定將符號表實現為線性鏈表.
#include
#include
#include
struct symtab {
int id;
char *name;
int type;
struct symtab *next;
};
enum types {
KEYWORD = 1,
CONSTANT,
IDENTIFIER,
OPERATOR,
DELIMITER,
WHITESPACE
};
struct symtab *last_entry(struct symtab *start)
{
struct symtab *p;
p = start;
while(p -> next != NULL) {
p = p -> next;
}
return p;
}
void add_entry(char* name, int type, struct symtab *start)
{
struct symtab *new;
new = last_entry(start);
int id;
if(new == start) {
new = start;
id = 0;
}
else {
new = malloc(sizeof(struct symtab));
id = last_entry(start) -> id;
last_entry(start) -> next = new;
}
new -> id = id + 1;
new -> name = name;
new -> type = type;
new -> next = NULL;
}
struct symtab *find_entry(char* name, struct symtab *start)
{
struct symtab *p;
p = start;
while(p -> next != NULL) {
if(strcmp(p -> name, name) == 0) {
return p;
}
}
}
但是,當我使用add_entry()添加符號,然后嘗試使用find_entry()查找它們時,find_entry()返回null.有人可以幫忙嗎?
解決方法:
看起來您試圖將列表表示為標題對象(開始),然后是列表的實際元素.這是一個好主意,因為它簡化了空列表的情況,但你沒有得到正確的實現.
添加時,您需要刪除為last_entry啟動時獲得的特殊情況代碼.起始節點永遠不會包含符號數據.
查找時,您必須確保跳過頭(開始),因為它不包含符號數據.查找代碼中的第二個錯誤是當p-> next為NULL時停止搜索(這意味著你永遠不能返回列表中的最后一個元素.)當p為NULL時你應該停止.
當然,您根本不應該使用鏈表:哈希表是更好的選擇,因為它具有更好的性能和內存效率.
標簽:c-3,linux,symbol-table
來源: https://codeday.me/bug/20190621/1254598.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的linux c 符号表,C中的符号表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux做软raid10,51CTO博
- 下一篇: Linux的使用(常见)