Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE 152: Compiler Design February 7 Class Meeting

Similar presentations


Presentation on theme: "CMPE 152: Compiler Design February 7 Class Meeting"— Presentation transcript:

1 CMPE 152: Compiler Design February 7 Class Meeting
Department of Computer Engineering San Jose State University Spring 2019 Instructor: Ron Mak

2 Program: Pascal Tokenizer
Verify the correctness of the Pascal token subclasses. Verify the correctness of the Pascal scanner. Demo (Chapter 3)

3 Assignment #2 Write a scanner for the C++ language.
Add a new C++ front end.

4 Quick Review of the Framework
Chapter 4 Quick Review of the Framework TO: FROM: Our next topic: The symbol table

5 The Symbol Table: Basic Concepts
Purpose To store information about certain tokens during the translation process (i.e., parsing and scanning) What information to store? Anything that’s useful! For an identifier: name data type how it’s defined (as a variable, type, function name, etc.)

6 The Symbol Table: Basic Operations
Enter new information. Look up existing information. Update existing information.

7 The Symbol Table: Conceptual Design
Goal: The symbol table should be source language independent. Each entry in the symbol table has a name attributes At the conceptual level, we don’t worry about implementation.

8 What to Store in Each Symbol Table Entry
Each symbol table entry is designed to store information about an identifier. The attribute keys indicate what information we will store for each type of identifier. Store common information in fixed fields (e.g., lineNumbers) and store identifier type-specific information as attributes. enum class SymTabKeyImpl {     // Constant.     CONSTANT_VALUE,     // Procedure or function.     ROUTINE_CODE, ROUTINE_SYMTAB, ROUTINE_ICODE,     ROUTINE_PARMS, ROUTINE_ROUTINES,     // Variable or record field value.     DATA_VALUE }; wci/intermediate/symtabimpl/SymTabEntryImpl.h

9 What Needs a Symbol Table?
A Pascal program Identifiers for constant, type, variable, procedure, and function names. A Pascal procedure or function Identifiers for formal parameter (argument) names. A Pascal record type Identifiers for field names.

10 The Symbol Table Stack Language constructs can be nested.
Procedures and functions are nested inside a program. Procedures and functions can be nested inside of each other. Record types are defined within programs, procedures, and functions. Record types can be nested inside of each other. Therefore, symbol tables need to be kept on a symbol table stack.

11 The Symbol Table Stack, cont’d
Whichever symbol table is on top of the stack is the local symbol table. The first symbol table created (the one at the bottom of the stack) is the global symbol table. It stores the predefined information, such as entries for the names of the standard types integer, real, char, and boolean. During the translation process, symbol tables are pushed onto and popped off the stack … … as the parser enters and exits nested procedures, functions, record types, etc. Global symbol table

12 The Symbol Table Stack, cont’d
For now, we’ll have only have a single symbol table. Therefore, the local symbol table is the global symbol table. We won’t need multiple symbol tables until we start to parse declarations. Implementing the symbol table stack now will make things easier for us later. Global symbol table

13 Modifications to Class PascalParserTD
    while ((token = next_token(token)) != nullptr)     {         TokenType token_type = token->get_type();         last_line_number = token->get_line_number();         Object value = token->get_value();         string type_str;         string value_str;         if (token_type == (TokenType) PT_IDENTIFIER)         {             string name = to_lower(token->get_text());             SymTabEntry *entry = symtab_stack->lookup(name);             if (entry == nullptr) entry = symtab_stack->enter_local(name);             entry->append_line_number(token->get_line_number());         }         else if (token_type == (TokenType) PT_ERROR)             PascalErrorCode error_code =                                     (PascalErrorCode) cast(value, int);             error_handler.flag(token, error_code, this); Cross-reference only identifiers. If it’s not already in the symbol table, create a new entry. wci/frontend/pascal/PascalParserTD.cpp

14 Cross-Reference Listing
A cross-reference listing verifies the symbol table code: ./Chapter4cpp compile -x newton.pas Modifications to the main Pascal class: Pascal.cpp     parser->parse();     source->close();     symtab_stack = parser->get_symtab_stack();     icode = parser->get_icode();     if (xref)     {         CrossReferencer cross_referencer;         cross_referencer.print(symtab_stack);     } A new utility class CrossReferencer generates the cross-reference listing. Demo

15 Quick Review of the Framework
Chapter 5 Quick Review of the Framework FROM: TO: Next topic: Parsing assignment statements and expressions, and generating parse trees.

16 Pascal Statement Syntax Diagrams

17 Pascal Statement Syntax Diagrams, cont’d
For now, greatly simplified!


Download ppt "CMPE 152: Compiler Design February 7 Class Meeting"

Similar presentations


Ads by Google