1. Trang chủ
  2. » Công Nghệ Thông Tin

slike bài giảng môn chương trình dịch chương 6 symbol tables

39 486 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 39
Dung lượng 143,33 KB

Nội dung

Symbol Tables Nguyen Hua Phung Faculty of IT HCMUT Faculty of IT - HCMUT Symbol Tables 2 Outline • Motivation • Linear Table • Hash Table Faculty of IT - HCMUT Symbol Tables 3 Motivation • Manipulation information binding to names • Keeping track of scope • Searched when a name is encountered → efficiently • dynamically grow Faculty of IT - HCMUT Symbol Tables 4 Symbol-table Entries • Binding information of a name – lexeme –type – address – scope • Read page 431 for details Faculty of IT - HCMUT Symbol Tables 5 Symbol-table Entries (cont’d) class SymEntry { String Lexeme; Type type; int address; } Faculty of IT - HCMUT Symbol Tables 6 Symbol-table Interface • enterScope(): – increase the scope level by 1 – invoked at the beginning of a scope • exitScope(): – remove all names at the current scope level – decrease the scope level by 1 – invoked at the end of a scope • insert(id): – look up the id in the current scope level – put the id into the symbol table – invoked at each declaration • lookup(id): – look up the id in the table – invoked at each applied occurrence of the id int foo(int a){…{int a;… …}…} int foo(int a){…{int a;… … a = foo(b + 1) / c; … Faculty of IT - HCMUT Symbol Tables 7 Example int a; int foo(int a) { int a; int b; b = a * 2; if (b > 0) { int c; c = foo(a); } } enterScope() insert(id) enterScope() insert(id) lookup(id) enterScope() insert(id) lookup(id) lookup(id) exitScope() Redeclared variable Faculty of IT - HCMUT Symbol Tables 8 Linear Table class SymLinearTable { Stack<SymEntry> symTable; Stack<Integer> scope; void enterScope() { put the next position of symTable into the scope stack } void exitScope() { remove entries above the position saved in the scope stack remove the top of the scope stack } 0 a foo 0 2 1 0 2 a foo 0 4 3 2 1 0 2 a b Faculty of IT - HCMUT Symbol Tables 9 Linear Table (cont’d) Object insert(SymEntry sym) { search from the top of symTable to the position saved in the top of scope if exists entry such that entry.Lexeme is the same as sym.Lexeme return entry push sym onto symTable } Object lookup(String .lexeme) { search from the top of symTable if exists entry such that entry.Lexeme is the same as lexeme return entry return null } } a foo 0 4 3 2 1 0 2 a b Faculty of IT - HCMUT Symbol Tables 10 Hash Table a m b foo c ma 6 5 4 3 2 1 0 [...]... SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 -1 2 -1 1 -1 0 a -1 0 } Faculty of IT - HCMUT Symbol Tables 16 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else return... scope.peek(); for (i = symTable.size()-1; i >= cScope; i ) { int h = hash(symTable[i].Lexeme); hashTable[h] = symTable[i].next; } symTable.setSize(cScope); scope.pop(); } Faculty of IT - HCMUT Symbol Tables 26 Hash Table (con’t) a → 0; Object lookup(String lexeme) { search from the top of symTable if exists entry such that entry.Lexeme is the same as lexeme return entry return null b → 1; foo → 0;... cScope; i ) { int h = hash(symTable[i].Lexeme); hashTable[h] = symTable[i].next; } symTable.setSize(cScope); scope.pop(); 4 3 b -1 a 1 2 -1 2 1 -1 1 foo 0 2 0 1 0 -1 0 a } Faculty of IT - HCMUT Symbol Tables 36 ... SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 b -1 -1 2 a 1 3 -1 1 foo -1 2 0 1 0 a 0 2 -1 0 } Faculty of IT - HCMUT Symbol Tables 14 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else... class SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 -1 2 -1 1 -1 0 0 } Faculty of IT - HCMUT Symbol Tables 15 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else... (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else return symTable[i]; sym.next = hashTable[h]; symTable.push(sym); hashTable[h] = symTable.size() - 1; return null; } Faculty of IT - HCMUT Symbol Tables 11 Hash Table (con’t) 2 class SymHashEntry extends SymEntry { int next; } class SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym)... SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 -1 2 -1 1 0 0 a -1 0 } Faculty of IT - HCMUT Symbol Tables 17 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else... SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 -1 2 -1 1 foo 0 0 0 -1 a 0 } Faculty of IT - HCMUT Symbol Tables 18 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else... SymHashTable extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 -1 2 -1 1 foo 0 1 0 -1 a 0 } Faculty of IT - HCMUT Symbol Tables 19 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else... extends SymLinearTable { Vector hashTable = new Vector(3); Object insert(SymHashEntry sym) { -1 -1 0 a → 0; b → 1; foo → 0; c →0 4 3 a 1 -1 2 -1 1 foo 0 2 1 0 -1 0 a } Faculty of IT - HCMUT Symbol Tables 20 Hash Table (con’t) 2 1 int h = hash(sym.Lexeme); int cScope = scope.peek(); for (i = hashTable[h]; i >= cScope; ) { if (!symTable[i].Lexeme.equals(sym.Lexeme)) i = symTable[i].next; else . Symbol Tables Nguyen Hua Phung Faculty of IT HCMUT Faculty of IT - HCMUT Symbol Tables 2 Outline • Motivation • Linear Table • Hash Table Faculty of IT - HCMUT Symbol Tables 3 Motivation •. IT - HCMUT Symbol Tables 4 Symbol- table Entries • Binding information of a name – lexeme –type – address – scope • Read page 431 for details Faculty of IT - HCMUT Symbol Tables 5 Symbol- table. null } } a foo 0 4 3 2 1 0 2 a b Faculty of IT - HCMUT Symbol Tables 10 Hash Table a m b foo c ma 6 5 4 3 2 1 0 Faculty of IT - HCMUT Symbol Tables 11 Hash Table (con’t) class SymHashEntry extends

Ngày đăng: 23/10/2014, 17:33

TỪ KHÓA LIÊN QUAN