如何创建符号表?
我可以使用 malloc 添加符号表条目吗?如何遍历表来检查是否已经存在某些内容?
Can I use malloc to add symbol table entries? How do I traverse the table to check if something is already there?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“符号表”并不描述特定类型的数据结构。它仅描述主要操作模式:添加符号和按名称检索符号。这里的符号基本上是属性名称。对于编译器类,这样的属性之一可以是 IsAFunction。
C 语言的内置数据结构非常少。在这种情况下,您必须自己创建一个。在 C++ 中,这只是一个
std::map
的问题。现在想必,如果您在编译器类中,您应该已经知道如何在 C 中实现数据结构(包括malloc()
的使用)。如果没有,那么编译器类确实不适合您。A "symbol table" doesn't describe a particular kind of data structure. It merely describes the primary modes of operation: adding symbols and retrieving symbols by name. Symbols here are basically attributed names. For a compiler class, one such an attribute could be
IsAFunction
.C has very few built-in datastructures. You'd have to create one yourself in this case. In C++, it would just be a matter of a
std::map<std::string, Attributes>
. Now presumably if you're in a compiler class, you should already know how to implement datastructures in C (including the use ofmalloc()
). If not, then a compiler class really isn't for you.一般来说,符号表是通过哈希表实现的。哈希表具有 O(1) 存储和检索的优点,但它们不按顺序存储数据。
假设您使用 C 语言工作,您可以使用
malloc()
,但它需要更多的工作。提供的链接应该能让您有所启发。In general, symbol tables are implemented through hash tables. Hash tables have the advantage of O(1) store and retrieve, but they don't store data sequentially.
Assuming you're working in C you can uses
malloc()
, but it requires more work than that. The provided link should enlighten you.我之前用双链表做到了这一点。但现在我肯定会用哈希表来做到这一点。
它只是一个数据结构。
I'v done that with a double chained linked list before. But now i will definitively do it with a hashtable.
It's just a datastructure.