Skip to content
Snippets Groups Projects
Commit 6a3a9c01 authored by zhaochaoyi's avatar zhaochaoyi
Browse files

Lab2 symbol table end

parent 6b8fc756
No related branches found
No related tags found
No related merge requests found
#ifndef STRUCT_H
#define STRUCT_H
#include <stdbool.h>
//语法树结构
typedef struct Tree_node_t {
......@@ -88,10 +88,12 @@ typedef struct Symbol_Node_t {
struct Symbol_Node_t * scope_prev, * scope_next;
}Symbol_Node_t;
extern struct Info_Node_Ops * nodeop;
struct SymbolInfoList_t {
Symbol_Node_t head,tail;
int list_cnt;
//在list链表节点cur之后插入新节点new !!!注意插入节点前要先malloc一个节点
//在list链表中删除cur节点, !!!注意:删除节点时候不free,在stack中pop的时候free;
};
......@@ -109,6 +111,7 @@ typedef struct SymbolTable_t {
void (*init)(int); //初始化哈希表
void (*insert)(Symbol_Node_t *); //插入节点
void (*remove)(Symbol_Node_t *); //删除节点
Symbol_Node_t * (*find)(char *); //查询元素,返回true,找到,false没有找到
}SymbolTable_t;
......
#line 1 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 1 "./lex.yy.c"
#line 3 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 3 "./lex.yy.c"
#define YY_INT_ALIGNED short int
......@@ -523,8 +523,8 @@ int yy_flex_debug = 0;
#define YY_MORE_ADJ 0
#define YY_RESTORE_YY_MORE_OFFSET
char *yytext;
#line 1 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 2 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 1 "./lexical.l"
#line 2 "./lexical.l"
#include <stdio.h>
#include "debug.h"
#include "syntax.tab.h"
......@@ -535,8 +535,8 @@ char *yytext;
extern int syntax;
int last_lineno = 0,cur_lineno = 0;
static Node_t* add_node_text(char * content,char *text,int len);
#line 538 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 539 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 538 "./lex.yy.c"
#line 539 "./lex.yy.c"
#define INITIAL 0
......@@ -753,9 +753,9 @@ YY_DECL
}
{
#line 44 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 44 "./lexical.l"
#line 758 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 758 "./lex.yy.c"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
......@@ -814,163 +814,163 @@ do_action: /* This label is used only to access EOF actions. */
case 1:
YY_RULE_SETUP
#line 45 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 45 "./lexical.l"
{ ELEMENT("STRUCT"); return STRUCT; }
YY_BREAK
case 2:
YY_RULE_SETUP
#line 46 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 46 "./lexical.l"
{ ELEMENT("RETURN"); return RETURN; }
YY_BREAK
case 3:
YY_RULE_SETUP
#line 47 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 47 "./lexical.l"
{ ELEMENT("IF"); return IF; }
YY_BREAK
case 4:
YY_RULE_SETUP
#line 48 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 48 "./lexical.l"
{ ELEMENT("ELSE"); return ELSE; }
YY_BREAK
case 5:
YY_RULE_SETUP
#line 49 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 49 "./lexical.l"
{ ELEMENT("WHILE"); return WHILE; }
YY_BREAK
case 6:
YY_RULE_SETUP
#line 50 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 50 "./lexical.l"
{ ELEMENT("TYPE"); return TYPE; }
YY_BREAK
case 7:
YY_RULE_SETUP
#line 51 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 51 "./lexical.l"
{ ELEMENT("INT"); return INT;}
YY_BREAK
case 8:
YY_RULE_SETUP
#line 52 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 52 "./lexical.l"
{ ELEMENT("FLOAT"); return FLOAT;}
YY_BREAK
case 9:
YY_RULE_SETUP
#line 53 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 53 "./lexical.l"
{ ELEMENT("ID"); return ID; }
YY_BREAK
case 10:
YY_RULE_SETUP
#line 54 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 54 "./lexical.l"
{ ELEMENT("SEMI"); return SEMI;}
YY_BREAK
case 11:
YY_RULE_SETUP
#line 55 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 55 "./lexical.l"
{ ELEMENT("COMMA"); return COMMA;}
YY_BREAK
case 12:
YY_RULE_SETUP
#line 56 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 56 "./lexical.l"
{ ELEMENT("ASSIGNOP"); return ASSIGNOP;}
YY_BREAK
case 13:
YY_RULE_SETUP
#line 57 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 57 "./lexical.l"
{ ELEMENT("RELOP"); return RELOP;}
YY_BREAK
case 14:
YY_RULE_SETUP
#line 58 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 58 "./lexical.l"
{ ELEMENT("PLUS"); return PLUS;}
YY_BREAK
case 15:
YY_RULE_SETUP
#line 59 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 59 "./lexical.l"
{ ELEMENT("MINUS"); return MINUS;}
YY_BREAK
case 16:
YY_RULE_SETUP
#line 60 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 60 "./lexical.l"
{ ELEMENT("STAR"); return STAR;}
YY_BREAK
case 17:
YY_RULE_SETUP
#line 61 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 61 "./lexical.l"
{ ELEMENT("DIV"); return DIV;}
YY_BREAK
case 18:
YY_RULE_SETUP
#line 62 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 62 "./lexical.l"
{ ELEMENT("AND"); return AND;}
YY_BREAK
case 19:
YY_RULE_SETUP
#line 63 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 63 "./lexical.l"
{ ELEMENT("OR"); return OR;}
YY_BREAK
case 20:
YY_RULE_SETUP
#line 64 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 64 "./lexical.l"
{ ELEMENT("DOT"); return DOT;}
YY_BREAK
case 21:
YY_RULE_SETUP
#line 65 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 65 "./lexical.l"
{ ELEMENT("NOT"); return NOT;}
YY_BREAK
case 22:
YY_RULE_SETUP
#line 66 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 66 "./lexical.l"
{ ELEMENT("LP"); return LP;}
YY_BREAK
case 23:
YY_RULE_SETUP
#line 67 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 67 "./lexical.l"
{ ELEMENT("RP"); return RP;}
YY_BREAK
case 24:
YY_RULE_SETUP
#line 68 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 68 "./lexical.l"
{ ELEMENT("LB"); return LB;}
YY_BREAK
case 25:
YY_RULE_SETUP
#line 69 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 69 "./lexical.l"
{ ELEMENT("RB"); return RB;}
YY_BREAK
case 26:
YY_RULE_SETUP
#line 70 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 70 "./lexical.l"
{ ELEMENT("LC"); return LC;}
YY_BREAK
case 27:
YY_RULE_SETUP
#line 71 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 71 "./lexical.l"
{ ELEMENT("RC"); return RC;}
YY_BREAK
case 28:
YY_RULE_SETUP
#line 72 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 72 "./lexical.l"
{ strcat(linetext," ");}
YY_BREAK
case 29:
YY_RULE_SETUP
#line 73 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 73 "./lexical.l"
{ yycolumn += 3; strcat(linetext,"\t");}
YY_BREAK
case 30:
/* rule 30 can match eol */
YY_RULE_SETUP
#line 74 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 74 "./lexical.l"
{ yycolumn = 1; yylineno += 1; memset(linetext,0,sizeof(linetext)); }
YY_BREAK
case 31:
YY_RULE_SETUP
#line 75 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 75 "./lexical.l"
{ char c = input(); while(c != '\n') { c = input(); } yylineno += 1; }
YY_BREAK
case 32:
YY_RULE_SETUP
#line 76 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 76 "./lexical.l"
{
char c1 = input(),c2 = input();
if (c1 == '\n') {
......@@ -993,15 +993,15 @@ YY_RULE_SETUP
YY_BREAK
case 33:
YY_RULE_SETUP
#line 95 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 95 "./lexical.l"
{ LexicalError("%s",yytext); syntax++; }
YY_BREAK
case 34:
YY_RULE_SETUP
#line 97 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 97 "./lexical.l"
ECHO;
YY_BREAK
#line 1004 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lex.yy.c"
#line 1004 "./lex.yy.c"
case YY_STATE_EOF(INITIAL):
yyterminate();
......@@ -2006,7 +2006,7 @@ void yyfree (void * ptr )
#define YYTABLES_NAME "yytables"
#line 97 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/lexical.l"
#line 97 "./lexical.l"
extern int syntax;
......
File added
......@@ -11,20 +11,35 @@ typedef SymbolInfoList_t list_t;
typedef struct SymbolStack_ele_t stack_ele_t;
//都应该使用static
static void node_init();
static void node_delete(void * cur,int mode);
static bool node_equal(unit_t*,unit_t*);
static struct Info_Node_Ops {
void (*init)();
void (*delete)(void *,int);
bool (*equal)(unit_t*,unit_t*);
}InfoNodeOp = {
.init = node_init,
.delete = node_delete,
.equal = node_equal,
};
struct Info_Node_Ops * nodeop = &InfoNodeOp;
static void SymbolInfoList_insert(list_t * list,unit_t * cur,unit_t * new);
static void SymbolInfoList_remove(list_t * list,unit_t * cur);
static void SymbolInfoList_init(list_t * list);
static unit_t * SymbolInfoList_find(unit_t * cur);
static unit_t * SymbolInfoList_find(list_t * list,char * name);
static list_t * SymbolInfoList_alloc();
static struct Hash_List_Ops {
void (*insert)(list_t * list,unit_t * cur,unit_t * new); //在list链表节点cur之后插入新节点new !!!注意插入节点前要先malloc一个节点
void (*remove)(list_t * list,unit_t * cur); //在list链表中删除cur节点, !!!注意:删除节点时候不free,在stack中pop的时候free;
void (*init)(list_t * list); //初始化
unit_t * (* find)(unit_t * cur);
unit_t * (* find)(list_t * list,char * name);
list_t * (*alloc)();
}listop = {
.insert = SymbolInfoList_insert,
......@@ -38,10 +53,11 @@ static struct Hash_List_Ops {
//hash table list 接口
static void SymbolTable_init(int size);
static unit_t * SymbolTable_node_alloc();
static void SymbolTable_init(int size);
static void SymbolTable_insert(unit_t * cur);
static void SymbolTable_remove(unit_t * cur);
static unit_t * SymbolTable_find(char *);
//SymbolTable API
......@@ -59,6 +75,7 @@ MODULE_DEF(SymbolTable_t,symbol_table) = {
.init = SymbolTable_init,
.insert = SymbolTable_insert,
.remove = SymbolTable_remove,
.find = SymbolTable_find,
};
......@@ -131,6 +148,16 @@ static void SymbolTable_remove(unit_t * cur) {
}//说明见最后,这里不free
//Symbol Table
static unit_t * SymbolTable_find(char * name) {
int id = symbol_table->hash(name);
list_t * list = symbol_table->table[id];
if(list == NULL) {
return NULL;
} else {
return listop.find(list,name);
}
}
/*
* 在stack中,unit_t *的指针作用
......@@ -207,7 +234,6 @@ static stack_ele_t * SymbolStack_top() {
} else {
Log("The Symbol Stack is empty!");
assert(0);
return NULL;
}
}
//Symbol Stack
......@@ -215,13 +241,22 @@ static stack_ele_t * SymbolStack_top() {
//Symbol List
static void SymbolInfoList_insert(list_t * list,unit_t * cur,unit_t * new) {
unit_t * next = cur->hash_next, * prev = cur->hash_prev;
prev->hash_next = next;
next->hash_prev = prev;
unit_t * next = cur->hash_next;
cur->hash_next = new;
new->hash_next = next;
next->hash_prev = new;
new->hash_prev = cur;
list->list_cnt++;
}
static void SymbolInfoList_init(list_t * list) {
list->list_cnt = 0;
list->head.hash_next = &list->tail;
list->tail.hash_prev = &list->head;
list->head.hash_prev = list->tail.hash_next = NULL;
list->head.type = list->tail.type = HASHLIST;
}
static void SymbolInfoList_remove(list_t * list,unit_t * cur) {
......@@ -229,9 +264,10 @@ static void SymbolInfoList_remove(list_t * list,unit_t * cur) {
assert(prev != NULL && next != NULL);
prev->hash_next = next;
next->hash_prev = prev;
list->list_cnt++;
}
static unit_t * SymbolInfoList_find(unit_t * cur) {
static unit_t * SymbolInfoList_find(list_t * list,char * name) {
}
......@@ -241,6 +277,20 @@ static list_t * SymbolInfoList_alloc() {
//Symbol List
//Symbol Info node ops
static void node_init() {
}
static void node_delete(void * cur,int mode) {
}
static bool node_equal(unit_t* n1,unit_t* n2) {
}
//Symbol Info node
/*
每次向散列表中插入元素时,总是将新插入的元素放到该槽下挂的链表以及该层
所对应的链表的表头。每次查表时如果定位到某个槽,则按顺序遍历这个槽下挂的链表并返回
......
This diff is collapsed.
......@@ -35,8 +35,8 @@
especially those whose name start with YY_ or yy_. They are
private implementation details that can be changed or removed. */
#ifndef YY_YY_HOME_PPX_ZHAOCHAOYI_COMPILER_LAB2_CODE_SYNTAX_TAB_H_INCLUDED
# define YY_YY_HOME_PPX_ZHAOCHAOYI_COMPILER_LAB2_CODE_SYNTAX_TAB_H_INCLUDED
#ifndef YY_YY_SYNTAX_TAB_H_INCLUDED
# define YY_YY_SYNTAX_TAB_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
......@@ -91,11 +91,11 @@ extern int yydebug;
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 31 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/syntax.y"
#line 31 "./syntax.y"
struct Tree_node_t * node;
#line 99 "/home/ppx/zhaochaoyi/Compiler/Lab2/Code/syntax.tab.h"
#line 99 "./syntax.tab.h"
};
typedef union YYSTYPE YYSTYPE;
......@@ -124,4 +124,4 @@ extern YYLTYPE yylloc;
int yyparse (void);
#endif /* !YY_YY_HOME_PPX_ZHAOCHAOYI_COMPILER_LAB2_CODE_SYNTAX_TAB_H_INCLUDED */
#endif /* !YY_YY_SYNTAX_TAB_H_INCLUDED */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment