程序代写代做代考 data structure compiler Java c++ ER algorithm Compiler Design Week 6

Compiler Design Week 6

Detailed content
Weekly program
 Week  Week  Week  Week  Week
 Week
 Week  Week  Week  Week  Week  Week  Week
1 – Introduction to Compiler Design 2 – Lexical Analysis
3 – CD Programming Language
4 – Syntax Analysis
5 – Top Down Parsing
6 – Symbol Table and Error Recovery
7 – Bottom-Up Parsing
8 – Stack Machine (SM) Architecture 9 – Semantic Analysis
10 – Code Generation
11 – Code Optimisation
12 – Revision
13 – Extra revision (if needed)
2
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au

Week 06 Lecture Outline
Error Recovery and Symbol Table
 Types of Errors
 Error Recovery Strategies
 Error Recovery in LL parsing
 Error Recovery in Table Driven parsing
 Error Recovery in Recursive Descent parsing  Error Reporting
 Symbol Table : Role, Information, Use
 Symbol Table Interface
 Symbol Table Format
 Scoped Symbol Table
3
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au

Types of Errors
• A good compiler should assist in identifying and locating errors
– Lexical errors: important, compiler can easily recover and continue
– Syntax errors: most important for compiler, can almost always recover
– Static semantic errors: important, can sometimes recover
– Dynamic semantic errors: hard or impossible to detect at
compile time, runtime checks are required
– Logical errors: hard or impossible to detect
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
4

Types of Errors (cont)
• The error categories above are types of errors, not necessarily the place or the phase where an error is found.
– Eg misspell else and it looks like an and the parser will find the error.
– Misspell an and the code generator may find its not declared.
– Leave out the keyword else and you get a wrong “working” program which hopefully will give recognisably funny results.
• You won’t be able to find every error and you certainly won’t be able to fix them BUT we can try to do what is possible.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
5

Viable-Prefix Property
• The viable-prefix property of LL/LR parsers allows early detection of syntax errors
Prefix
… Error is for (;)detected here

Prefix
Error is
… detected here
DO 10 I = 1;0 …
– –
Goal: Detection of an error as soon as possible without consuming unnecessary input
How: Detect an error as soon as the prefix of the input does not match a prefix of any string in the language
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
6

Goals
Error Detection
• Detect as many errors as possible
• Recover from an error quickly enough to find subsequent errors
• Should not significantly slow down the processing of correct programs
• Most errors are simple  simple error handling
Error Reporting
• Report errors clearly and accurately
• As close as possible to where it occurred.
• LL and LR parsers can detect an error as soon as possible and the best reporting time is also as soon as possible
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
7

Error Recovery
• What should happen when your parser finds an error in the user’s input?
– Stop immediately
– Try to continue
Error recovery:
• Process of adjusting input stream so that the parser can continue after unexpected input
• Possible adjustments:
– delete tokens
– insert tokens
– substitute tokens
• Classes of recovery:
– local recovery: adjust input at the point where error was detected (and also possibly immediately after)
– global recovery: adjust input before point where error was detected.
• Error recovery is possible in both top-down and bottom-up parsers
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
8

Error Recovery Strategies
1. Panic Mode – The parser discards symbols until it finds an input symbol which it can use as a “synchronising token”. These tokens are generally special delimiters. In CD21 we could use tokens like if, else, ;, repeat, for, return, end,….
– Disadvantage – can skip a lot of input
– Advantage – simple, guaranteed not to loop indefinitely.
2. Phrase-Level Recovery – Perform a local correction on the input such as insert a semicolon, delete a semicolon, change a comma to a semicolon, etc. to repair the error
– Disadvantage – gets into trouble if the error occurred before the point of detection.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
9

Error Recovery Strategies
3. Error Productions – Find most common errors and add extra rules to the grammar that will generate the erroneous constructs. Build a parser for the augmented grammar.
Use Error Production => Generate Error Message.
– Disadvantage – very easy to make the grammar ambiguous.
4. Global Correction – Given an incorrect input string x, apply an algorithm which will produce the closest correct string y with a minimal sequence of changes to input string x.
– Disadvantage – not cost effective.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
10

Error Detection in LL(1) Parsing
How do we detect errors:
(1) If we are expanding R and we have a number of productions (R1) (R2) …
If the current input symbol is t and none of 1, 2, … begin with t, then error.
(2) Mismatch: If as we process a production Xi, we come to a point where the next input doesn’t match a terminal, then error.
• This is much easier to do if our parser is non-recursive.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
11

