Вы находитесь на странице: 1из 16

/* Write a program for recognision of a symbols */ #include<stdio.h> #include<ctype.

h> void main() { char ch; ch=getchar(); if (isalpha(ch)) ch=getchar(); else error(); while(isalpha(ch)||isdigit(ch)) ch=getchar(); } Output: a accepted /* Write a program for recognision of a set of symbols */ #include<stdio.h> #include<ctype.h> void main() { void error(); char ch,*c; int i=0; clrscr(); printf("\n Enter a character\n");

ch=getchar(); c[i++]=ch; fflush(stdin); if(isalpha(ch)||isdigit(ch)) { c[i++]=','; ch=getchar(); c[i++]=ch; } else error(); fflush(stdin); while(isalpha(ch)||isdigit(ch)) { fflush(stdin); ch=getchar(); if(isalpha(ch)||isdigit(ch)) { c[i++]=','; c[i++]=ch; } } c[i]='\0'; printf("\nThe set of symbols is {%s}",c); getch(); } Output: a

B C D E The set of symbols is {a,b,c,d,e }

/* Write a program for recognition of a identifier*/ #include<stdio.h> #include<ctype.h> char main() { char state='q',ch; ch=getchar(); while(isalpha(ch)||isdigit(ch)) { switch(state) { case 'q' :if(isalpha(ch)) state='f'; else printf("error"); break; case 'f':state='f'; break;

} ch=getchar();

} return(state=='f'); } OUTPUT: Name Accepted

/*loads file with predicate isdigit*/ #include<stdio.h> #include<ctype.h> int lookahead; void main() { lookahead = getchar(); expr(); putchar('\n'); } expr() { term(); while(1) if(lookahead == '+') { match('+'); term(); putchar('+'); }

else if(lookahead == '-') { match('-'); term(); putchar('-'); } else break; } term() { if(isdigit(lookahead)) { putchar(lookahead); match(lookahead); } else error(); //printf("ERROR"); } match(t) { //int t; if(lookahead == t) lookahead = getchar(); else

error(); } error() { printf("syntax error\n"); exit(1); } output :22 12 33 2 1 3 /* WAP to accept a real number by finite Automata*/ #include<stdio.h> #include<ctype.h> int ysign(char sign) { return(sign == '+'||sign == '-' ); } char main() { char state; char ch; state = 'q'; ch = getchar (); while (isdigit(ch) || ysign(ch) || ch == '.') {

switch(state) { case 'q': if(isdigit(ch) ||ysign (ch)) state = 'p'; else if ((ch) == '.') state = 'r'; break; case 'p': if(isdigit(ch)) state = 'p'; else if (ch == '.') state = 'r'; else printf("ERROR"); break; case 'r': if(isdigit(ch)) state = 'f'; else printf("ERROR"); break; case 'f': if(isdigit(ch)) state = 'f'; else

printf("ERROR"); break; } ch = getchar(); } return(state == 'f'); } Output: 236.754 Accepted 34cvf error /* WAP to design a finite automata which accept all binary strings end with at least two 1's */ #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char num[20],state='p'; int i; clrscr(); printf("enter a binary string"); scanf("%s",num); for(i=0;num[i]!='\0';i++) { if(isalpha(num[i])) {printf("error");

break; } else { switch(state) { case 'p': if(num[i]=='0') state='p'; else state = 'q'; break; case 'q':if(num[i]=='1') state = 'f'; else state='p'; break; case 'f':if(num[i]=='1') state = 'f'; else state='p'; break; } } } if(state=='f')

printf("\n accepted "); else printf("error"); getch(); } Output : 111001011 Accepted 10101 Error /* WAP to design a finite automata which accept all binary strings which is divisible by 3 */ #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char num[20],state='p'; int i; clrscr(); printf("enter a binary string"); scanf("%s",num); for(i=0;num[i]!='\0';i++) { if(isalpha(num[i])) {printf("error"); break; }

else { switch(state) { case 'p': if(num[i]=='0') state='p'; else state = 'q'; break; case 'q':if(num[i]=='1') state = 'p'; else state='r'; break; case 'f':if(num[i]=='1') state = 'r'; else state='q'; break; } } } if(state=='p') printf("\n accepted "); else

printf("error"); getch(); } Output : 11 Accepted 101 error /* WAP to design a finite automata which accept all binary strings which contains odd no. 0's & 1s */ #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char num[20],state='p'; int i; clrscr(); printf("enter a binary string"); scanf("%s",num); for(i=0;num[i]!='\0';i++) { if(isalpha(num[i])) {printf("error"); break; } else {

switch(state) { case 'p': if(num[i]=='0') state='r'; else state = 'q'; break; case 'q':if(num[i]=='1') state = 'p'; else state='f'; break; case 'f':if(num[i]=='1') state = 'r'; else state='q'; break; case 'r':if(num[i]=='1') state = 'f'; else state='p'; break; } }} if(state=='f')

printf("\n accepted "); else printf("error"); getch(); } Output: 101010 Accepted 1001 error /* WAP to design finite automata for natural no.s divisible by 5*/ #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char num[20],state='p'; int i; clrscr(); printf("enter a natural no."); scanf("%s",num); for(i=0;num[i]!='\0';i++) { if(isalpha(num[i])) {state='E'; break; }

else { switch(state) { case 'p': if(num[i]=='0' ||num[i]=='5') state='p'; else state = 'q'; break; case 'q': if(num[i]=='0'||num[i]=='5') state = 'p'; else state ='q'; break; } } } if(state=='p') printf("\n %s is divisible by '5' ",num); else printf("error");; getch(); } Output : 873

Error 21585 Accepted

Вам также может понравиться