ademosh

CYK

Nov 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #include "pch.h"
  2. #include<iostream>
  3. #include<cstring>
  4. #include<algorithm>
  5. #include<string>
  6. #include<cassert>
  7. #include<iomanip>
  8. #include<fstream>
  9. using namespace std;
  10.  
  11. #define MAX 100
  12. #define for(i,a,b) for(i=a;i<b; i++)
  13.  
  14. string gram[MAX][MAX]; //to store entered grammar
  15. string grambuf[MAX];
  16. string matrix[MAX][MAX];
  17. string dpr[MAX];
  18. int key;
  19. int p, np; //np-> number of productions
  20.  
  21. inline string concat(string a, string b) //concatenates unique non-terminals
  22. {
  23. int i;
  24. string r = a;
  25. for (i, 0, b.length())
  26. if (r.find(b[i]) > r.length())
  27. r += b[i];
  28. return (r);
  29. }
  30.  
  31. inline void break_gram(string a) //seperates right hand side of entered grammar
  32. {
  33. int i;
  34. p = 0;
  35. while (a.length())
  36. {
  37. i = a.find("|");
  38. if (i > a.length())
  39. {
  40. dpr[p++] = a;
  41. a = "";
  42. }
  43. else
  44. {
  45. dpr[p++] = a.substr(0, i);
  46. a = a.substr(i + 1, a.length());
  47. }
  48. }
  49. }
  50.  
  51. inline int lchomsky(string a) //checks if LHS of entered grammar is in CNF
  52. {
  53. if (a.length() == 1 && a[0] >= 'A' && a[0] <= 'Z')
  54. return 1;
  55. return 0;
  56. }
  57.  
  58. inline int rchomsky(string a) //checks if RHS of grammar is in CNF
  59. {
  60. if (a.length() == 1 && a[0] >= 'a' && a[0] <= 'z')
  61. return 1;
  62. if (a.length() == 2 && a[0] >= 'A' && a[0] <= 'Z' && a[1] >= 'A' && a[1] <= 'Z')
  63. return 1;
  64. return 0;
  65. }
  66.  
  67. inline string search_prod(string p) //returns a concatenated string of variables which can produce string p
  68. {
  69. int j, k;
  70. string r = "";
  71. for (j, 0, np)
  72. {
  73. k = 1;
  74. while (gram[j][k] != "")
  75. {
  76. if (gram[j][k] == p)
  77. {
  78. r = concat(r, gram[j][0]);
  79. }
  80. k++;
  81. }
  82. }
  83. return r;
  84. }
  85.  
  86. inline string gen_comb(string a, string b) //creates every combination of variables from a and b . For eg: BA * AB = {BA, BB, AA, BB}
  87. {
  88. int i, j;
  89. string pri = a, re = "";
  90. for (i, 0, a.length())
  91. for (j, 0, b.length())
  92. {
  93. pri = "";
  94. pri = pri + a[i] + b[j];
  95. re = re + search_prod(pri); //searches if the generated productions can be created or not
  96. }
  97. return re;
  98. }
  99. // if (matrix[0][str.length() - 1].find(start[i]) <= matrix[0][str.length() - 1].length())
  100. inline int gen(int i, int j, char B,int end, int ukaz)
  101. {
  102. cout << endl << i << ' ' << j << ' ' << B << endl;
  103. if (i == end) return -2;
  104. if (j == 0) {
  105. for (p, 0, ukaz) {
  106. if ((grambuf[p][0] == B) && (grambuf[p].length() == 2)) cout<<p<<" is parser number";
  107. }
  108. }
  109. else {
  110. int check = 0;
  111. int z, x, c;
  112. int buf;
  113. for (p, 0, j - 1) {
  114. for (z, 0, ukaz) {
  115. if ((grambuf[z][0] == B) &&
  116. ((matrix[i][p].find(grambuf[z][1]) <= matrix[i][p].length())) &&
  117. ((matrix[i + p][j - p].find(grambuf[z][2])) <= matrix[i + p][j - p].length())) {
  118. cout << z << " is parser number";
  119. cout << endl;
  120. cout << grambuf[z][1] << grambuf[z][2] << endl;
  121. cout << endl << i << ' ' << j << ' ' << B << endl << "Next step" << endl;
  122. gen(i, 0, grambuf[z][1], end, ukaz);
  123. gen(i+1 , j-1 , grambuf[z][2], end, ukaz);
  124. check = 1;
  125. break;
  126.  
  127. }
  128. }
  129. if (check == 1) break;
  130. }
  131. return -1;
  132. }
  133. }
  134.  
  135. int main()
  136. {
  137. int i, pt, j, l, k;
  138. string a, str, r, pr, start;
  139. cout << "\nEnter the start Variable ";
  140. start='S';
  141. cout << "\nNumber of productions ";
  142. np=2;
  143. ifstream in("start.txt");
  144. for (i, 0, np)
  145. {
  146. in >> a;
  147. pt = a.find("->");
  148. gram[i][0] = a.substr(0, pt);
  149. if (lchomsky(gram[i][0]) == 0)
  150. {
  151. cout << "\nGrammar not in Chomsky Form";
  152. abort();
  153. }
  154. a = a.substr(pt + 2, a.length());
  155. break_gram(a);
  156. for (j, 0, p)
  157. {
  158. gram[i][j + 1] = dpr[j];
  159. if (rchomsky(dpr[j]) == 0)
  160. {
  161. cout << "\nGrammar not in Chomsky Form";
  162. abort();
  163. }
  164. }
  165. }
  166. string st;
  167. cout << "\nEnter string to be checked : ";
  168. str="abaab";
  169. for (i, 0, str.length()) //Assigns values to principal diagonal of matrix
  170. {
  171. r = "";
  172. st = "";
  173. st += str[i];
  174. for (j, 0, np)
  175. {
  176. k = 1;
  177. while (gram[j][k] != "")
  178. {
  179. if (gram[j][k] == st)
  180. {
  181. r = concat(r, gram[j][0]);
  182. }
  183. k++;
  184. }
  185. }
  186. matrix[i][i] = r;
  187. }
  188. int ii, kk;
  189. for (k, 1, str.length()) //Assigns values to upper half of the matrix
  190. {
  191. for (j, k, str.length())
  192. {
  193. r = "";
  194. for (l, j - k, j)
  195. {
  196. pr = gen_comb(matrix[j - k][l], matrix[l + 1][j]);
  197. r = concat(r, pr);
  198. }
  199. matrix[j - k][j] = r;
  200. }
  201. }
  202. cout << endl;
  203. for (i, 0, str.length()) //Prints the matrix
  204. {
  205. k = 0;
  206. l = str.length() - i - 1;
  207. for (j, l, str.length())
  208. {
  209. cout << setw(5) << matrix[k++][j] << " ";
  210. }
  211. cout << endl;
  212. }
  213. int ukaz = 0;
  214. for (i, 0, np) {
  215. for (j, 1, 10) {
  216. if (gram[i][j] != "") {
  217. string buf;
  218. buf += gram[i][0];
  219. buf += gram[i][j];
  220. grambuf[ukaz] += buf;
  221. ukaz += 1;
  222. }
  223. }
  224. }
  225. cout << endl;
  226. for (i, 0, ukaz) {
  227. cout << i<<' '<<grambuf[i] << endl;
  228. }
  229. // gen(int i, int j, char B, int end, int ukaz) end - последняя строка, ее номер, указатель - понятно
  230. cout << endl;
  231. for (i, 0, start.length())
  232. if (matrix[0][str.length() - 1].find(start[i]) <= matrix[0][str.length() - 1].length()) //Checks if last element of first row contains a Start variable
  233. {
  234. cout << "String can be generated\n";
  235. int check = 0;
  236. int i=0;
  237. int j= 4;
  238. int k = 1;
  239. char B = 'S';
  240. cout << gen(i, j, B, str.length()-1, ukaz) << ' ';
  241.  
  242. system("pause");
  243. return 0;
  244. }
  245. system("pause");
  246. cout << "String cannot be generated\n";
  247. return 0;
  248. }
Add Comment
Please, Sign In to add comment