Error Recovery in LL(1) Parsing
• Simple option: When see an error, print a message and halt
• Useful error recovery
– Insert “expected” token and continue – can have a problem with termination
– Deleting tokens – for an error for non-terminal A, keep deleting tokens until see a token in synchronizing set
24/08/2021
• Eg: If we are expanding A but nothing can match the input symbol then
repeat
consume input symbol
until input symbol can legitimately follow A. return from A;
• FOLLOW(A) can serve as synchronizing set
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
12

Error Recovery in LL(1) Parsing
For example:
E  T E’
First (E) = {(, id} Follow(E) = { $, )}
E() {
} }
24/08/2021
if (lookahead in {(,id} ) { T(); E_prime();
} else {
printf(“E expecting ( or identifier”); while (lookahead != ) or $)
lookahead = nextToken();
Error reporting Error recovery
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
13

Error Recovery in LL(1) Parsing
• FOLLOW may not be enough.
if (X < Y) X := 1 + 2 if ( X < Z ) Z = X endif ; else Y:= 2 + 3; endif ; X := Y; • Missing ; after X:= 1 + 2 • PLs have hierarchical constructs: e.g. (i.e. block) 
• Add to the synch set of a lower-level constructs () the symbol that
begins the higher-level constructs (e.g. )
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
; | ;
 if () else endif
| if () endif
| := +
FOLLOW() = { $, else, endif} FOLLOW() = {;} FOLLOW() = {;, + }
FIRST ( ) = {if, ID}
14

Error Recovery in LL(1) Parsing
• FOLLOW may not be enough.
else
Y:= 2 * X (+ missing) 3;
endif ; X := Y;
• with s we can synch on ; or endif or else and can even synch on + or -,
• It can be done at different levels (blocks, statement, expression)
– In COMP3290 – sufficient to do at statement level
• It will always be easy to think up cases where a single error can give rise to multiple errors being reported.
• There is a limit. All you can do is your best.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
; | ;
 if () else endif
| if () endif
| := +
*
15

Other Techniques
• Error Productions
::= end if | end | endif
::= end loop | end | endloop
::= end proc | end | end | endproc
These non-terminals now just go in place of the terminals within the relevant productions.
Part of the processing of the “error” production will be to output an error message (possibly just a warning error).
::= ; | ε or
::= , | ε are interesting possibilities.
At all times with error productions, you must be careful that you are not making the grammar ambiguous.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
16

Error Detection in Table Driven Parsing
• An error is detected whenever an empty table slot is encountered.
• We would like our parser to be able to recover from an error and continue parsing.
• Panic mode recovery
– Modify the stack and/or the input string to try and reach state
from which we can continue.
• Phase-level recovery
– We associate each empty slot with an error handling procedure
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
17

Panic Mode Recovery in Table Driven Parsing
• Idea:
– Decide on a set of synchronizing tokens.
– When an error is found and there’s a nonterminal at the top of the stack, discard input tokens until a synchronizing token is found.
• Synchronizing tokens are chosen so that the parser can recover quickly after one is found
– e.g. a semicolon when parsing statements.
– If there is a terminal at the top of the stack, we could try popping it to see whether we can continue.
• Assume that the input string is actually missing that terminal.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
18

Panic Mode Recovery Table Driven Parsing
• Possible synchronizing tokens for a nonterminal A – the tokens in FOLLOW(A)
• When one is found, pop A of the stack and try to continue – the tokens in FIRST(A)
• When one is found, match it and try to continue
– tokens such as semicolons that terminate statements
– keywords that begins statements to the synchronizing sets for the nonterminals generating expressions.
– If a nonterminal can generate , then the production deriving  can be used as a default. This may postpone some error detection, but cannot cause an error to be missed.
– If a terminal on top of stack cannot be matched, a simple idea is to pop the terminal, issue a message saying that the terminal was inserted.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
19

Panic Mode Recovery
Add synchronizing actions to undefined entries based on FOLLOW
FOLLOW(E) = { $ ) } FOLLOW(ER) = { $ ) } FOLLOW(T) = { + $ ) } FOLLOW(TR) = { + $ ) } FOLLOW(F) = { * + $ ) }
id
+
*
(
)
$
E
E  T ER
E  T ER
synch
synch
ER
ER +TER
ER  
ER  
T
T  F TR
synch
T  F TR
synch
synch
TR
TR  
TR *FTR
TR  
TR  
F
F  id
synch
synch
F(E)
synch
synch
synch: pop A and skip input till synch token or skip until FIRST(A) found
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
20

