Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- DFA
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char a[10];
- printf("As the expression has to end with 01 as a substring it has 3 states 0,1,2\n");
- printf("Enter the expression to be checked:\n");
- scanf("%[^\n]s",a);
- printf("%s\n",a);
- int state = 0;
- for (int i=0; i<strlen(a);i++){
- if(state==0 && a[i]=='1'){
- state = 0;
- }
- else if(state==0 && a[i]=='0'){
- state = 1;
- }
- else if(state==1 && a[i]=='0'){
- state=1;
- }
- else if(state==1 && a[i]=='1'){
- state = 2;
- }
- else if(state==2 && a[i]=='0'){
- state = 1;
- }
- else if(state==2 && a[i]=='1'){
- state = 1;
- }
- }
- if(state==2){
- printf("The expression is accepted as it reached the final state 2");
- }
- else{
- printf("The expression is rejected as it didn't reach the final state, it is in state %d",state);
- }
- return 0;
- }
- NFA
- #include <stdio.h>
- #include <string.h>
- int main()
- {
- char a[10];
- printf("Enter the input string:\n");
- scanf("%[^\n]s",a);
- int state = 0;
- int i = 0;
- int len = strlen(a);
- while(i < len){
- if(state == 0 && a[i] == '0'){
- state = 0;
- }
- else if(state == 0 && a[i] == '1'){
- state = 1;
- }
- else if(state == 1 && a[i] == '0'){
- state = 1;
- }
- else if(state == 1 && a[i] == '1'){
- state = 2;
- }
- else if(state == 2 && a[i] == '0'){
- state = 0;
- }
- else if(state == 2 && a[i] == '1'){
- state = 2;
- }
- i++;
- }
- if(state == 2){
- printf("The input string is accepted as it reached the final state 2.and ending with 11\n");
- }
- else{
- printf("The input string is rejected as it didn't reach the final state, it is in state %d.\n", state);
- }
- return 0;
- }
- FIRST
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
- int num_nt;
- char nt[10];
- int p[10];
- char productions[10][10][10];
- void find_first(char non_t) {
- for (int i = 0; i < num_nt; i++) {
- if (nt[i] == non_t) {
- for (int j = 0; j < p[i]; j++) {
- char first = productions[i][j][0];
- if (isupper(first)) {
- find_first(first);
- } else {
- printf("%c ", first);
- }
- }
- }
- }
- }
- int main() {
- printf("Enter number of non-terminals:\n");
- scanf("%d", &num_nt);
- printf("Enter non-terminals:\n");
- for (int i = 0; i < num_nt; i++) {
- scanf(" %c", &nt[i]);
- }
- for (int i = 0; i < num_nt; i++) {
- printf("Enter the number of productions for %c: ", nt[i]);
- scanf("%d", &p[i]);
- printf("Enter the productions for %c:\n", nt[i]);
- for (int j = 0; j < p[i]; j++) {
- scanf("%s", productions[i][j]);
- printf("\n");
- }
- }
- for (int i = 0; i < num_nt; i++) {
- printf("First ( %c ): ", nt[i]);
- find_first(nt[i]);
- printf("\n");
- }
- return 0;
- }
- SYMBOL TABLE
- #include<stdio.h>
- #include<ctype.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h> void
- main()
- {
- int i=0,j=0,x=0,n; void *p,*add[5];
- char ch,srch,b[15],d[15],c;
- printf("Expression terminated by $:");
- while((c=getchar())!='$')
- {
- b[i]=c;
- i++;
- }
- n=i-1;
- printf("\nGiven Expression:");
- i=0;
- while(i<=n)
- {
- printf("%c",b[i]);
- i++;
- }
- printf("\n\nSymbol Table\n\n");
- printf("Symbol \t addr \t\t type");
- while(j<=n)
- {
- c=b[j];
- if(isalpha(toascii(c)))
- {
- p=malloc(c);
- add[x]=p; d[x]=c;
- printf("\n%c \t %d \t identifier\n",c,p);
- x++; j++;
- }
- else
- {
- ch=c;
- if(ch=='+'||ch=='-'||ch=='*'||ch=='=')
- {
- p=malloc(ch);
- add[x]=p; d[x]=ch;
- printf("\n%c \t %d \t operator\n",ch,p);
- x++; j++;
- }
- }
- }
- }
- PDA
- #include<stdio.h>
- #include<string.h>
- int top;
- char s[10];
- void push(int x)
- {
- s[top++]=x;
- }
- void pop()
- {
- top--;
- }
- int main()
- {
- int i,n,state=0;
- char a[10];
- printf("Program For PDA Which Accpets Strings Of (a^n)(b^n)\n");
- printf("Enter String:");
- gets(a);
- n=strlen(a);
- top=-1;
- for(i=0;i<n;i++)
- {
- if(a[i]=='a' && state==0)
- {
- push(a[i]);
- state=0;
- }
- else if(a[i]=='b' && state==0)
- {
- pop(a[i]);
- state=1;
- }
- else if(a[i]=='b' && state==1)
- {
- pop();
- state=1;
- }
- else
- {
- break;
- }
- }
- if(a[i]=='\0' && top==-1 && state==1)
- state=100;
- if(state==100)
- printf("String Accepted.");
- else
- printf("String Rejected.");
- return 0;
- }
Add Comment
Please, Sign In to add comment