ademosh

CYK уже лучше

Nov 19th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 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, 10) {
  114. cout << endl <<"P is "<< p << endl;
  115. for (z, 0, ukaz) {
  116. if ((grambuf[z][0] == B) &&((matrix[i][p].find(grambuf[z][1]) <= matrix[i][p].length())) &&((matrix[i + p][j - p].find(grambuf[z][2])) <= matrix[i + p][j - p].length())) {
  117. cout << z << " is parser number";
  118. cout << endl;
  119. cout << endl << i << ' ' << j << ' ' << B << endl << "Next step" << endl;
  120. gen(i, 0, grambuf[z][1], end, ukaz);
  121. gen(i+1 , j-1 , grambuf[z][2], end, ukaz);
  122. check = 1;
  123. break;
  124.  
  125. }
  126. }
  127. if (check == 1) break;
  128. }
  129. return -1;
  130. }
  131. }
  132.  
  133. int main()
  134. {
  135. int i, pt, j, l, k;
  136. string a, str, r, pr, start;
  137. cout << "\nEnter the start Variable ";
  138. start='S';
  139. cout << "\nNumber of productions ";
  140. np=2;
  141. ifstream in("start.txt");
  142. for (i, 0, np)
  143. {
  144. in >> a;
  145. pt = a.find("->");
  146. gram[i][0] = a.substr(0, pt);
  147. if (lchomsky(gram[i][0]) == 0)
  148. {
  149. cout << "\nGrammar not in Chomsky Form";
  150. abort();
  151. }
  152. a = a.substr(pt + 2, a.length());
  153. break_gram(a);
  154. for (j, 0, p)
  155. {
  156. gram[i][j + 1] = dpr[j];
  157. if (rchomsky(dpr[j]) == 0)
  158. {
  159. cout << "\nGrammar not in Chomsky Form";
  160. abort();
  161. }
  162. }
  163. }
  164. string st;
  165. cout << "\nEnter string to be checked : ";
  166. str="abaab";
  167. for (i, 0, str.length()) //Assigns values to principal diagonal of matrix
  168. {
  169. r = "";
  170. st = "";
  171. st += str[i];
  172. for (j, 0, np)
  173. {
  174. k = 1;
  175. while (gram[j][k] != "")
  176. {
  177. if (gram[j][k] == st)
  178. {
  179. r = concat(r, gram[j][0]);
  180. }
  181. k++;
  182. }
  183. }
  184. matrix[i][i] = r;
  185. }
  186. int ii, kk;
  187. for (k, 1, str.length()) //Assigns values to upper half of the matrix
  188. {
  189. for (j, k, str.length())
  190. {
  191. r = "";
  192. for (l, j - k, j)
  193. {
  194. pr = gen_comb(matrix[j - k][l], matrix[l + 1][j]);
  195. r = concat(r, pr);
  196. }
  197. matrix[j - k][j] = r;
  198. }
  199. }
  200. cout << endl;
  201. for (i, 0, str.length()) //Prints the matrix
  202. {
  203. k = 0;
  204. l = str.length() - i - 1;
  205. for (j, l, str.length())
  206. {
  207. cout << setw(5) << matrix[k++][j] << " ";
  208. }
  209. cout << endl;
  210. }
  211. int f;
  212. for (i, 0, 10) { //исправляем пирамидчатое заполнение
  213. for (j, 0, 10) if ((matrix[i][j]==""))
  214. {
  215. for (f, 1, 11) if (matrix[i][j + f] != "") {
  216. matrix[i][j] = matrix[i][j + f];
  217. matrix[i][j + f] = "";
  218. break;
  219. }
  220. }
  221. }
  222. /*for (i, 0, 10) { //вывод с кодами
  223. for (j, 0, 10) if(matrix[i][j]!="") cout << "i="<<i << setw(1)<<j << setw(1)<<matrix[i][j] << setw(5);
  224. cout << endl;
  225. } */
  226.  
  227. int ukaz = 0;
  228. for (i, 0, np) {
  229. for (j, 1, 10) {
  230. if (gram[i][j] != "") {
  231. string buf;
  232. buf += gram[i][0];
  233. buf += gram[i][j];
  234. grambuf[ukaz] += buf;
  235. ukaz += 1;
  236. }
  237. }
  238. }
  239. cout << endl;
  240. for (i, 0, ukaz) {
  241. cout << i<<' '<<grambuf[i] << endl;
  242. }
  243. // gen(int i, int j, char B, int end, int ukaz) end - последняя строка, ее номер, указатель - понятно
  244. cout << endl;
  245. for (i, 0, start.length())
  246. 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
  247. {
  248. cout << "String can be generated\n";
  249. int check = 0;
  250. int i=0;
  251. int j= 4;
  252. int k = 1;
  253. char B = 'S';
  254. cout << gen(i, j, B, str.length()-1, ukaz) << ' ';
  255.  
  256. system("pause");
  257. return 0;
  258. }
  259. system("pause");
  260. cout << "String cannot be generated\n";
  261. return 0;
  262. }
Add Comment
Please, Sign In to add comment