T_tasir256

Untitled

Nov 17th, 2021
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.91 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // Functions to calculate Follow
  4. void followfirst(char, int, int);
  5. void follow(char c);
  6.  
  7. // Function to calculate First
  8. void findfirst(char, int, int);
  9.  
  10. int cnt, n = 0;
  11.  
  12. // Stores the final result
  13. // of the First Sets
  14. char calc_first[10][100];
  15.  
  16. // Stores the final result
  17. // of the Follow Sets
  18. char calc_follow[10][100];
  19. int m = 0;
  20.  
  21. // Stores the production rules
  22. char production[10][10];
  23. char f[10], first[10];
  24. int k;
  25. char ck;
  26. int e;
  27.  
  28. int main(int argc, char **argv)
  29. {
  30. int jm = 0;
  31. int km = 0;
  32. int i, choice;
  33. char c, ch;
  34. // count = 8;
  35. scanf("%d", &cnt);
  36. // cin>>count;
  37. for (int i = 0; i < cnt; i++)
  38. {
  39. cin >> production[i];
  40. // printf("%c", production[i]);
  41. }
  42.  
  43. // The Input grammar
  44.  
  45. int kay;
  46. char done[cnt];
  47. int ptr = -1;
  48.  
  49. // Initializing the calc_first array
  50. for (k = 0; k < cnt; k++)
  51. {
  52. for (kay = 0; kay < 100; kay++)
  53. {
  54. calc_first[k][kay] = '!';
  55. }
  56. }
  57. int point1 = 0, point2, xxx;
  58.  
  59. for (k = 0; k < cnt; k++)
  60. {
  61. c = production[k][0];
  62. point2 = 0;
  63. xxx = 0;
  64.  
  65. // Checking if First of c has
  66. // already been calculated
  67. for (kay = 0; kay <= ptr; kay++)
  68. if (c == done[kay])
  69. xxx = 1;
  70.  
  71. if (xxx == 1)
  72. continue;
  73.  
  74. // Function call
  75. findfirst(c, 0, 0);
  76. ptr += 1;
  77.  
  78. // Adding c to the calculated list
  79. done[ptr] = c;
  80. printf("\n First(%c) = { ", c);
  81. calc_first[point1][point2++] = c;
  82.  
  83. // Printing the First Sets of the grammar
  84. for (i = 0 + jm; i < n; i++)
  85. {
  86. int lark = 0, chk = 0;
  87.  
  88. for (lark = 0; lark < point2; lark++)
  89. {
  90.  
  91. if (first[i] == calc_first[point1][lark])
  92. {
  93. chk = 1;
  94. break;
  95. }
  96. }
  97. if (chk == 0)
  98. {
  99. printf("%c, ", first[i]);
  100. calc_first[point1][point2++] = first[i];
  101. }
  102. }
  103. printf("}\n");
  104. jm = n;
  105. point1++;
  106. }
  107. printf("\n");
  108. printf("-----------------------------------------------\n\n");
  109. char donee[cnt];
  110. ptr = -1;
  111.  
  112. // Initializing the calc_follow array
  113. for (k = 0; k < cnt; k++)
  114. {
  115. for (kay = 0; kay < 100; kay++)
  116. {
  117. calc_follow[k][kay] = '!';
  118. }
  119. }
  120. point1 = 0;
  121. int land = 0;
  122. for (e = 0; e < cnt; e++)
  123. {
  124. ck = production[e][0];
  125. point2 = 0;
  126. xxx = 0;
  127.  
  128. // Checking if Follow of ck
  129. // has already been calculated
  130. for (kay = 0; kay <= ptr; kay++)
  131. if (ck == donee[kay])
  132. xxx = 1;
  133.  
  134. if (xxx == 1)
  135. continue;
  136. land += 1;
  137.  
  138. // Function call
  139. follow(ck);
  140. ptr += 1;
  141.  
  142. // Adding ck to the calculated list
  143. donee[ptr] = ck;
  144. printf(" Follow(%c) = { ", ck);
  145. calc_follow[point1][point2++] = ck;
  146.  
  147. // Printing the Follow Sets of the grammar
  148. for (i = 0 + km; i < m; i++)
  149. {
  150. int lark = 0, chk = 0;
  151. for (lark = 0; lark < point2; lark++)
  152. {
  153. if (f[i] == calc_follow[point1][lark])
  154. {
  155. chk = 1;
  156. break;
  157. }
  158. }
  159. if (chk == 0)
  160. {
  161. printf("%c, ", f[i]);
  162. calc_follow[point1][point2++] = f[i];
  163. }
  164. }
  165. printf(" }\n\n");
  166. km = m;
  167. point1++;
  168. }
  169. }
  170.  
  171. void follow(char c)
  172. {
  173. int i, j;
  174.  
  175. // Adding "$" to the follow
  176. // set of the start symbol
  177. if (production[0][0] == c)
  178. {
  179. f[m++] = '$';
  180. }
  181. for (i = 0; i < 10; i++)
  182. {
  183. for (j = 2; j < 10; j++)
  184. {
  185. if (production[i][j] == c)
  186. {
  187. if (production[i][j + 1] != '\0')
  188. {
  189. // Calculate the first of the next
  190. // Non-Terminal in the production
  191. followfirst(production[i][j + 1], i, (j + 2));
  192. }
  193.  
  194. if (production[i][j + 1] == '\0' && c != production[i][0])
  195. {
  196. // Calculate the follow of the Non-Terminal
  197. // in the L.H.S. of the production
  198. follow(production[i][0]);
  199. }
  200. }
  201. }
  202. }
  203. }
  204.  
  205. void findfirst(char c, int q1, int q2)
  206. {
  207. int j;
  208.  
  209. // The case where we
  210. // encounter a Terminal
  211. if (!(isupper(c)))
  212. {
  213. first[n++] = c;
  214. }
  215. for (j = 0; j < cnt; j++)
  216. {
  217. if (production[j][0] == c)
  218. {
  219. if (production[j][2] == '#')
  220. {
  221. if (production[q1][q2] == '\0')
  222. first[n++] = '#';
  223. else if (production[q1][q2] != '\0' && (q1 != 0 || q2 != 0))
  224. {
  225. // Recursion to calculate First of New
  226. // Non-Terminal we encounter after epsilon
  227. findfirst(production[q1][q2], q1, (q2 + 1));
  228. }
  229. else
  230. first[n++] = '#';
  231. }
  232. else if (!isupper(production[j][2]))
  233. {
  234. first[n++] = production[j][2];
  235. }
  236. else
  237. {
  238. // Recursion to calculate First of
  239. // New Non-Terminal we encounter
  240. // at the beginning
  241. findfirst(production[j][2], j, 3);
  242. }
  243. }
  244. }
  245. }
  246.  
  247. void followfirst(char c, int c1, int c2)
  248. {
  249. int k;
  250.  
  251. // The case where we encounter
  252. // a Terminal
  253. if (!(isupper(c)))
  254. f[m++] = c;
  255. else
  256. {
  257. int i = 0, j = 1;
  258. for (i = 0; i < cnt; i++)
  259. {
  260. if (calc_first[i][0] == c)
  261. break;
  262. }
  263.  
  264. // Including the First set of the
  265. // Non-Terminal in the Follow of
  266. // the original query
  267. while (calc_first[i][j] != '!')
  268. {
  269. if (calc_first[i][j] != '#')
  270. {
  271. f[m++] = calc_first[i][j];
  272. }
  273. else
  274. {
  275. if (production[c1][c2] == '\0')
  276. {
  277. // Case where we reach the
  278. // end of a production
  279. follow(production[c1][0]);
  280. }
  281. else
  282. {
  283. // Recursion to the next symbol
  284. // in case we encounter a "#"
  285. followfirst(production[c1][c2], c1, c2 + 1);
  286. }
  287. }
  288. j++;
  289. }
  290. }
  291. }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment