SlideShare a Scribd company logo
1 of 20
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)
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”)
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;”.
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.
Topics
 Scope
 Symbol Table
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
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
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
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
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
11
How to check scope?
 Using Symbol Table
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
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
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
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
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
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
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
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
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;
}

More Related Content

Similar to Chapter 5.ppt

cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
WatchDog13
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
SukhpreetSingh519414
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
FALLEE31188
 

Similar to Chapter 5.ppt (20)

OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
C++ Overview PPT
C++ Overview PPTC++ Overview PPT
C++ Overview PPT
 
C++
C++C++
C++
 
cppt-170218053903 (1).pptx
cppt-170218053903 (1).pptxcppt-170218053903 (1).pptx
cppt-170218053903 (1).pptx
 
Bc0037
Bc0037Bc0037
Bc0037
 
CPP-overviews notes variable data types notes
CPP-overviews notes variable data types notesCPP-overviews notes variable data types notes
CPP-overviews notes variable data types notes
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Unit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptxUnit 2 CMath behind coding.pptx
Unit 2 CMath behind coding.pptx
 
Session 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdfSession 6_Java Interfaces_Details_Programs.pdf
Session 6_Java Interfaces_Details_Programs.pdf
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Session 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .pptSession 6_Interfaces in va examples .ppt
Session 6_Interfaces in va examples .ppt
 
Core java
Core javaCore java
Core java
 
C++ language
C++ languageC++ language
C++ language
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
Core C#
Core C#Core C#
Core C#
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
 
Introduction to c++
Introduction to c++Introduction to c++
Introduction to c++
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
White Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR ChandigarhWhite Box testing by Pankaj Thakur, NITTTR Chandigarh
White Box testing by Pankaj Thakur, NITTTR Chandigarh
 
C++ version 1
C++  version 1C++  version 1
C++ version 1
 

Recently uploaded

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
aroranaina404
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
instagramfab782445
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
suhanimunjal27
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
home
 
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion pills in Kuwait Cytotec pills in Kuwait
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
amitlee9823
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
tbatkhuu1
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 

Recently uploaded (20)

FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts ServiceVVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
VVIP CALL GIRLS Lucknow 💓 Lucknow < Renuka Sharma > 7877925207 Escorts Service
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
infant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptxinfant assessment fdbbdbdddinal ppt.pptx
infant assessment fdbbdbdddinal ppt.pptx
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Th...
 
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
Recommendable # 971589162217 # philippine Young Call Girls in Dubai By Marina...
 
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...Top Rated  Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
Top Rated Pune Call Girls Saswad ⟟ 6297143586 ⟟ Call Me For Genuine Sex Serv...
 
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman MuscatAbortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
Abortion Pills in Oman (+918133066128) Cytotec clinic buy Oman Muscat
 
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 105, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
Brookefield Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore...
 
Case Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, PuneCase Study of Hotel Taj Vivanta, Pune
Case Study of Hotel Taj Vivanta, Pune
 
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Vasundhra (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
Editorial design Magazine design project.pdf
Editorial design Magazine design project.pdfEditorial design Magazine design project.pdf
Editorial design Magazine design project.pdf
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...AMBER GRAIN EMBROIDERY | Growing folklore elements |  Root-based materials, w...
AMBER GRAIN EMBROIDERY | Growing folklore elements | Root-based materials, w...
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
HiFi Call Girl Service Delhi Phone ☞ 9899900591 ☜ Escorts Service at along wi...
 
Q4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentationQ4-W4-SCIENCE-5 power point presentation
Q4-W4-SCIENCE-5 power point presentation
 

Chapter 5.ppt

  • 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
  • 11. 11 How to check scope?  Using Symbol Table
  • 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; }