Starting from:
$35

$29

Lab #8 Solution

Objective: To design and implement a mini compiler consisting lexer, parser, and semantic analyzer, to deal with variable declarations in a block structured language.

As learned in class, semantic analysis/translation can be peformed while parsing itself by implementing the rules/actions with the parser. The task for this lab is the following. Consider type declarations in a block structured language like C, in which a program may be written in terms blocks where each block may have declaration section followed by the statements section. Consider variable declarations consisting of premitive data types int, float , char, composite data type, array (including multidimensional arrays), and pointers. Assume sizes of char, float, and int data types are 1, 4, and 4 bytes respectively. Also consider the size of a pointer variable as 4 bytes.

Design a grammar to generate the all possible type declarations consisting of data types mentioned above, and implement the necessary functionality

    • to add the type information for each identifier defined in the program,

    • to find the size requirements for each declaration section,

    • to maintain the relative memory address (i.e., offset) for each variable declared. Assume 16-bit memory addresses.

Note that in next lab session, you require use these type declarations for typechecking and intermediate code generation.

Input : Blocks of C variable declarations

Output: Display symbol table entries

Execusion: $./minicc prog.c

1. Testcase:

Input:

{

i n t  a ,    b ,    c ;

char
e ;

f l o a t  p i  =  3 . 2 4 ;
}


Output:


0 x0 0 0 0
a
i n t
0 x0 0 0 4
b
i n t
0 x0 0 0 8
c
i n t
0 x0 0 0 9
e
char
0 x000A
p i
f l o a t  3 . 2 4

2. Testcase:

Input:

{





i n t  a ;



i n t  x ,
y ;


f l o a t
c ;


{




i n t  a ;



i n t  b ;


}



{




char m;



i n t  n [ 1 0 ] ;


}

}



Output:



0 x0 0 0 0
a
i n t

0 x0 0 0 4
x
i n t

0 x0 0 0 8
y
i n t

0 x000C
c  f l o a t

0 x0 0 0 0
a
i n t

0 x0 0 0 4
b
i n t

0 x0 0 0 0
m char

0 x0 0 0 1
n  i n t a r r a y  40

3. Testcase

Input:

{

i n t  a ;

i n t  b ;

i n t  a ;

}

Output:

e r r o r :    r e d e c l a r a t i o n    o f    ’ a ’

4. Testcase

Input:

{

i n t  a ;

char  a ;

{

i n t    c ;

i n t    c ;

}

}

Output:

e r r o r :    c o n f l i c t i n g    t y p e s    f o r    ’ a ’

e r r o r :    r e d e c l a r a t i o n    o f    ’ c ’

More products