Panic Mode Recovery
Parsing algorithm works as follows:
• If the parser looks up entry M[A,a] and
– If the entry is blank, the input symbol a is skipped. – If the entry is synch,
• the nonterminal A on top of the stack is popped and skip input till synch token OR
• skip until FIRST(A) found
– If a token on top of the stack does not match the input
symbol, then we pop the token from the stack.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
21

Panic Mode Recovery
FOLLOW(E) = { $ ) } FOLLOW(E) = { $ ) } FOLLOW(T) = { + $ ) } FOLLOW(T) = { + $ ) } FOLLOW(F) = { * + $ ) }
E T
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
FIRST(E) = {(, id} FIRST(E) = {+, } FIRST(T) = {(, id} FIRST(T) = {*, } FIRST(F) = {(, id}
22

Phrase-Level Recovery
Change input stream by inserting missing * For example: id id is changed into id * id
id
+
*
(
)
$
E
E  T ER
E  T ER
synch
synch
ER
ER +TER
ER  
ER  
T
T  F TR
synch
T  F TR
synch
synch
TR
insert *
TR  
TR *FTR
TR  
TR  
F
F  id
synch
synch
F(E)
synch
synch
insert *: insert missing * and redo the production Pro: Can be fully automated
Cons: Recovery not always intuitive
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
23

Error Productions
E  T ER
ER +TER | T  F TR
TR *FTR | F  ( E ) | id
Add error production:
T FT RR
to ignore missing *, e.g.: id id
id
+
*
(
)
$
E
E  T ER
E  T ER
synch
synch
ER
ER +TER
ER  
ER  
T
T  F TR
synch
T  F TR
synch
synch
TR
TR  F TR
TR  
TR *FTR
TR  
TR  
F
F  id
synch
synch
F(E)
synch
synch
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
24

Error Recovery in Table Driven Parsing
• Error Recovery is done by – consuming input
and/or
– popping elements off the stack
till we have synchronised input to stack.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
25

Recursive Descent Parsing – Recovery (1)
• In recursive descent, we have the stack stored in our call structure and each line of the parse table is hard coded into the routines for each non- terminal. The input also holds the next (and subsequent) tokens from the scanner.
• This means that a language like Java or C++ can mimic the panic mode recovery that is used in table-driven parsing
• (A) –

Consume Input until we can continue
We can consume input to synchronise best for that error
If we find a token that is significant enough to allow the parsing to continue (as if the error has not occurred), then we simply return from the error reporting routine
• Return leaving the significant token in place (e.g. keyword), or
• Consume the significant token (e.g. semicolon) and return ready to process what follows that special token.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
26

Recursive Descent Parsing – Recovery (2)
• (B)
– We can consume input to synchronise best for that error
– An exception can be thrown when an error is found
– The place where the exception is caught determines which recursive descent routines have been “returned from” (ie what elements have been popped off the equivalent table-driven stack)
– Within the routine where the exception has been caught we can decide to either return or to try that routine again.
– Once again the token that lead to the exception being thrown for that error can be left in place or consumed.
• (C)
Some errors may indicate that recovery is impossible
Pop symbols from the “stack” until we can continue
– We can keep a different exception that is not caught until outside the parser, thereby ending compilation (with an error message to that effect).
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
27

Recursive Descent Parsing – Recovery (3)
• We finish up with 2 basic error recovery strategies
– Return to routine that issued the error, or
– Throw Exception and catch it at the recovery point
• Within each of these once we have a synchronising token, we can decide to
– leave that token in place for recovery, or
– to consume that token and recover according to what FOLLOWs
that token.
• Making 5 strategies in all (find a synch token first of all):
1) Return to routine with error, leaving the synch token in place 2) Consume the synch token and return to routine with error
3) Throw an exception leaving synch token in place and recover
where the exception is caught
4) Consume the synch token, throw an exception and recover where
the exception is caught
5) Throw a fatal exception and stop trying to compile.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
28

Error Reporting
Source Code
Lexemes (tokens)
Syntax Object Tree Code
Scanner
Parser
Semantic Analysis & Code Gen.
• Question:
• More general question:
• How do we create a program listing?
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
How do we report errors?
Symbol Table
29

