CD Unit 4 Complete Notes Topic Wise
CD Unit 4 Complete Notes Topic Wise
Symbol table is used to store the information about the occurrence of various entities
such as objects, classes, variable name, interface, function name etc. it is used by both
the analysis and synthesis phases.
o It is used to store the name of all entities in a structured form at one place.
o It is used to verify if a variable has been declared.
o It is used to determine the scope of a name.
o It is used to implement type checking by verifying assignments and expressions
in the source code are semantically correct.
A symbol table can either be linear or a hash table. Using the following format, it maintains
the entry for each name.
For example, suppose a variable store the information about the following variable
declaration:
Implementation
The symbol table can be implemented in the unordered list if the compiler is used to
handle the small amount of data.
Operations
The symbol table provides the following operations:
Insert ( )
o Insert () operation is more frequently used in the analysis phase when the tokens
are identified and names are stored in the table.
o The insert() operation is used to insert the information in the symbol table like
the unique name occurring in the source code.
o In the source code, the attribute for a symbol is the information associated with
that symbol. The information contains the state, value, type and scope about
the symbol.
o The insert () function takes the symbol and its value in the form of argument.
For example:
int x;
lookup ( )
In the symbol table, lookup() operation is used to search a name. It is used to
determine:
The scope of a name and symbol table is arranged in the hierarchy structure as shown
below:
int value=10;
void sum_num()
{
int num_1;
int num_2;
{
int num_3;
int num_4;
}
int num_5;
{
int_num 6;
int_num 7;
}
}
Void sum_id
{
int id_1;
int id_2;
{
int id_3;
int id_4;
}
int num_5;
}
The above grammar can be represented in a hierarchical data structure of symbol
tables:
The global symbol table contains one global variable and two procedure names. The
name mentioned in the sum_num table is not available for sum_id and its child tables.
Data structure hierarchy of symbol table is stored in the semantic analyzer. If you want to
search the name in the symbol table then you can search it using the following algorithm:
o These scope rules need a more complicated organization of symbol table than a list of
associations between names and attributes.
o Tables are organized into stack and each table contains the list of names and their
associated attributes.
o Whenever a new block is entered then a new table is entered onto the stack. The new
table holds the name that is declared as local to this block.
o When the declaration is compiled then the table is searched for a name.
o If the name is not found in the table then the new name is inserted.
o When the name's reference is translated then each table is searched, starting from the
each table on the stack.
For example:
int x;
void f(int m) {
float x, y;
int i, j;
int u, v;
}
}
int g (int n)
{
bool t;
}
Fig: Symbol table organization that complies with static scope information rules
Storage Organization
o When the target program executes then it runs in its own logical address space in which
the value of each program has a location.
o The logical address space is shared among the compiler, operating system and target
machine for management and organization. The operating system is used to map the
logical address into physical address which is usually spread throughout the memory.
Activation Record
o Control stack is a run time stack which is used to keep track of the live procedure
activations i.e. it is used to find out the procedures whose execution have not been
completed.
o When it is called (activation begins) then the procedure name will push on to the stack
and when it returns (activation ends) then it will popped.
o Activation record is used to manage the information needed by a single execution of
a procedure.
o An activation record is pushed into the stack when a procedure is called and it is
popped when the control returns to the caller function.
Access Link: It is used to refer to non-local data held in other activation records.
Saved Machine Status: It holds the information about status of machine before the
procedure is called.
Local Data: It holds the data that is local to the execution of the procedure.
Storage Allocation
The different ways to allocate memory are:
Example:
fact (int n)
{
if (n<=1)
return 1;
else
return (n * fact(n-1));
}
fact (6)
Lexical Error
During the lexical analysis phase this type of error can be detected.
Lexical error is a sequence of characters that does not match the pattern of any token.
Lexical phase error is found during the execution of the program.
o Spelling error.
o Exceeding length of identifier or numeric constants.
o Appearance of illegal characters.
o To remove the character that should be present.
o To replace a character with an incorrect character.
o Transposition of two characters.
Example:
Void main()
{
int x=10, y=20;
char * a;
a= &x;
x= 1xab;
}
In this code, 1xab is neither a number nor an identifier. So this code will show the
lexical error.
Syntax Error
During the syntax analysis phase, this type of error appears. Syntax error is found
during the execution of the program.
o Error in structure
o Missing operators
o Unbalanced parenthesis
When an invalid calculation enters into a calculator then a syntax error can also occurs.
This can be caused by entering several decimal points in one number or by opening
brackets without closing them.
16 if (number=200)
17 count << "number is equal to 20";
18 else
19 count << "number is not equal to 200"
In this code, if expression used the equal sign which is actually an assignment operator
not the relational operator which tests for equality.
Due to the assignment operator, number is set to 200 and the expression number=200
are always true because the expression's value is actually 200. For this example the
correct code would be:
16 if (number==200)
Compiler message:
Semantic Error
During the semantic analysis phase, this type of error appears. These types of error are
detected at compile time.
Most of the compile time errors are scope and declaration error. For
example: undeclared or multiple declared identifiers. Type mismatched is another
compile time error.
The semantic error can arises using the wrong variable or using wrong operator or
doing operation in wrong order.
int i;
void f (int m)
{
m=t;
}
int a = "hello"; // the types String and int are not compatible
String s = "...";
int a = 5 - s; // the - operator does not support arguments of type String