managing the symbol table - hampden-sydney...
TRANSCRIPT
Managing the Symbol TableLecture 21
Sections 2.7, 5.1
Robb T. Koether
Hampden-Sydney College
Fri, Mar 6, 2015
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 1 / 26
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 2 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 3 / 26
Symbol Table Members
Symbol Table MembersLinkedList id_tableint level
id_table – The linked list of hashtables of symbols.level – The current level in the linked list.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 4 / 26
Symbol Table Functions
Symbol Table Functionsvoid init()
void init_keywords()
void install_keyword()
void enter_block()
void leave_block()
IdEntry id_lookup(String name, int blk_lev)
IdEntry install(String name, int blk_lev)
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 5 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 6 / 26
The Function init()
The Function init()id_table = new LinkedList<Hashtable<String, IdEntry>>();id_table.addLast(null);
Create a linked list.Create a null hash table at level 0 (node 0).
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 7 / 26
The Function init_keywords()
The Function init_keywords()install_res_word(sym.INT, "int");install_res_word(sym.DOUBLE, "double");
...
Initialize the symbol table with all the keywords.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 8 / 26
The Function install_keyword(kw)
The Function install_keyword(kw)IdEntry p = install(kw, 1);p.type = t;
Install the keyword s at level 1 of the symbol table.The install() function returns a reference to the IdEntry inthe table.Set the type attribute to t.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 9 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 10 / 26
The Function enter_block()
The Function enter_block()level++;id_table.addLast(new Hashtable<String, IdEntry>());
This function creates a new hash table at the next higher level.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 11 / 26
The Function leave_block()
The Function leave_block()id_table.removeLast();level-;
This function removes the hash table from the highest level.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 12 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 13 / 26
The Function id_lookup(name, blk_lev)
The Function id_lookup()Identry id = null;Hashtable<String, Identry> ht = id_table.get(blk_lev);id = ht.get(name);
If blk_lev is positive, then id_lookup() searches only thehash table at that level.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 14 / 26
The Function id_lookup(name, blk_lev)
The Function id_lookup()Identry id = null;for (int i = id_table.size() - 1; i > 0; i-){
Hashtable<String, Identry> ht = id_table.get(i);id = ht.get(name);if (id != null) break;
}
If blk_lev is 0, then id_lookup() searches all hash tables,beginning at the highest level.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 15 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 16 / 26
The Function install(name, blk_lev)
The Function install()Hashtable<String, Identry> ht = id_table.get(blk_lev);IdEntry id = new IdEntry();id.name = s;id.block_level = blk_lev;ht.put(s, id);
If blk_lev is 0, then install() installs the identifier at thecurrent level, level.If blk_lev is positive, then install() installs the identifier atthat level.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 17 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 18 / 26
The Lexer and the Symbol Table
The lexer must distinguish between identifiers and keywords.We could write a separate regular expression for each keyword:"int" {return new Symbol(sym.INT)}
However, this would make the lexer much more complicated.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 19 / 26
The Lexer and the Symbol Table
When a token consists of a string of letters, the lexer will checklevel 1 of the symbol table.If the string is found there, then the lexer will return theappropriate keyword token.If the string is not found there, then the lexer will assume that it isan identifier and return an identifier token.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 20 / 26
The Function lookup(name)
The Function lookup(name)IdEntry entry = SymbolTable.id_lookup(name, 1);if (entry != null)
return new Symbol(entry.type);else
return new Symbol(sym.ID, s);
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 21 / 26
The Function lookup(name)
The lookup() function is placed in the directive section of theflex file, within %{ and %}.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 22 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 23 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
Processing the statement int count;
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"int"
The Lexer sees “int”
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"int"
lookup("int", 1)
Search the Symbol Table for keyword “int”
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"int"
not null
Keyword “int” is found
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"int" sym.INT
Send the INT token to the Parser
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"int" sym.INT type ::= INT
The Parser matches type→ INT
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count"
The Lexer sees “count”
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count"
lookup("count", 1)
Search the Symbol Table for keyword “count”
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count"
null
Keyword “count” is not found
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count"sym.ID, "count"
Send the ID token to the Parser
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count" lval ::= ID
The Parser matches lval → ID
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count" lval ::= IDid("count")
Invoke the Semantic Action id("count")
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count"
id_lookup("count", 0)
lval ::= ID
Look up “count” in the Symbol Table
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count" lval ::= ID
null
“count” is not found
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
"count" lval ::= ID
install("count", 0)
IdEntryname = "count"
Install “count” in the Symbol Table
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";"
IdEntryname = "count"
The Lexer sees “;”
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";"sym.SEMI
IdEntryname = "count"
Send the SEMI token to the Parser
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";"sym.SEMI
dcl ::= type ID ;
IdEntryname = "count"
The Parser matches dcl → type ID ;
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";" dcl ::= type ID ;dcl("count", 0)
IdEntryname = "count"
Invoke the Semantic Action dcl("count", 0)
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";"
install("count", 0)
dcl ::= type ID ;
IdEntryname = "count"
Invoke install("count", 0)
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";" dcl ::= type ID ;
IdEntryname = "count"
IdEntry
Retrieve the IdEntry for count
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Managing the Symbol Table
Lexer ParserSemantic
Action
SymbolTable
";" dcl ::= type ID ;
IdEntryname = "count"type = INT
Update the type of count to INT
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 24 / 26
Outline
1 Symbol Table FunctionsInitialization Functionsenter_block() and leave_block()id_lookup()install()
2 The Lexer and the Symbol Table
3 An Example
4 Assignment
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 25 / 26
Assignment
HomeworkRead Sections 2.7 and 5.1.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 26 / 26