Compiler Design Assignments
Compiler Design Assignments
4. Write a C program to check if another C file has the necessary prerequisites for
executing a C program(such as #include<stdio.h>, int main(){},return 0; )
Output:
#include<stdio.h>
int main()
{int a=10;return 0;}
Valid program
#include<stdio.h>
int main()
{int a=10;}
No return statement
10. Write a C program that will take another C file and check if all the functions defined in it
are called or not. If not, it just delete the function.
Output:
Input a program:
Void fun1(){printf(“hi..”);}
Void fun2(){printf(“this function is not called”);
Void main()
{fun1();
fun2();}
Void fun1(){printf(“hi..”);}
Void main()
{fun1();}
11. Write a C program that should check if all the members of the structures are having
a defined data type. If not, print an error.
Output:
Input a structure definition:
Struct demo{
Int a;
Float b;
};
The structure demo has defined data types
12. Write a program to ignore the comments in the given input source program(i.e., delete
them).
Output:
Input a program:
Void main()
{int a;
//this is a comment line
float b;
}
Void main()
{int a;
float b;
}
13. Write a C program that will check whether the input string is containing “Monday” in it.
14. Write a C program that will take a C file as an input and output a file which will have \n
and \b replaced by corresponding spaces.
Output:
Input a file:
#include<stdio.h>
Void main()
{printf(“hi students\t this is compiler design”);
printf(“uem\nkolkata”);}
#include<stdio.h>
Void main()
{printf(“hi students this is compiler design”);
printf(“uem
kolkata”);}
15. Write a C program that will check whether all the variables declared in an input file
are initialized or not. If not, initialize them with 0.
Output:
void main()
{int a,b=10,c;} After
initialization:
void main()
{int a=0,b=10,c=0;}
Input a program:
#include<stdio.h>
Void main()
{int a, b=10,c=20;
50=a;}
Valid operators: {=,=}
20. Write a C program to replace all the digits in a file to their corresponding words. Use a
switch case.
21. Write a program in C that will take two files as input and merge them into one and delete
any redundant words from the resulting file.
aAb->aab
It is not context free
23. Write a C program to count the number of white spaces between two consecutive
tokens in a program and replace it with a single whitespace.
Output:
Enter a program:
#include<stdio.h>
Void main()
{int a= 20;float b=30.0;}
The modified program is:
#include<stdio.h>
Void main()
{int a=20;float b=30.0;}
24. Write a program in C to find the First and Follow for a given set of productions
Output:
Enter the no. of Non-terminals in the grammer:3
Enter the Non-terminals in the grammer: E T V
Enter the no. of Terminals in the grammer: 5 Enter
the Terminals in the grammer: + * ( ) i
Enter the production for E ( End the production with '$' sign ) :(i)$
Enter the production for T ( End the production with '$' sign ) :i*E$ Enter
the production for V ( End the production with '$' sign ) :E+i$ The
production for E -> (i)
The production for T -> i*E
The production for V -> E+i
The first of E -> (
The first of T -> i
The first of V -> (
Enter a program:
If(condition)
Printf(“hi..”);
Else
Printf(“hello”);
No dangling else found
36. Write a C program which will copy each line of a given program and number
each newline.
Output:
#include<stdio.h>
Void main()
{
Printf(“hi”);
}
Line 1: #include<stdio.h>
Line 2: Void main()
Line 3:{
Line 4: Printf(“hi”);
Line 5: }
37. Write a program in C to print an error when a user doesn’t provide a semi-colon at the
end of a line of a program. User input should be a file containing a program.(N.B.- For
loop doesn’t have a semicolon at the end normally)
41. Write a C program that will count the number of lowercase and uppercase characters
from a file.