Architecture: Relationship between the Scanner, Parser, Listing & Error Reporting
Source Code
Listing
Classified Lexemes
(Tokens)
Error Messages
Semantic Error Messages – – later
Scanner
Output Controller
Symbol Table
• Both the Scanner and the Parser (and later the Semantic Analyser) will need to produce a program listing and report errors.
• Only the Scanner can produce the listing (otherwise comments are lost)
• BUT All 3 (Scanner, Parser, and Semantic Analysis) must be able to produce error messages in a coherent and cooperative way.
• A separate Output Controller object can coordinate the output (the feedback) to
the CD21 programmer. 24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
Parser
GetToken
Parser I/P Routine
30

Error Reporting (cont)
• Listing – needs to be produced as program is read.
– Basically we need to read a char then write it out
– It is quite easy to incorporate a line count for error reporting and
listing production.
• Reporting Errors
– can report asap but this will be messy
eg. if error missing left paren x == y)
– can save a list of errors with line no’s for end of program
– can flush error list at end of each line – be careful not to have too
many errors on any one line (can keep a char count within the line to report the position of an error).
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
31

Symbol Table
ROLE OF SYMBOL TABLE
• Essential data structure for compiler
• Used for storing information about identifiers appearing in a source program
• Lexical Analyser and Parser fill up symbol table
• Code generator and optimizer make use of symbol table
• Entities stored in a symbol table
– Variables,procedures,functions,definedconstants,labels, structures, file identifications, compiler generated temporaries
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
32

Symbol Table
import f (float, float, float)
import g (int)
{
Symbol Number
Symbol Name
Attribute
1
f
void func (float, float, float)
2
g
void func (int)
3
w
int
4
x
int
5
x
float
6
z
float
int w, x {
float x, z
f(x,w,z) }
g(x)
Block
Code
}
Block
Code Block
Code Call Args
Ref w
Dcl w
Dcl x
z
f
Ref x
Call
g Args Ref x
Ref z
Dcl 3
Dcl 4
6
Ref 1
Ref 5
Block
Code Call Args
Ref 3
Call Ref 2

Dcl x
Dcl 5
Args Ref 4
Ref 6
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
33

Information in Symbol Table
• Name: May be stored directly in the table or the entry may point
to another character string, possibly in an associated string table
• Type: type of identifier
– Whether variable / function / procedure name
– For variables, identify whether integer / real / array …
• Location: offset in the program where the identifier is defined
• Scope: identifies the region of the program in which the current symbol definition is valid
• Other attributes: Array limits, record fields / parameters / return values of functions
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
34

Use of Symbol Table Information
• Semantic Analysis: check correct semantic usage of language
constructs
– May need checking types of identifiers
• Code generation: All program variables and temporaries need to be allocated some memory locations
– Symbol table provides information regarding memory size required for identifiers by their types
• Error Detection: Leave variables undefined
• Optimization: To reduce the total number of variables used in a program we need to reuse the temporaries generated by the compiler
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
35

Symbol Table Management
name
attributes
:
:
• Collect attributes when a name is declared.
• Provide information when a name is used.
• Two issues:
– interface: how to use symbol tables – implementation: how to implement it.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
36

Symbol Table Interface
• A symbol table class provides the following methods: – create() : symbol_table [ Enter ()]
– destroy (symbol_table) [ Exit ()]
– enter (name, table) : pointer to an entry
– find (name, table) : pointer to an entry – set_attributes(*entry,attributes)
– get_attributes (*entry) : attributes
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
37

Symbol Table Format
• Basic operations: enter() and find() • Considerations:
– number of names
– storage space
– retrieval time
Organizations:
1. UnorderedList
» Can be represented using linked list or array
» Insert, Lookup and modify operations take O(n) time, n being the number of identifiers
» Insertion can be made in O(1) be remembering the pointer to the next free position
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
38

Symbol Table Format
2. OrderedList
» » –
binary search on arrays
expensive insertion
(+) good for a fixed set of names
• (e.g.reservedwords)
3. Binary Search Tree
» » »
Each entry is used represented as a node in a tree On average, searching takes O(log(n)) time.
However, names in programs are not chosen randomly.
» Average case performance does not hold of symbol table » In the worst case search takes O(n) time.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
39

