Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Experiment 1: D I T C: Tt1 Batch: B

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

MARWADI UNIVERSITY

DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

Experiment 1
Title: Write a C program to identify whether a given line is a comment or not

Program:

#include<stdio.h>
void main() {
char str[30];
int i=2,a=0;

printf("\n Enter comment:");


gets(str);
if(str[0]=='/') {
if(str[1]=='/')
printf("\n It is a comment");
else if(str[1]=='*') {
for(i=2;i<=30;i++)
{
if(str[i]=='*'&&str[i+1]=='/')
{
printf("\n It is a comment");
a=1;
break; }
else
continue; }
if(a==0)
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
}
else
printf("\n It is not a comment");
getch();

MOHAMAD MAHMOUD 91800104060 Page |


1
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

Output:

Experiment 2
Title: Write a C program to test that the given input is valid identifier or keyword.

Program:

#include <stdio.h>

void main()
{

int i=0,flag=0;
char
keyw[10][10]={"int","float","break","long","char","for","if","switch","else","while"},a[10];
printf("Enter Identifier : ");
gets(a);
for(i=0;i<10;i++)
{
if((strcmp(keyw[i],a)==0))
{
flag=1;

MOHAMAD MAHMOUD 91800104060 Page |


2
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

}
}
if(flag==1)
{
printf("\n%s is Keyword.",a);
}
else
{
flag=0;

if((a[0]=='_')||(isalpha(a[0])!=0))
{
for(i=1;a[i]!='\0';i++)
{
if((isalnum(a[i])==0)&&(a[i]!='_'))
{
flag=1;
}
}
}
else
{
flag=1;
}
}
if(flag==0)
{
printf("\n%s is an Identifier.",a);
}
else
{
printf("\n%s is Not an Identifier.",a);
}
getch();
}

MOHAMAD MAHMOUD 91800104060 Page |


3
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

Output:

Experiment 3
Title: Write a C program for dfa accepting string”abb”;

Program:

#include<stdio.h

>

#include<stdlib.h

>

void main()

int

initial_state=1,len,current_state=1;
MOHAMAD MAHMOUD 91800104060 Page |
4
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B
char a,b,str[10];

printf("Enter string : ");

MOHAMAD MAHMOUD 91800104060 Page |


5
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

scanf("%s",&str

);

len=strlen(str);

if(len==3){

if(initial_state==1 && str[0]=='a'){

current_state=2;

else {

printf("String is Rejected.");

exit(0);}

if(current_state==2 &&

str[1]=='b'){ current_state=3;

else {

printf("String is

Rejected."); exit(0);}

if(current_state==3 && str[2]=='b'){

current_state=4;

printf(" String is accepted");}

MOHAMAD MAHMOUD 91800104060 Page |


6
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

else {

printf("String is

rejected."); exit(0);}

else{

printf("Enter correct string."

); exit(0);

getch();

Output:

MOHAMAD MAHMOUD 91800104060 Page |


7
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

Experiment 4
Title: Write a C program to remove left recursion from the grammar.

Program:

#include<stdio.h

>

#include<conio.h

>

#include<string.h

> #define SIZE

10 void main () {

char

non_terminal;

char beta,alpha;

char

production[SIZE]; int

index=3;

printf("Enter the grammar:\n");

scanf("%s",production);

non_terminal=production[0];

if(non_terminal==production[index]

) { alpha=production[index+1];

printf("Grammar is left

MOHAMAD MAHMOUD 91800104060 Page |


8
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B
recursive.\n");

while(production[index]!=0 &&

production[index]!='|') index++;

if(production[index]!=0) {

MOHAMAD MAHMOUD 91800104060 Page |


9
MARWADI UNIVERSITY
DEPARTMENT OF INFORMATION
TECHNOLOGY CLASS: TT1 BATCH: B

beta=production[index+1];

printf("Grammar without left recursion:\n");

printf("%c-

>%c%c\'",non_terminal,beta,non_terminal);

printf("\n%c\'->%c%c\'|E\n",non_terminal,alpha,non_terminal);

else

printf("Grammar can't be reduced\n");

else

printf("Grammar is not left recursive.\n");

Output:

******************************* the end ****************************************

MOHAMAD MAHMOUD 91800104060 Page |


10

You might also like