1. Chapter 5 – Symbol Table
Prepared by:
Suhailan bin Dato’ Hj. Safei
suhailan@udm.edu.my
019-555 9677
Block B, Deputy Dean Room
Reference Book:
ALSU, Compilers(Principles, Techniques & Tools)
2. Recap
Semantic Analysis is done using attribute grammar
◦ Attributes – binding values/types to the tokens
– Annotated Parse Tree
◦ Grammar – semantic rules for each CFG (production)
– Dependency Graphs
2 kind of attribute grammar
◦ Synthesized – assigning value to all node (eg.input “2+8”)
Inherited – assigning type to all node (eg. Input “int x, y, z”)
3. Tradeoffs of Attribute
Grammar
Synthesized attributes are easy to compute, but
difficult to be used to express semantics.
Inherited attributes are difficult to compute, but are
easy to express the semantics
AG is only for non side effect statement (change
values of others not related to the return values of
functions themselves). Or we can say that AG is
used to evaluate single line of program statement
such as “2+3+1;” or “float a,b,c;”.
4. Tradeoffs of Attribute
Grammar
Without using global data (>1 program statements)
to create side effects, some of the semantic actions
cannot be performed such as
◦ Checking whether a variable is declared before its usage
int a;
b=3;
◦ Checking the type of a variable
int a; float b;
a=b;
◦ Checking whether a variable is used or not
int a,b;
b=1;
To process the above semantic error, SYMBOL
TABLE can be used.
6. 6
Scope
Scope of identifier
◦ portion of program where identifier can be
referred to
Lexical scope
◦ Statement block
◦ Method body
◦ Class body
◦ Module / package / file
◦ Whole program (multiple modules)
7. 7
Scope example
class Foo {
int value;
int test() {
int b = 3;
return value + b;
}
void setValue(int c) {
value = c;
{ int d = c;
c = c + d;
value = c;
}
}
}
class Bar extends Foo {
int value;
void setValue(int c) {
value = c;
test();
}
}
scope of
field value
scope of
local variable b
scope of formal
parameter c
scope of c
scope of value
scope of local variable
in statement block d scope of
method test
8. 8
Scope tree
Generally scope hierarchy forms a tree
class Foo {
int value;
int test() {
int b = 3;
return value + b;
}
void setValue(int c) {
value = c;
{ int d = c;
c = c + d;
value = c;
}
}
}
Foo
value
test
b
setValue
c
block I
d
block I
9. 9
Subclasses
Scope of subclass enclosed in scope of its
superclass
Subtype relation must be acyclic
Class Foo {
int a;
}
Class Bar extends Foo {
}
Bar sees “a” as well
10. 10
Scope hierarchy
Global scope
◦ The names of all classes defined in the program
Class scope
◦ Instance scope: all fields and methods of the class
◦ Static scope: all static methods
◦ Scope of subclass nested in scope of its superclass
Method scope
◦ Formal parameters and local variables
◦ Variables defined in block
Code block scope
◦ Variables defined in block
12. 12
Symbol table
An environment that stores information
about identifiers
A data structure that captures scope
information
Symbol Kind Type Properties
value field int …
test method -> int private
setValue method int -> void public
13. 13
Symbol table
Each entry in symbol table contains
◦ name of an identifier
◦ kind (variable/method/field…)
◦ Type (int, string, myClass…)
◦ Additional properties, e.g., final, public
One symbol table for each scope
14. 14
Scope nesting
Symbol Kind Type Properties Global
Symbol Kind Type Properties Class
Symbol Kind Type Properties Method
Symbol Kind Type Properties Block
names of all classes
fields and methods
formals + locals
variables defined in block
Scope nesting mirrored in hierarchy of symbol tables
15. 15
class Foo {
int value;
int test() {
int b = 3;
return value + b;
}
void setValue(int c) {
value = c;
{ int d = c;
c = c + d;
value = c;
}
}
}
class Bar {
int value;
void setValue(int c) {
value = c;
}
}
scope of value
scope of b
scope of c
scope of c
scope of value
scope of d
Symbol table example
block1
16. 16
class Foo
{
int value;
int test()
{
int b = 3;
return value + b;
}
void setValue(int c)
{
value = c;
{ int d = c;
c = c + d;
value = c;
}
}
}
Symbol table example
Symbol Kind Type Properties
value field int …
test method -> int
setValue method int -> void
(Foo)
Symbol Kind Type Properties
b var int …
(test)
Symbol Kind Type Properties
c var int …
(setValue)
Symbol Kind Type Properties
d var int …
(block1)
17. 17
Symbol Kind Type Properties
value field int …
test method -> int
setValue method int -> void
Symbol Kind Type Properties
b var int …
Symbol Kind Type Properties
c var int …
Symbol Kind Type Properties
d var int …
(Foo)
(test)
…
Symbol table example cont.
(setValue)
(block1)
18. 18
Checking scope rules
Symbol Kind Type Properties
value field int …
test method -> int
setValue method int -> void
Symbol Kind Type Properties
b var int …
Symbol Kind Type Properties
c var int …
Symbol Kind Type Properties
d var int …
(Foo)
(test) (setValue)
(block1)
void setValue(int c) {
value = c;
{ int d = c;
c = c + d;
value = c;
}
}
lookup(value)
19. 19
Symbol Kind Type Properties
value field int …
test method -> int
setValue method int -> void
Symbol Kind Type Properties
b var int …
Symbol Kind Type Properties
c var int …
Symbol Kind Type Properties
d var int …
(Foo)
(test) (setValue)
(block1)
void setValue(int c) {
value = c;
{ int d = c;
c = c + d;
myValue = c;
}
}
lookup(myValue)
Error !
undefined
symbol
Catching semantic errors
20. Create Symbol TAble
class PROTOTAIP_MERAH
{
char GRED='E';
String KUIZSIMPATI(int MARKAH)
{
String KASIHAN;
if (MARKAH == 0)
{
char GRED1 = 'E';
char GRED2 = 'A';
KASIHAN = "Takziah! Anda berjaya dapat "+GRED1;
}
else
{
KASIHAN = "Tahniah! Anda gagal mendapat "+GRED;
}
return KASIHAN;
}
int a;
}