Advertisement
Sri27119

cd6 first

Nov 20th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. //S->Ab|B {a,b,c}
  2. //A->aA|e {a,e}
  3. //B->b|C {b,c}
  4. //C->c {c}
  5.  
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9.  
  10. #define MAX_NON_TERMINALS 10
  11. #define MAX_PRODUCTIONS 10
  12. #define MAX_SYMBOLS 10
  13.  
  14. typedef struct {
  15. char non_terminal;
  16. char production[MAX_PRODUCTIONS][MAX_SYMBOLS];
  17. int count;
  18. } Production;
  19.  
  20. Production productions[MAX_NON_TERMINALS];
  21. char first[MAX_NON_TERMINALS][MAX_SYMBOLS] = {0};
  22. int production_count = 0, non_terminal_count = 0;
  23.  
  24. void addProduction(char non_terminal, char *prod) {
  25. for (int i = 0; i < production_count; i++) {
  26. if (productions[i].non_terminal == non_terminal) {
  27. strcpy(productions[i].production[productions[i].count++], prod);
  28. return;
  29. }
  30. }
  31. productions[production_count].non_terminal = non_terminal;
  32. strcpy(productions[production_count].production[productions[production_count].count++], prod);
  33. production_count++;
  34. }
  35.  
  36. int findNonTerminalIndex(char non_terminal) {
  37. for (int i = 0; i < non_terminal_count; i++) {
  38. if (first[i][0] == non_terminal)
  39. return i;
  40. }
  41. first[non_terminal_count][0] = non_terminal;
  42. return non_terminal_count++;
  43. }
  44.  
  45. int addToFirst(int index, char symbol) {
  46. for (int i = 1; first[index][i]; i++) {
  47. if (first[index][i] == symbol)
  48. return 0;
  49. }
  50. first[index][strlen(first[index])] = symbol;
  51. return 1;
  52. }
  53.  
  54. void computeFirst() {
  55. int changes;
  56. do {
  57. changes = 0;
  58. for (int i = 0; i < production_count; i++) {
  59. int index = findNonTerminalIndex(productions[i].non_terminal);
  60. for (int j = 0; j < productions[i].count; j++) {
  61. char *prod = productions[i].production[j];
  62. for (int k = 0; prod[k]; k++) {
  63. if (islower(prod[k]) || prod[k] == 'e') {
  64. changes |= addToFirst(index, prod[k]);
  65. break;
  66. }
  67. int ntIndex = findNonTerminalIndex(prod[k]);
  68. int hasEpsilon = 0;
  69. for (int l = 1; first[ntIndex][l]; l++) {
  70. if (first[ntIndex][l] == 'e')
  71. hasEpsilon = 1;
  72. else
  73. changes |= addToFirst(index, first[ntIndex][l]);
  74. }
  75. if (!hasEpsilon) break;
  76. }
  77. }
  78. }
  79. } while (changes);
  80. }
  81.  
  82. void displayFirstSets() {
  83. printf("\nFIRST sets of non-terminals:\n");
  84. for (int i = 0; i < non_terminal_count; i++) {
  85. printf("FIRST(%c) = { ", first[i][0]);
  86. for (int j = 1; first[i][j]; j++) {
  87. printf("%c ", first[i][j]);
  88. }
  89. printf("}\n");
  90. }
  91. }
  92.  
  93. int main() {
  94. printf("Enter the number of productions: ");
  95. int n;
  96. scanf("%d", &n);
  97. getchar();
  98.  
  99. printf("Enter productions (e.g., A-->aB|e):\n");
  100. for (int i = 0; i < n; i++) {
  101. char line[100], *prod;
  102. fgets(line, sizeof(line), stdin);
  103. line[strcspn(line, "\n")] = '\0';
  104.  
  105. char *non_terminal = strtok(line, "-->");
  106. prod = strtok(NULL, "-->");
  107. for (char *token = strtok(prod, "|"); token; token = strtok(NULL, "|")) {
  108. addProduction(non_terminal[0], token);
  109. }
  110. }
  111.  
  112. computeFirst();
  113. displayFirstSets();
  114. return 0;
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement