ademosh

CYK done

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