Guest User

Untitled

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