Upload
rebecca-lambert
View
212
Download
0
Embed Size (px)
Citation preview
Attribute Grammar Examples Attribute Grammar Examples and Symbol Tables and Symbol Tables
66.648 Compiler Design Lecture (02/23/98)66.648 Compiler Design Lecture (02/23/98)
Computer ScienceComputer Science
Rensselaer PolytechnicRensselaer Polytechnic
Lecture OutlineLecture Outline
• ExamplesExamples
• Symbol TablesSymbol Tables
• AdministrationAdministration
ExampleExample
Question 5.5, 5.4 and 5.8 in the text book.Question 5.5, 5.4 and 5.8 in the text book.
Qn: 5.5 Give a syntax directed translation scheme Qn: 5.5 Give a syntax directed translation scheme to differentiate expressions.to differentiate expressions.
D/dx (x*x + x) = 1*x + x* 1 + 1.D/dx (x*x + x) = 1*x + x* 1 + 1.
How do we do it? How do we do it?
Yet Another Example Yet Another Example qn 5.8 To get the value of a binary number:qn 5.8 To get the value of a binary number:
s --> L.L | Ls --> L.L | L
L --> L B | BL --> L B | B
B --> 0 | 1B --> 0 | 1
We can do the problem in two steps:We can do the problem in two steps:
S--> L and S --> .L . Then how do we combineS--> L and S --> .L . Then how do we combine
Qn. 5 .4 Give a syntax directed translation scheme Qn. 5 .4 Give a syntax directed translation scheme to remove extra parentheses.to remove extra parentheses.
(( x * x)) = x * x(( x * x)) = x * x
(x*x) +x x * x +x(x*x) +x x * x +x
(x+x) *x = (x+x) * x(x+x) *x = (x+x) * x
How do we do this in terms of attributes?How do we do this in terms of attributes?
Yet Another ExampleYet Another Example
In this example, we will explore how we take care In this example, we will explore how we take care of identifiers and types.of identifiers and types.
Simple Declarative languageSimple Declarative language
Symbol TableSymbol Table
The symbol tables - repository of all information The symbol tables - repository of all information within a compiler. All parts of a compiler within a compiler. All parts of a compiler communicate thru these table and access the communicate thru these table and access the data-symbols. Symbol tables are also used to data-symbols. Symbol tables are also used to hold labels, constants, and types.hold labels, constants, and types.
Symbol tables map names ( constants, identifiers, Symbol tables map names ( constants, identifiers, label numbers) into sets of symbols. Different label numbers) into sets of symbols. Different names have different attributes. - for identifiers, names have different attributes. - for identifiers, it could be its type, its location in a stack frame, it could be its type, its location in a stack frame, its storage class etc.its storage class etc.
Symbol Tables- ContdSymbol Tables- Contd
Symbols are collected into symbol tables. The Symbols are collected into symbol tables. The symbol-table module manages symbols and symbol-table module manages symbols and tables.tables.
Symbol Management should also deal with scope Symbol Management should also deal with scope and visibility that is imposed by the language.and visibility that is imposed by the language.
Symbol Tables are usually lists of hash tables, Symbol Tables are usually lists of hash tables, one for each scope.one for each scope.
Symbol Table- ContdSymbol Table- Contd
A typical table (see Fraser and Hanson book page A typical table (see Fraser and Hanson book page 40)40)
typedef struct table *Table; typedef struct table *Table;
struct table {struct table {
int level ; /* scope value */int level ; /* scope value */
Table previous;Table previous;
struct entry { struvt symbol sym; struct entry { struvt symbol sym;
struct entry *link; } *buckets[256];struct entry *link; } *buckets[256];
Symbol all; } ;Symbol all; } ;
Symbol Table- ContdSymbol Table- ContdThe buckets field is an array of pointers to the hash The buckets field is an array of pointers to the hash
chains. Previous field points to the table of the chains. Previous field points to the table of the enclosing scope. In each table structure all heads enclosing scope. In each table structure all heads a list of symbols in this and enclosing scopes.a list of symbols in this and enclosing scopes.
Symbol Table entries are (can be ) non-uniform Symbol Table entries are (can be ) non-uniform (variable sized). Depending upon what the (variable sized). Depending upon what the identifier represents. (We will see how a symbol is identifier represents. (We will see how a symbol is defined soon).defined soon).
Symbol Table look-up is performed in the context of Symbol Table look-up is performed in the context of a current scope , global, local, structured type.a current scope , global, local, structured type.
Symbol Table InterfaceSymbol Table Interface
Create_scope(parent_scope) - return index for a Create_scope(parent_scope) - return index for a new scope contained in a parent scope.new scope contained in a parent scope.
Insert_scope(scope,name) - insert identifier into Insert_scope(scope,name) - insert identifier into the specified scope of a symbol table.the specified scope of a symbol table.
Lookup(scope,name) - look up identifier in Lookup(scope,name) - look up identifier in specified scope. specified scope.
Delete_scope(scope) - delete specified scope and Delete_scope(scope) - delete specified scope and all symbol table entries that it containsall symbol table entries that it contains
SymbolsSymbols
Typedef struct symbol *Symbol;Typedef struct symbol *Symbol;
struct symbol { char *name; struct symbol { char *name;
int scope; int scope;
Coordinates src; Symbol up; Coordinates src; Symbol up;
List uses; int sclass; /*storgeclass*/List uses; int sclass; /*storgeclass*/
float ref;float ref;
union { constants,,function symbols, union { constants,,function symbols, globals, temporaries} u;globals, temporaries} u;
Xsymbol x; /* debugger infmtion */ } ;Xsymbol x; /* debugger infmtion */ } ;
U fieldU field
The u field supplies additional data for labels, The u field supplies additional data for labels, structure and union types, static variables and structure and union types, static variables and temporary variables. temporary variables.
We will discuss this in later classes. We will discuss this in later classes.
Comments and FeedbackComments and Feedback
Please read chapter 6 and look at the sample Please read chapter 6 and look at the sample compilers in the course home page to get an compilers in the course home page to get an over all picture. All the programs discussed to-over all picture. All the programs discussed to-day are also in the home page.day are also in the home page.