Symbol Table Format
4. Hash Table:
» Hash function used to map identifier names to hash table locations, organized as an array
» Most common method implementing Symbol Table in compilers
» (+) constant time
• Hash Table VS Height-Balanced Binary Tree
– Hash table needs a fixed size regardless of the number of entries.
– Sometimes, we may use multiple symbol tables. Height balanced Binary Tree is better than hashing in this case in terms of space utilization
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
40

String Space/ Name Space
• Should we store the name of the symbol in the symbol table?
24/08/2021
• •
• •
Names do not change during compilation Name lengths differ significantly.
• e.g. i, account_receivable, a_very_very_long_name
Space is wasted if space of max size is reserved.
Solution : store the identifiers in string space, and store an index and length in the symbol table
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
41

String Space/ Name Space
…..
1
7
…..
13
7
…..
8
5
1 8 13 20
……
string space
a
c
c
o
u
n
t
t
e
m
p
l
b
a
l
a
n
c
e
• •
Usually, symbol table manages the string space. Names in string space may be re-used.
Symbol Table
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
42

Scoped Symbol Table
• Scope of a symbol table defines the region of the program in which a particular definition of the symbol is valid / visible.
• Block structured PLs permits different types of scopes
• Global Scope
– Global Variables
• File-wide scope
– Modules in more than one file (static variables / functions)
• Local scope within a procedure – Function local variables
• Local scope within a block
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
43

Scope Rules
• Static or lexical scoping
– Depends purely on the organization of the program
– Scope is defined by the syntactic nesting
– If a name is defined in more than one scope, the innermost definition closest to the reference is to be used to interpret the reference to that name
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
44

Scope Rules
• Static or lexical scoping
– When a scope is exited, all variables declared in that scope becomes inaccessible
begin
………
end
begin
int x;
real y; type z;
:
: end
/* x,y,z are inaccessible here */
:
:
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
45

Scope Rules
• Dynamic or runtime scoping
– As the program runs, the same use of x could refer to any of several different declarations of x.
• Example:
– Procedure P1 is callable from P2 and P3
– P1 has reference to a non-local variable x
– There exist two different definitions of x, one each in P2 (say real) and P3 (say integer)
– When P1 is called from P2, x will refer to the definition in P2 (i.e. real), while when called from P3, it will refer to the definition in P3 (i.e. integer)
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
46

Scope In Symbol Table
Two Implementations:
• One table per scope
• One table for all scopes
One table per scope:
• Many symbol tables (one per scope)
• Use a stack of tables.
• The symbol table for the current scope is on stack top.
• The symbol tables for other enclosing scopes are placed under the current one.
• Push a new table when a new scope is entered.
• Pop a symbol table when a scope is closed.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
47

One Table Per Scope
Example:
begin
int H,A,L;
begin
real x,y;
:
: end
begin
char A,C,M;
print(M); H + A ….. ; X + L …… ;
end end
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
stack
symbol table
A:char C:char M:char :
H:int A:int L:int
48

One Table Per Scope
• To search for a name, we check the symbol tables on the stack from top to bottom.
– (- ) We may need to search multiple tables.
– E.g. A global name is defined in the bottom-most symbol table.
– (- ) Space may be wasted if a fixed-sized hash table is used to implement symbol tables.
• hashtabletoobig–waste
• hashtabletoosmall–collisions
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
49

One Symbol Table
• All names are in the same table.
• What about the same name is declared several times?
• Each name is given a scope number.
should be unique in the table.
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
50

One Symbol Table
begin
int H,A,L;
begin
real x,y;
:
: end
begin
char A,C,M;
print(M); H + A ….; X + L …. ;
Ex.
scope number
1
2
3
symbol table
A(3)
L(1)
A(1)
C(3)
H(1)
M(3)
24/08/2021
end end
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
51

One Symbol Table
• • •
To search a name is easy.
New names are placed at the front of lists.
To close a scope, we need to remove all entries defined in that scope. (We need to examine each list.)
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
52

One Symbol Table
• One global table cannot be implemented with binary trees easily.
– It is not easy to delete all entries declared in a scope when the scope is closed.
– To find a name, we need to search the last entry (rather than the first entry).
Ex.
H(1)
A(1)
L(1)
A(3)
C(3)
Consider the case when A is being searched for.
M(3)
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
53

References
• Compilers: Principles, Techniques, and Tools (2nd Edition)
– By A.V. Aho, Lam, R. Sethi, . Ullman
• Chapter 4 and 2
24/08/2021
COMP3290 – Semester 2 – 2021 | www.newcastle.edu.au
54