Advertisement
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
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement