You are on page 1of 12

Course: - MCA 1st SEM

Subject Code: - 20MCA21CL1

Practical File: - Compiler Design.

Submitted To:- Submitted By:-

Ms. Ritu Agrawal Lalit Kumar


Index

Program Titel Date Remarks


No.
1 Program to check
Weather a given 29-01-2021
identifier is
valid or not
2 Program to Create 08-02-2021
an autometa that
will accept all
string ends with
‘b’
3 Program to 15-02-2021
recognize string
under
‘a’,’a*b+’,’abb’
4 Program to check 25-02-2021
no of vovels and
consonents in a
given string.
Output:-
1. WAP to test weather a given identifier is valid or not.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>

void main()
{

char a[10];
int flag, i=1;
clrscr();

printf("\n Enter an identifier:");


gets(a);
if(isalpha(a[0]))
flag=1;
else
printf("\n Not a valid identifier");
while(a[i]!='\0')
{
if(!isdigit(a[i])&&!isalpha(a[i]))
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("\n Valid identifier");
getch();
}
Output:-
2. WAP to create an autometa that will accept all strings
ending with ‘b’ over the alphabet set {a,b}.

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
char ch[]="ababaccca";
clrscr();
for(i=0;i<strlen(ch);i++)
{
if(i!=strlen(ch)-1)
{
if(ch[i]=='a'||ch[i]=='b')
{
continue;
}
else
{
for(i=0;i<strlen(ch);i++)
{
printf("%c",ch[i]);
}
printf("\nstring is not accepted");
break;
}
}
else
{
if(ch[i]=='b')
{
for(i=0;i<strlen(ch);i++)
{
printf("%c",ch[i]);
}
printf("\nString is accepted");
break;
}
else
{
for(i=0;i<strlen(ch);i++)
{
printf("%c",ch[i]);
}
printf("\nString is not accepted");
break;
}
}
}
getch();
}

Output:-
3.WAP to recognize strings under ‘a’, ‘a*b+’, ‘abb’.

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char s[20],c;
int state=0,i=0;
clrscr();
printf("\n Enter a string:");
gets(s);
while(s[i]!='\0')
{
switch(state)
{
case 0: c=s[i++];
if(c=='a')
state=1;
else if(c=='b')
state=2;
else
state=6;
break;
case 1: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=4;
else
state=6;
break;
case 2: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 3: c=s[i++];
if(c=='a')
state=3;
else if(c=='b')
state=2;
else
state=6;
break;
case 4: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=5;
else
state=6;
break;
case 5: c=s[i++];
if(c=='a')
state=6;
else if(c=='b')
state=2;
else
state=6;
break;
case 6: printf("\n %s is not recognised.",s);
exit(0);
}
}
if(state==1)
printf("\n %s is accepted under rule 'a'",s);
else if((state==2)||(state==4))
printf("\n %s is accepted under rule 'a*b+'",s);
else if(state==5)
printf("\n %s is accepted under rule 'abb'",s);
getch();
}

Output:-
4 WAP to find the total number of vovels and consonents in the
given string.

#include <stdio.h>
#include <conio.h>
int main()
{ clrscr();
int c = 0, count = 0;
char s[100];

printf("Input a string\n");
gets(s);

while (s[c] != '\0')


{
if (s[c] == 'a' || s[c] == 'A' || s[c] == 'e'
|| s[c] == 'E' || s[c] == 'i' || s[c] == 'I'
|| s[c] =='o' || s[c]=='O' || s[c] == 'u' || s[c] == 'U')
count++;
if (s[c]!= 'a' || s[c]!= 'A' || s[c]!= 'e'
|| s[c]!= 'E' || s[c]!= 'i' || s[c]!= 'I'
|| s[c]!= 'o' || s[c]!='O' || s[c]!= 'u' || s[c]!= 'U')
c++;
}
printf("Number of vowels and consonents in the string: %d\t
%d", count,c-count);

getch();
return 0;
}

You might also like