Compiler Construction
Compiler Construction
h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void main()
{
char string[20];
int state=0,count=0;
clrscr();
cout<<"the grammar is: S->aS, S->Sb, S->ab \n";
cout<<"enter the string to be checked \n";
gets(string);
while(string[count]!='\0')
{
switch(state)
{
case 0: if (string[count]=='a')
state=1;
else
state=3;
break;
case 1: if (string[count]=='a')
state=1;
else if(string[count]=='b')
state=2;
else
state=3;
break;
case 2: if (string[count]=='b')
state=2;
else
state=3;
break;
default: break;
}
count++;
if(state==3)
break;
}
if(state==2)
cout<<"string is accepted";
else
cout<<"string is not accepted";
getch();
}
1.
1. #include<stdio.h>
2. #include<conio.h>
3. #include<string.h>
4. void main() {
5.
char string[50];
6.
int flag,count=o;
7.
clrscr();
8.
printf("The grammar is: S->aS, S->Sb, S->ab\n");
9.
printf("Enter the string to be checked:\n");
10.
gets(string);
11.
if(string[0]=='a') {
12.
flag=0;
13.
for (count=1;string[count-1]!='\0';count++) {
14.
if(string[count]=='b') {
15.
flag=1;
16.
continue;
17.
} else
if((flag==1)&&(string[count]=='a')) {
18.
printf("The string does not
belong to the specified grammar");
19.
break;
20.
} else if(string[count]=='a')
21.
continue; else
if(flag==1)&&(string[count]='\0')) {
22.
printf("String
accepted..!!!!");
23.
break;
24.
} else {
25.
printf("String not accepted");
26.
}
27.
}
28.
}
29.
getch();
30.
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
void main()
{
int i=0,flag=0;
char a[10][10]={"int","float","break","long","char","for","if","switch","else","while"},string[10];
clrscr();
printf("Enter a string :");
gets(string);
/*----Checking whether the string is in array a[][]----*/
for(i=0;i<10;i++)
{
if((strcmp(a[i],string)==0))
flag=1;
}
/*----If it is in the array then it is a keyword----*/
if(flag==1)
printf("\n%s is a keyword ",string);
/*----Otherwise check whether the string is an identifier----*/
else
{
flag=0;
/*----Checking the 1st character*----*/
if((string[0]=='_')||(isalpha(string[0])!=0))
{
/*---Checking rest of the characters*---*/
for(i=1;string[i]!='\0';i++)
if((isalnum(string[i])==0)&&(string[i]!='_'))
flag=1;
}
else
flag=1;
if( flag==0)
printf("\n%s is an identifier ",string);
else
printf("\n%s is neither a keyword nor an identifier ",string);
}
getch();
}
#include <iostream.h>
#include <conio.h>
#include <string.h>
enum track {true, false};
void main()
{
clrscr();
char*str;
enum track track_pos, track_pos_2;
cout<<"enter the string: ";
cin>>str;
int len=strlen(str);
cout<<"length of the string is "<<len;
getch();
int i;
for(i=0;i<len; i++)
{
++str;
cout<<"loop"<<i;
if(*str=='a' && i%2==0)
{
cout<<"\nchecking a...";
track_pos=true;
cout<<"\na.check";
++str;
if (*str=='b')
{
cout<<"\nchecking b...";
track_pos=true;
cout<<"\nb.check";
}
else{
track_pos=false;
cout<<"\nb.uncheck";
}
}
}
if(*str=='b')
track_pos_2=true;
else
track_pos_2=false;
if(track_pos==true && track_pos_2==true)
cout<<"\nThe string is accpeted.";
else
cout<<"\nThe string is rejected.";
getch();
cout<<"\n\nDo you want to continue (Y/N)? ";
char ch;
cin>>ch;
if(ch=='y' || ch=='Y')
main();
}
#include <iostream.h>
#include <conio.h>
#include <string.h>
enum track {true, false};
void main()
{
clrscr();
char*str;
enum track track_pos, track_pos_2;
cout<<"enter the string: ";
cin>>str;
int len=strlen(str);
cout<<"length of the string is "<<len;
getch();
int i;
for(i=0;i<len; i++)
{
++str;
cout<<"loop"<<i;
if(*str=='a' && i%2==0)
{
cout<<"\nchecking a...";
track_pos=true;
cout<<"\na.check";
++str;
if (*str=='b')
{
cout<<"\nchecking b...";
track_pos=true;
cout<<"\nb.check";
}
else{
track_pos=false;
cout<<"\nb.uncheck";
}
}
}
if(*str=='b')
track_pos_2=true;
else
track_pos_2=false;