TCS CodeVita previous years questions solved – Compiler Design


TCS CodeVita2014 Round 1 question

TCS CodeVita previous years questions

Problem : Compiler Design – Limit the Method Instructions




Raj is a newbie to the programming and while learning the programming language he came to know the following rules:
–         Each program must start with ‘{‘ and end with ‘}’.
–         Each program must contain only one main function. Main function must start with ‘<‘ and end with ‘>’.
–         A program may or may not contain user defined function(s). There is no limitation on the number of user defined functions in the program. User defined function must start with ‘(‘ and end with ‘)’.
–         Loops are allowed only inside the functions (this function can be either main function or user defined function(s)). Every loop must start with ‘{‘ and end with ‘}’.
–         User defined function(s) are not allowed to be defined inside main function or other user defined function(s).
–         Nested loops are allowed.
–         Instructions can be anywhere inside the program.
–         Number of instructions inside any user defined function must not be more than 100.
If any of the above conditions is not satisfied, then the program will generate compilation errors. Today Raj has written a few programs, but he is not sure about the correctness of the programs. Your task is to help him to find out whether his program will compile without any errors or not.
Input Format:
First line starts with T, number of test cases. Each test case will contain a single line L, where L is a program written by Raj.
Output Format:
Print “No Compilation Errors” if there are no compilation errors, else print “Compilation Errors”.


Constraints:


1<=T<=100


L is a text and can be composed of any of the characters {, }, (, ), <, >and P, where P will represents the instruction.


L, comprised of characters mentioned above should be single spaced delimited.
Number of characters in the text, |L| < = 10000

Sample Input and Output


SNo. Input Output
1 3

{ < > ( P ) }

{ < { } > ( { } ) )

{ ( { } ) }
No Compilation Errors

Compilation Errors

Compilation Errors




Note:
Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Note:
Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc

TCS CodeVita previous years questions – Compiler Design solution
Sample Solution in C Programming:
#include<stdio.h>

int main()

{

int T;

char arr[100][10000];

scanf(“%d”,&T);

while(T>0)

{

int i,length,e=0,mf=0,nmf=0,uf=0,l=0;

scanf(“%s”,arr[T]);

length=strlen(arr[T]);

if(arr[T][0]!='{‘ || arr[T][length-1]!=’}’)

e++;

for(i=1;i<length-1;i++)

{

if(arr[T][i]=='<‘ && mf==0 && uf==0 && l==0)

{

mf++;

nmf++;

}

else if(arr[T][i]==’>’ && mf==1 && uf==0 && l==0)

mf–;

else if(arr[T][i]=='(‘ && mf==0 && uf==0 && l==0)

uf++;

else if(arr[T][i]==’)’ && mf==0 && uf==1 && l==0)

uf–;

else if(arr[T][i]=='{‘ && (mf==1 || uf==1))

l++;

else if(arr[T][i]==’}’ && l>0 && (mf==1 || uf==1))

l–;

else if(arr[T][i]==’P’);

else

e++;

}

if(e!=0 || uf!=0 || mf!=0 || nmf=1 || l!=0)

printf(“\nCompilation Error\n”);

else

printf(“\nNo Compilation Error\n”);

T–;

}

return 0;

}

/* use of variables:
T-numer of test cases
length- length of the test case
arr[][]-storing test cases (single dimension array also satisfy the question)
uf- count of < & > (checking main functions presence)
nmf- count of < (checking main function is present only once)
uf- count of ( & ) (checking user function creation)
l- count of { & } ( checking loops)
e-compilation error
validate the constraints too
*/

Comments

Popular posts from this blog

A Ten Year Journey: How SEO PowerSuite Has Kept Its SEO Tools Current

4 Ways to Crack a Facebook Password and How to Protect Yourself from Them

Create Separate Contact Page in Blogger