fork download
  1. #include<stdio.h>
  2.  
  3. #include<math.h>
  4.  
  5. #include<string.h>
  6.  
  7. #include<ctype.h>
  8.  
  9. #include<stdlib.h>
  10.  
  11. int n, m = 0, p, i = 0, j = 0;
  12. char a[10][10], f[10];
  13. void follow(char c);
  14. void first(char c);
  15. int main() {
  16. int i, z;
  17. char c, ch;
  18. printf("Enter the no of productions : \n");
  19. scanf("%d", & n);
  20. printf("Enter the productions:\n");
  21. for (i = 0; i < n; i++)
  22. scanf("%s%c", a[i], & ch);
  23. do {
  24. m = 0;
  25. printf("Enter a variable whose fisrt & follow is to be found:");
  26.  
  27. scanf("%c", & c);
  28. first(c);
  29. printf("First(%c)={", c);
  30. for (i = 0; i < m; i++)
  31. printf("%c", f[i]);
  32. printf("}\n");
  33. strcpy(f, " ");
  34. m = 0;
  35. follow(c);
  36. printf("Follow(%c)={", c);
  37. for (i = 0; i < m; i++)
  38. printf("%c", f[i]);
  39. printf("}\n");
  40. printf("Want to continue or not(1/0) ? ");
  41. scanf("%d%c", & z, & ch);
  42. }
  43. while (z == 1);
  44. return (0);
  45. }
  46. void first(char c) {
  47. int k;
  48. if (!isupper(c))
  49. f[m++] = c;
  50. for (k = 0; k < n; k++) {
  51. if (a[k][0] == c) {
  52. if (a[k][2] == '$')
  53. follow(a[k][0]);
  54. else if (islower(a[k][2]))
  55. f[m++] = a[k][2];
  56. else
  57. first(a[k][2]);
  58.  
  59. }
  60. }
  61. }
  62. void follow(char c) {
  63. if (a[0][0] == c)
  64. f[m++] = '$';
  65. for (i = 0; i < n; i++) {
  66. for (j = 2; j < strlen(a[i]); j++) {
  67. if (a[i][j] == c) {
  68. if (a[i][j + 1] != '\0')
  69. first(a[i][j + 1]);
  70. if (a[i][j + 1] == '\0' && c != a[i][0])
  71. follow(a[i][0]);
  72.  
  73. }
  74. }
  75. }
  76. }
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Enter the no of productions : 
Enter the productions:
Enter a variable whose fisrt & follow is to be found:First()={}
Follow()={$}
Want to continue or not(1/0) ?