Advertisement
Guest User

Lab 13

a guest
Oct 19th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. LAB 15:
  2. PDA accepting equal numbers of 0’s and 1’s with empty stack PDA description:
  3.  
  4. It starts at q0 and pushes $ into the empty stack. At the same state q0,
  5. PDA pushes input symbol if stack top symbol is $ or stack top is same as
  6. input symbol. Otherwise if input is 0 and stack top is 1 or input is 1
  7. and stack top is 0, stack top is popped off. If input is finished and
  8. stack top is $, PDA accepts the input strings.
  9.  
  10. Code for PDA Accepting by empty Stack
  11. #include<stdio.h>
  12. #include<string.h>
  13. #define MAX 100
  14. Enum states {q0};
  15. void push(char ch);
  16. void pop();
  17. char get_stack_top();
  18. enum states delta(enum states, char, char);
  19. struct stack
  20. {
  21. Char symbols[MAX];
  22. int top;
  23. };
  24. struct stack s;
  25. int main()
  26. {
  27. char input[20];
  28. enum states curr_state=q0;
  29. s.top=-1;
  30. int i=0;
  31. char ch=’e’;
  32. char st_top=’e’;
  33. curr_state=delta(curr_state,ch,st_top);
  34. printf(“\n Enter a binary string\t”);
  35. gets(input);
  36. ch=input[i];
  37. st_top= get_stack_top();
  38. int c=0;
  39. while(c<=strlen(input))
  40. {
  41. curr_state=delta(curr_state,ch,st_top);
  42. ch=input[++i];
  43. st_top=get_stack_top();
  44. c++;
  45. }
  46. if(s.symbols[s.top]==’$’)
  47. printf(“\nThe string %s is accepted.”,input);
  48. else
  49. printf(“\nThe string %s is not accepted.”,input);
  50. return 0;
  51. }
  52. enum states delta(enum states s, char ch, chat st_top)
  53. {
  54. enum states curr_state;
  55. switch(s)
  56. {
  57. case q0;
  58. if(ch==’e’ && st_top==’e’)
  59. {
  60. curr_state = q0;
  61. push(‘$’);
  62. }
  63. else if(ch==’0’ && (st_top==’$’ || st_stop==’0’))
  64. {
  65. curr_state = q0;
  66. push(ch);
  67. }
  68. else if(ch==’1’ && (st_top==’$’ ||st_top==’1’))
  69. {
  70. curr_state = q0;
  71. push();
  72. }
  73. else if(ch==’1’ && st_top==’0’ ||ch==’0’ &&st_top==’1’)
  74. {
  75. curr_state = q0;
  76. pop();
  77. }
  78. else if(ch==’\0’ &&st_top==’$’)
  79. {
  80. curr_state = q0;
  81. //pop();
  82. }
  83. break;
  84. }
  85. return surr_state;
  86. }
  87. char get_stack_top()
  88. {
  89. return(s.symbols[s.top]);
  90. }
  91. void push(char ch)
  92. {
  93. if(s.top<MAX-1)
  94. {
  95. s.symbols[++s.top] =ch;
  96. }
  97. else
  98. {
  99. printf(“\nStack Full.”);
  100. }
  101. }
  102. void pop()
  103. {
  104. if(s.top>-1)
  105. {
  106. s.symbols[s.top]=’ ‘;
  107. s.top--;
  108. }
  109. else
  110. printf(“\nStack Empty.”);
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement