Skip to main content

Posts

Showing posts with the label Complier Design Lab

Write program to find ε – closure of all states of any given NFA with ε transition.

 Write program to find ε – closure of all states of any given NFA with ε transition. Agenda 1.Program 2.Input/Output 1.Program #include <stdio.h> #include <string.h> char  result[ 20 ][ 20 ], copy[ 3 ], states[ 20 ][ 20 ]; void  add_state( char  a[ 3 ],  int  i) {   strcpy(result[i], a); } void  display( int  n) {    int  k =  0 ;   printf( "nnn Epsilon closure of %s = { " , copy);    while  (k < n) {     printf( " %s" , result[k]);     k++;   }   printf( " } nnn" ); } int  main() {   FILE * INPUT;   INPUT = fopen( "input.dat" ,  "r" );    char  state[ 3 ];    int  end, i =  0 , n, k =  0 ;  ...

Program to Construct a recursive descent parser for an expression.

Program to Construct a recursive descent parser for an expression. Agenda 1.Algorithm 2.Code(program) 3.Input/output 1.Algorithm Read the input string. Write procedures  for  the non terminals Verify the next token equals to non terminals  if  it satisfies match the non terminal. If the input string does not match print error. 2.Code #include <stdio.h> #include <conio.h> #include <string.h> char  input[ 100 ]; int  i, l; void  main() {   clrscr(); printf( "\nRecursive descent parsing for the following grammar\n" );   printf( "\nEnter the string to be checked:" );   gets(input); if  (E())   {      if  (input[i +  1 ] ==  '\0' )      ...

Write a program to perform constant propagation

  Write a program to perform constant propagation Agenda- 1.Algorithm 2.Program for  Write a program to perform constant propagation 3.Input 4.Output 1.Algorithms 1. For all 4-tuples do     1.1 get next 4-tuple    1.2 If the operator is "Label" then          1.2.1 free the constant table         1.2.2 write the 4-tuple  1.3 If either or both of the operands are in the constant table then        1.3.1 substitute the constant value(s) for the operand(s)         1.3.2 if either of the operands are intermediate results then                  1.3.2.1 remove those operand(s) from the constant table.  1.4 if 4-tuple operand(s) are constant then ...

Implement Intermediate code generation for simple expressions

 C program To Implement Intermediate code generation for simple expressions Input - + a b t1 * c d t2 - t1 t2 t = t ? x Program- #include <stdio.h> #include <conio.h> #include <string.h> char   op [ 2 ], arg1 [ 5 ], arg2 [ 5 ], result [ 5 ]; void   main () {    FILE  * fp1 ,* fp2 ;    fp1 = fopen ( "input.txt" , "r" );    fp2 = fopen ( "output.txt" , "w" );    while (! feof ( fp1 ))   {      fscanf ( fp1 , "%s%s%s%s" , op , arg1 , arg2 , result );      if ( strcmp ( op , "+" )== 0 )     {        fprintf ( fp2 , " \n MOV R0,%s" , arg1 );        fprintf ( fp2 , " \n ADD R0,%s" , arg2 );        fprintf ( fp2 , " \n MOV %s,R0" , result );     }    ...

. Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using an 8086 assembler. The target assembly instructions can be simple move, add, sub, jump etc.

Implement the back end of the compiler which takes the three address code and produces the 8086 assembly language instructions that can be assembled and run using an 8086 assembler. The target assembly instructions can be simple move, add, sub, jump etc. Algorithms- 1. Start the program. 2. Get the three variables from statements and stored in the text file k.txt. 3. Compile the program and give the path of the source file. 4. Execute the program. 5. Target code for the given statement was produced. 6. Stop the program. Code- # include  < stdio . h  > # include  < stdio . h  > # include < conio . h > # include  < string . h  > void   main () {    char   icode [ 10 ][ 30 ],  str [ 20 ],  opr [ 10 ];    int   i  =  0 ;    clrscr ();    printf ( " \n  Enter the set of intermediate code (terminated by exit):...