-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsymtab.h
More file actions
81 lines (66 loc) · 2 KB
/
symtab.h
File metadata and controls
81 lines (66 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/************************************************/
/* File: symtab.h */
/* Symbol table interface for the C- compiler */
/* tallate */
/************************************************/
#ifndef _SYMTAB_H
#define _SYMTAB_H
#include <stdio.h>
#include "globals.h"
/* SIZE 为hash table的大小
*/
#define SIZE 211
/* SHIFT 为hash function中两项间的乘数
*/
#define SHIFT 4
/* LineList 记录变量被引用的行号
*/
typedef struct LineListRec {
int lineno;
struct LineListRec *next;
} LineList;
/* BucketList 记录每个变量
* 包括其id、所处嵌套层次、声明的位置、出现行号、内存位置
*/
typedef struct BucketListRec {
char *name;
LineList *lines;
int nestLevel;
TreeNode *decl;
int memloc;
struct BucketListRec *next;
} BucketList;
/* SeparateTable 代表每个声明scope
*/
typedef struct SymbolTable {
BucketList *hashTable[SIZE];
/* 该scope中分配的相对地址, */
int location;
int maxOffset; /* 考虑复合语句的嵌套,最多的变量偏移 */
/* 嵌套层次 */
int nestLevel;
int err;
struct SymbolTable *next;
} SymbolTable;
/* 多scope符号表,在symbol.c中定义 */
extern SymbolTable *symbolTable;
/* st_insert 将行号和内存地址插入符号表(loc = memory location)
* 只有第一次有效,之后的忽略(?)
*/
void st_insert(char *name, TreeNode *dec, int lineno, int loc);
/* st_lookup 返回变量对应的BucketList
* 若找到则返回其指针
* 若scopes用完或者depth用完则终止,并返回NULL(depth赋值为-1表示搜索所有scope)
*/
BucketList *st_lookup(char *name, int depth);
/* st_augment 在原symbolTable的基础上增加一个scope
*/
void st_augment();
/* st_abrade 提取一个最近的scope并返回
*/
SymbolTable *st_abrade();
/* printSymTab 打印格式化的符号表到listing
* 打印当前scope的符号表
*/
void printSymTab(FILE *listing);
#endif // _SYMTAB_H