Starting from:
$35

$29

Lab #9 Solution

Objective: To augment the mini compiler design with the translation to intermediate code and type checking capabilities

In previous lab, you have designed and implemented a mini compiler consisting lexer, parser, and semantic analyzer, to deal with variable declarations in a block structured language. You need to augment the same with translation capabilities. The task is restricted to

    • translation of arithmetic expressions declared in a block to the equivalent three address code,

    • generating error if a variable is not declared, and

    • preforming typechecking.

Input : Blocks of C variable declarations and statements containing arithmetic expressions

Output: three address code and errors if any

Execusion: $./minicc prog.c

1. Testcase:

Input:

{

i n t  x1 ,    x2 ,    y1 ,    y2 ,    d i s t ;

f l o a t  m1 ,  m2 ,  m3 ,    t o t a l ,    x ,    y ;
d i s t  =  ( x1 − x2 )    ∗    ( x1 − x2 )  +  ( y1 − y2 )    ∗    ( y1 − y2 ) ;
t o t a l  = m1  ∗ m2  ∗ m3 ;

x = y +  5 ;

}

Output:

        ◦ 0=x1−x2

        ◦ 1=x1−x2
        ◦ 2=t 0 ∗ t 1

        ◦ 3=y1−y2

        ◦ 4=y1−y2
        ◦ 5=t 3 ∗ t 4

        ◦ 6=t 2+t 5 d i s t=t 6

        ◦ 7=m1∗m2

        ◦ 8=t 7 ∗m3

        ◦ o t a l=t 8

        ◦ 9=y+ ( f l o a t ) 5 x=t 9

    2. Testcase:

Input:

{

i n t  a ;

{

i n t  b ,    c ;
a = b + c ;

x = a + b ;

}

}

Output:

t 0 = b + c

a =  t 0

e r r o r :    v a r    ’ a ’    i s    no t    d e c l a r e d    i n    t h e    s c o p e

More products