CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.
-
Upload
brittany-clopton -
Category
Documents
-
view
239 -
download
1
Transcript of CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.
![Page 1: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/1.jpg)
CPSC 388 – Compiler Design and Construction
Symbol Tables and Static Checking
![Page 2: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/2.jpg)
Compiler Design
Lexical Analyzer(Scanner)
Syntax Analyzer(Parser)
Symantic Analyzer
Intermediate CodeGenerator
Optimizer
Code Generator
Symbol Table
![Page 3: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/3.jpg)
Introduction
Parser ensures proper syntax but cannot (or does not) check for: A variable should not be declared more than
once in the same scope. A variable should not be used before being
declared. The type of the left-hand side of an assignment
should match the type of the right-hand side. Methods should be called with the right number
and types of arguments.
![Page 4: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/4.jpg)
Static Semantic Analyzer
Traverses the Abstract Syntax Tree: For each scope in the program: Process the
declarations, adding new entries to the symbol table and reporting any variables that are multiply declared; process the statements, finding uses of undeclared variables, and updating the "ID" nodes of the abstract-syntax tree to point to the appropriate symbol-table entry.
Process all of the statements in the program again, using the symbol-table information to determine the type of each expression, and finding type errors.
![Page 5: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/5.jpg)
Symbol Table Holds names declared in a program:
Classes Fields Methods Variables
Each name in Symbol Table has attributes: Kind of name Type Nesting level Location at runtime
![Page 6: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/6.jpg)
Scoping Rules
Say when a program is allowed to reuse a name
Match use of a name with the corresponding declaration
![Page 7: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/7.jpg)
Reusing Names in Java?
class Test { int Test;
void Test( ) {int Test;…
}}
also reuse names for more than one methodas long as the number and/or types of parametersare unique
![Page 8: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/8.jpg)
Reusing Names in C/C++void f(int k) {
int x = 0; /* x is declared here */while (...) {
int x = 1; /* another x is declared */...if (...) {
float x = 5.5; /*another x */...
}}
}
![Page 9: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/9.jpg)
Matching Uses to Declarations
Static Scoping Determination of match made at compile
time Dynamic Scoping
Determination of match made at runtime Java, C, and C++ use Static Scoping
![Page 10: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/10.jpg)
C and C++ Scoping Rules “Most Closely Nested” rule
A use of variable x matches the declaration in the most closely enclosing scope such that the declaration precedes the use.
Scoping Levels One outermost scope includes function names
and global variables Each Function has one (or more) scopes
One scope for parameters and “top-level” declarations
One scope for each block in the function C++ has a scope for each for loop, i.e. you can
declare variables in for-loop header
![Page 11: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/11.jpg)
You Try It(What reuses are allowed in Java?)class animal {
// methodsvoid attack(int animal) {
for (int animal=0; animal<10; animal++) {int attack;
}}int attack(int x) {
for (int attack=0; attack<10; attack++) {int animal;
}}void animal() { }// fieldsdouble attack;int attack;int animal;
}
![Page 12: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/12.jpg)
You Try ItMatch uses to declarations (in C++)
int k=10, x=20;
void foo(int k) { int a = x; int x = k; int b = x; while (...) { int x;
if (x == k) { int k, y; k = y = x;
}if (x == k) {
int x = y;}
}}
![Page 13: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/13.jpg)
Dynamic Scoping
A use of a variable that has no corresponding declaration in the same function corresponds to the declaration in the most-recently-called still active function.
![Page 14: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/14.jpg)
Example Dynamic Scopingvoid main() { f1(); f2();}void f1() { int x = 10; g();}void f2() { String x = "hello"; f3(); g();}void f3() { double x = 30.5;}void g() { print(x);}
Output: “10 hello”
![Page 15: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/15.jpg)
You Try It: What is Output?void main() { int x = 0; f1(); g(); f2();}void f1() { int x = 10; g();}void f2() { int x = 20; f1(); g();}void g() { print(x);}
![Page 16: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/16.jpg)
Using Names Before Defining(in Java)class Test { void f() { val = 0; g(); x = 1; int x; }
void g() {} int val;}
// field val has not yet been declared -- OK// method g has not yet been declared -- OK// variable x has not yet been declared -- ERROR!
![Page 17: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/17.jpg)
Scoping in Our (C--) Language uses static scoping requires that all names be declared before
they are used does not allow multiple declarations of a
name in the same scope (even for different kinds of names)
does allow the same name to be declared in multiple nested scopes (but only once per scope)
uses the same scope for a method's parameters and for the local variables declared at the beginning of the method
![Page 18: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/18.jpg)
Symbol Table Given a declaration of a name, is
there already a declaration of the same name in the current scope (i.e., is it multiply declared)?
Given a use of a name, to which declaration does it correspond (using the "most closely nested" rule), or is it undeclared?
![Page 19: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/19.jpg)
Symbol Table Operations Look up a name in the current scope only
(to check if it is multiply declared). Look up a name in the current and
enclosing scopes (to check for a use of an undeclared name, and to link a use with the corresponding symbol-table entry).
Insert a new name into the symbol table with its attributes.
Do what must be done when a new scope is entered.
Do what must be done when a scope is exited.
![Page 20: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/20.jpg)
Symbol Table
Entries in Table Symbol Name Type Nesting Level of declaration
Use List of Hashtables
DeclarationsIn S
Declarations made in scopesThat enclose S
![Page 21: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/21.jpg)
Example Symbol Tablevoid f(int a, int b) { double x; while (...) { int x, y; ... }}void g() { f();}
x: int, 3y: int, 3
a: int, 2b: int, 2x: double, 2
f: (int, int), 1g: (), 1
![Page 22: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/22.jpg)
Operations Performed duringScope Analysis On scope entry: increment the current level number
and add a new empty hashtable to the front of the list.
To process a declaration of x: look up x in the first table in the list. If it is there, then issue a "multiply declared variable" error; otherwise, add x to the first table in the list.
To process a use of x: look up x starting in the first table in the list; if it is not there, then look up x in each successive table in the list. If it is not in any table then issue an "undeclared variable" error.
On scope exit, remove the first table from the list and decrement the current level number.
![Page 23: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/23.jpg)
Type Checking
Determine the type of each expression in the program (each node in the AST that corresponds to an expression).
Find type errors.
![Page 24: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/24.jpg)
Type Rules
specify, for every operator (including assignment), what types the operands can have, and what is the type of the result.
Examples: Addition of int and double: result double
(in java and C++) Assignment of double to int: C++ OK,
Java error
![Page 25: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/25.jpg)
Table of Operators and Types(in Java)
Operator 1st Operand 2nd Operand Result
+ int int int
+ int double double
= double int double
&& boolean boolean boolean
< int int boolean
…
![Page 26: CPSC 388 – Compiler Design and Construction Symbol Tables and Static Checking.](https://reader036.fdocuments.in/reader036/viewer/2022081504/5519b1fe55034667578b4628/html5/thumbnails/26.jpg)
Context Method Call Type Errors
Context Type Errors the condition of an if statement the condition of a while loop the termination condition part of a for loop
Method Call Type Errors calling something that is not a method calling a method with the wrong number of
arguments calling a method with arguments of the wrong
types