0% found this document useful (0 votes)
15 views4 pages

1

Uploaded by

Devu Narayana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

1

Uploaded by

Devu Narayana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1)

a)
#include <iostream>

using namespace std;

int main()
{
string input;
cout<<"Enter The String :"<<endl;
cin>>input;
int n=input.size();
int i=0;
string state="q0";
while(input[i]!='\0'){
switch(input[i]){
case 'a':
if (state=="q0")
state="q2";
else if(state=="q2")
state="q3";
else if(state=="q3")
state="q3";
else if(state=="q1")
state="q2";
else if(state=="q4")
state="q2";

break;
case 'b':
if (state=="q0")
state="q1";
else if(state=="q1")
state="q4";
else if(state=="q2")
state="q1";
else if(state=="q3")
state="q1";
else if(state=="q4")
state="q4";
break;
default:
cout<<"invalid String";
exit(0);
break;

}
i++;
}
cout<<endl;
if(state=="q3"||state=="q4")
cout<<"String is Accepted";
else
cout<<"String is not accepted";

return 0;
}
b)
#include<iostream>
using namespace std;
int main(){
string input;
cout<<"Enter String"<<endl;
cin>>input;
int i=0;
string state="q0";
if (input=="\0"){
cout<<"String not accepted";
exit(0);
}
//Accepting set all strings with even number of a an b
while(input[i]!='\0'){
switch(input[i]){
case 'a':
if (state=="q0")
state="q1";
else if(state=="q1")
state="q0";
else if(state=="q2")
state="q3";
else if (state=="q3")
state="q2";
break;
case 'b':
if (state=="q0")
state="q2";
else if(state=="q1")
state="q3";
else if(state=="q2")
state="q0";
else if (state=="q3")
state="q1";
break;
default:
cout<<"invalid String";
exit(0);
break;

}
i++;
}
if(state=="q0")
cout<<"String is Accepted";
else
cout<<"String is not accepted";

}
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
{
FILE *input, *output;
int l=1;
int t=0;
int j=0;
int i,flag;
char ch,str[20];
input = fopen("input.txt","r");
output = fopen("output.txt","w");
char keyword[20][20] =
{"int","float","double","char","string","main","if","else","do","while","printf","i
nclude","void","main"};
fprintf(output,"Line no. \t Token no. \t Token \t Lexeme\n\n");
while(!feof(input))
{
i=0;
flag=0;
ch=fgetc(input);
if( ch=='+' || ch== '-' || ch=='*' || ch=='/'|| ch=='=' )
{
fprintf(output,"%7d\t\t %7d\t\t Operator\t %7c\n",l,t,ch);
t++;
}
else if( ch==';' || ch=='{' || ch=='}' || ch=='(' || ch==')' || ch=='?' ||
ch=='@' || ch=='!' || ch=='#' || ch=='%')
{
fprintf(output,"%7d\t\t %7d\t\t Special symbol\t %7c\n",l,t,ch);
t++;
}
else if(isdigit(ch))
{
fprintf(output,"%7d\t\t %7d\t\t Digit\t\t %7c\n",l,t,ch);
t++;
}
else if(isalpha(ch))
{
str[i]=ch;
i++;
ch=fgetc(input);
while(isalnum(ch) && ch!=' ')
{
str[i]=ch;
i++;
ch=fgetc(input);
}
str[i]='\0';
for(j=0;j<=30;j++)
{
if(strcmp(str,keyword[j])==0)
{
flag=1;
break;
}
}
if(flag==1)
{
fprintf(output,"%7d\t\t %7d\t\t Keyword\t %7s\n",l,t,str);
t++;
}
else
{
fprintf(output,"%7d\t\t %7d\t\t Identifier\t %7s\n",l,t,str);
t++;
}

}
else if(ch=='\n')
{
l++;
}
}
fclose(input);
fclose(output);
return 0;
}

You might also like