0% found this document useful (0 votes)
69 views

Stage 2: Implementation Issues

The document discusses implementation issues related to stage 2 of compiler construction. It covers symbol table implementation with details on type, scope, and annotated parse trees. It also provides examples of type checking for expressions, variables, matrices, and context-free grammar rules with semantic checking.

Uploaded by

Annie Rawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Stage 2: Implementation Issues

The document discusses implementation issues related to stage 2 of compiler construction. It covers symbol table implementation with details on type, scope, and annotated parse trees. It also provides examples of type checking for expressions, variables, matrices, and context-free grammar rules with semantic checking.

Uploaded by

Annie Rawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Stage 2

Implementation Issues
Symbol Table (ST)
 Each variable identifier find space in the ST
 Major information collected during front end
compilation
 Type
 Width
 Offset
 Scope (visibility) of variables is implemented as
hierarchical symbol tables
 As each lexeme is checked once, the ST data
structure is expected to be of O(1) time
complexity
CS F363/IS F342 Implementation
March 25, 2014 2
issues
Scope
• The scope is defined as the nested function.
• A function scope can be implemented as a
separate hash table.
• In order to support semantic checks such as
function overloading, the names of the
function identifiers are used as hashing keys
while accessing the symbol table
corresponding to the function scope.

CS F363/IS F342 Implementation


March 25, 2014 3
issues
Type
• Type information is collected by traversing the
declaration construct.
• The type field of the symbol table entry
corresponding to a construct can be
– A simple primitive data type – real, integer
– A tuple <matrix, m,n>
– A type string

CS F363/IS F342 Implementation


March 25, 2014 4
issues
Annotated Parse tree for
INT id,id
D

T.type=integer
T L
L.in=integer

int L , id
L.in=integer

addtype(id.entry,L.in)

id

Symbol table is updated for both tokens ‘id’ with type of these as integers

CS F363/IS F342 Implementation


March 25, 2014 5
issues
Type checking

exp
exp.t=BOOL

exp == exp
exp.t=INT exp.t=INT

( exp )
exp.t=INT 4

exp + exp
exp.t=INT exp.t=INT annotated Parse Tree
Parse Tree

2 2

CS F363/IS F342 Implementation


March 25, 2014 6
issues
Context free grammar rules with SDD

