Tải bản đầy đủ (.pdf) (39 trang)

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

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (143.33 KB, 39 trang )

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
Faculty of IT - HCMUT Symbol Tables 11
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {

Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
Faculty of IT - HCMUT Symbol Tables 12
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];

symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
4
3
2
1
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
a -
1
-1
2
1
0
-1
-1
-1
3
1
foo 0
0
a 1
2

b -
1
0
2
-1
-1
Faculty of IT - HCMUT Symbol Tables 13
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
4
3
2

1
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
a -
1
-1
2
1
0
-1
-1
-1
3
1
foo 0
0
a 1
2
b -
1
0
2
-1
-1
Faculty of IT - HCMUT Symbol Tables 14
Hash Table (con’t)

class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
4
3
2
1
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
a -1

-1
2
1
0
-1
-1
-1
3
1
foo 0
0
a 1
2
b -1
0
2
-1
-1
Faculty of IT - HCMUT Symbol Tables 15
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
-1
0
Faculty of IT - HCMUT Symbol Tables 16

Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3

2
1
0
2
1
0
-1
-1
-1
0
a -1
Faculty of IT - HCMUT Symbol Tables 17
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;

}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
0
0
a -1
Faculty of IT - HCMUT Symbol Tables 18
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);

Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0

-1
-1
0
0
a -1
foo 0
Faculty of IT - HCMUT Symbol Tables 19
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2

1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
1
0
a -1
foo 0
Faculty of IT - HCMUT Symbol Tables 20
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
1
0

a -1
foo 0
2
a 1
Faculty of IT - HCMUT Symbol Tables 21
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0

-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
2
0
a -1
foo 0
2
a 1
Faculty of IT - HCMUT Symbol Tables 22
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
-1
2
0

a -1
foo 0
2
a 1
b -1
Faculty of IT - HCMUT Symbol Tables 23
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1

0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
3
2
0
a -1
foo 0
2
a 1
b -1
Faculty of IT - HCMUT Symbol Tables 24
Hash Table (con’t)
class SymHashEntry extends SymEntry {
int next;
}
class SymHashTable extends SymLinearTable {
Vector<Integer> hashTable = new Vector(3);
Object insert(SymHashEntry sym) {
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 symTable[i];
sym.next = hashTable[h];
symTable.push(sym);
hashTable[h] = symTable.size() - 1;
return null;
}
0
a → 0; b → 1;
foo → 0; c →0
2
1
0
-1
-1
-1
4
3
2
1
0
2
1
0
-1
3

2
0
a -1
foo 0
2
a 1
b -1
Faculty of IT - HCMUT Symbol Tables 25
Hash Table (con’t)
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
}
void exitScope() {
int cScope = 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();
}

×