Managing the Symbol Table - Hampden-Sydney...
Transcript of Managing the Symbol Table - Hampden-Sydney...
![Page 1: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/1.jpg)
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
![Page 2: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/2.jpg)
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
![Page 3: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/3.jpg)
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
![Page 4: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/4.jpg)
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
![Page 5: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/5.jpg)
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
![Page 6: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/6.jpg)
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
![Page 7: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/7.jpg)
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
![Page 8: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/8.jpg)
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
![Page 9: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/9.jpg)
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
![Page 10: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/10.jpg)
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
![Page 11: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/11.jpg)
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
![Page 12: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/12.jpg)
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
![Page 13: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/13.jpg)
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
![Page 14: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/14.jpg)
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
![Page 15: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/15.jpg)
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
![Page 16: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/16.jpg)
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
![Page 17: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/17.jpg)
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
![Page 18: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/18.jpg)
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
![Page 19: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/19.jpg)
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
![Page 20: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/20.jpg)
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
![Page 21: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/21.jpg)
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
![Page 22: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/22.jpg)
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
![Page 23: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/23.jpg)
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
![Page 24: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/24.jpg)
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
![Page 25: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/25.jpg)
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
![Page 26: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/26.jpg)
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
![Page 27: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/27.jpg)
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
![Page 28: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/28.jpg)
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
![Page 29: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/29.jpg)
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
![Page 30: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/30.jpg)
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
![Page 31: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/31.jpg)
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
![Page 32: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/32.jpg)
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
![Page 33: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/33.jpg)
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
![Page 34: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/34.jpg)
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
![Page 35: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/35.jpg)
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
![Page 36: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/36.jpg)
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
![Page 37: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/37.jpg)
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
![Page 38: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/38.jpg)
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
![Page 39: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/39.jpg)
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
![Page 40: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/40.jpg)
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
![Page 41: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/41.jpg)
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
![Page 42: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/42.jpg)
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
![Page 43: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/43.jpg)
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
![Page 44: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/44.jpg)
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
![Page 45: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/45.jpg)
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
![Page 46: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/46.jpg)
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
![Page 47: Managing the Symbol Table - Hampden-Sydney Collegepeople.hsc.edu/faculty-staff/robbk/Coms480/Lectures/Spring 2015/Lecture...Managing the Symbol Table Lecture 21 Sections 2.7, 5.1 Robb](https://reader030.fdocuments.net/reader030/viewer/2022041212/5dd109f6d6be591ccb63ec28/html5/thumbnails/47.jpg)
Assignment
HomeworkRead Sections 2.7 and 5.1.
Robb T. Koether (Hampden-Sydney College) Managing the Symbol Table Fri, Mar 6, 2015 26 / 26