if ((exp2.t == INT) and (exp3.t == INT)


1. exp -> exp + exp then exp1.t = INT
else exp1.t = ERROR

2. exp -> exp and exp if ((exp2.t == BOOL) and (exp3.t ==BOOL)
then exp1.t = BOOL
else exp1.t = ERROR

3. exp -> true exp.t = BOOL

CS F363/IS F342 Implementation


March 25, 2014 7
issues
Context free grammar rules with SDD

4. exp -> false exp.t = BOOL

5. exp -> int exp.t = INT

6. exp -> ( exp ) exp1.t = exp2.t

7. exp -> exp == exp if ((exp2.t == exp3.t) and (exp2.t != ERROR))


then exp1.t = BOOL
else exp1.t = ERROR
CS F363/IS F342 Implementation
March 25, 2014 8
issues
Type Checking

_main[]
int a;
a integer
real b; b real
a = 5;
a = a + b;
print(a);
end;
Symbol table

CS F363/IS F342 Implementation


March 25, 2014 9
issues
_main[]
Type Checking int a;
real b,c;
a = 5;
a = a + b*c; a integer
b real
print(a); c real
end;
MAIN

Symbol table
D D
SA SA SI

int a real b,c a=5 a


Print(a)
=a+b*c
CS F363/IS F342 Implementation
March 25, 2014 10
issues
Type rules
The type of a simple expression (say E) of the
form expression(say E1) <operator>
Expression(say E2) is
 integer, if both expressions are of type integer
and the operator is PLUS, MINUS or MUL.
 is real, if both the expressions are of type real
and the operator is PLUS, MINUS or MUL.
 is real, if both expressions are of type integer or
real and the operator is DIV.

CS F363/IS F342 Implementation


March 25, 2014 11
issues
Type expression
a integer
b real
computation
c real Construct
ERROR
Expression,
PLUS

Construct
Type: Construct
Type:
integer
VAR1 Expression,
real
MUL

Construct
Type:
Construct
Type:
VAR1
real
VAR1
real
Type:
ID
integer
Type: Type:
ID
CS F363/IS F342 Implementation real ID
March 25, 2014
issues
real 12
Matrix
_main[]
matrix mat1, mat2;
mat1 = [1,2, 3; 4, 5, 6; 6, 7, 8];
mat2 = [-6, -1, 10; 6, -10, 0];
m = mat1 + mat2;
print(a);
end;

CS F363/IS F342 Implementation


March 25, 2014 13
issues
Grammar

• <var>===> ID | NUM | RNUM | <matrixElement> | STR | <matrix>


• <matrix>===>SQO <rows> SQC
• <rows> ===> <row> SEMICOLON <rows> | <row>
• <row> ===> NUM <remainingColElements> | NUM
• <remainingColElements> ===> COMMA NUM <remainingColElements>
• <remainingColElements> ===> 

CS F363/IS F342 Implementation


March 25, 2014 14
issues
mat1 = [1,2, 3; 4, 5, 6; 6, 7, 8];
ASSIGNOP Matrix
<matrix> COL =3
type = 3
ROW
COL = 3
ID SQOROW
<rows>
= 3 SQC Matrix
COL = 3 type
Matrix<row> SEMICOLON <rows>
COL = 3 ROW = 2
type
ROW = 1
Matrix Matrix
type
COL =3 COL = 3
<row> SEMICOLON <rows> type
ROW = 1 ROW = 1

<row>

NUM <remainingColElements>
COUNT
mat1 matrix
SIZE:
bottom COMMA NUM <remainingColElements>
up
COMMA NUM <remainingColElements>
CS F363/IS F342 Implementation
March 25, 2014 15
issues 
mat1 = [1,2, 3; 4, 5, 6; 6, 7, 8];
ASSIGNOP
<matrix> COL = 3
ROW = 3

ID SQO <rows> SQC

<row> SEMICOLON <rows>

<row> SEMICOLON <rows>

<row>

NUM <remainingColElements>
COUNT
mat1 matrix
SIZE:
<matrix, 3,3> bottom COMMA NUM <remainingColElements>
up
COMMA NUM <remainingColElements>
CS F363/IS F342 Implementation
March 25, 2014 16
issues 
Matrix
_main[]
matrix mat1, mat2;
mat1 = [1,2, 3; 4, 5, 6; 6, 7, 8];
mat2 = [-6, -1, 10; 6, -10, 0];
m = mat1 + mat2;
print(a);
end;

CS F363/IS F342 Implementation


March 25, 2014 17
issues
m = mat1+mat2;
• The lexeme m when seen while traversing the
tree, is looked up in the symbol table.
• If the entry exists then the type is seen.
• If the type of mat1 and type of mat 2 are same
then the type of the RHS expression is same as
one of them.
• The type of RHS expression is matched with
that of the LHS variable (information obtained
from the ST)
CS F363/IS F342 Implementation
March 25, 2014 18
issues
Other Semantic Rules
 Variable is declared before its usage
 Variable and its type exist in the Symbol table
 Variable is declared multiple times
 Variable and its type exist in the Symbol table. The
declaration construct must notify the double existence
through the tag or by t he fact the type field is about to be
rewritten.

CS F363/IS F342 Implementation


March 25, 2014 19
issues
Visibility of a variable
• { int x
{
int y;
y = x+4;
}
}

CS F363/IS F342 Implementation


March 25, 2014 20
issues
Count the number of parameters and
establish type checking
• The types and the number of input and output
parameters of a function must be the same as that of
the parameters used in invoking the function.
_main[]
string str1, str2;
matrix m1;
str1 = "eradicate"; Maintain information
blankSTR = " ";
str2 = "plagiarism"; corresponding to the
function [string s]= _concatenate[string a, string a2]scope
function
str1 = a + a2;
end;
string ccSubmission;
ccSubmission = _concatenate(str1, blankSTR);
ccSubmission = _concatenate(ccSubmission, str2);
end; Function Function
Definition Call
FUNID
FUNID

CS F363/IS F342 Implementation


March 25, 2014 21
issues
Using Definition populate ST Use ST to verify
Semantic Rules
– The types and the number of input and output
parameters of a function must be the same as
that of the parameters used in invoking the
function.
– The output parameters of a function must be
assigned a value within the function definition. If
a parameter does not get a value assigned within
the function definition, it should be reported as
an error.
– The function that does not return any value,
must be invoked appropriately.
CS F363/IS F342 Implementation
March 25, 2014 22
issues
Semantic Rules
– The conditional expression in the if statement
must be of boolean type.
– Function overloading in the same static scope is
not allowed.
– The function cannot be invoked recursively.
– Two matrices cannot be multiplied or divided.
– Two strings cannot be subtracted, multiplied or
divided.
– Matrix addition or subtraction can be applied to
matrices of same sizes.
CS F363/IS F342 Implementation
March 25, 2014 23
issues
Semantic Rules
– Size of the string or matrix should be computed
at compile time and appropriately assigned to
one variable or two variables respectively.
– The right hand side expression of an assignment
statement must be of the same type as that of
the left hand side identifier.
– A function definition for a function being used
(say F1) by another (say F2) must precede the
definition of the function using it(i.e. F2) and
should be visible in the scope of the calling
function.
CS F363/IS F342 Implementation
March 25, 2014 24
issues

You might also like