spacepaste

  1.  
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. // A C program to demonstrate linked list based implementation of queue
  7. // A linked list (LL) node to store a queue entry
  8. struct trie_QNode
  9. {
  10. char * rule_name;
  11. int rule_name_index;
  12. char * key;
  13. int index;
  14. char * rule_next;
  15. int rule_next_index;
  16. struct trie_QNode *next;
  17. };
  18. // The queue, front stores the front node of LL and rear stores ths
  19. // last node of LL
  20. struct trie_Queue
  21. {
  22. struct trie_QNode *front, *rear;
  23. };
  24. // A utility function to create a new linked list node.
  25. struct trie_QNode* trie_newNode(char * a, int b, char * c, int d, char * e, int f)
  26. {
  27. struct trie_QNode *temp = (struct trie_QNode*)malloc(sizeof(struct trie_QNode));
  28. temp->rule_name = a;
  29. temp->rule_name_index = b;
  30. temp->key = c;
  31. temp->index = d;
  32. temp->rule_next = e;
  33. temp->rule_next_index = f;
  34. temp->next = NULL;
  35. return temp;
  36. }
  37. // A utility function to create an empty queue
  38. struct trie_Queue *trie_createQueue()
  39. {
  40. struct trie_Queue *q = (struct trie_Queue*)malloc(sizeof(struct trie_Queue));
  41. q->front = q->rear = NULL;
  42. return q;
  43. }
  44. void trie_store_asm(struct trie_Queue *q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index)
  45. {
  46. // Create a new LL node
  47. struct trie_QNode *temp = trie_newNode(rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  48. // If queue is empty, then new node is front and rear both
  49. if (q->rear == NULL)
  50. {
  51. q->front = q->rear = temp;
  52. q->rear->rule_next = NULL;
  53. q->rear->rule_next_index = -1;
  54. return;
  55. }
  56. // Add the new node at the end of queue and change rear
  57. if (q->rear) {
  58. q->rear->rule_next = rule_name;
  59. q->rear->rule_next_index = rule_name_index;
  60. }
  61. q->rear->next = temp;
  62. q->rear = temp;
  63. if (q->rear) {
  64. q->rear->rule_next = NULL;
  65. q->rear->rule_next_index = -1;
  66. }
  67. }
  68. void trie_store_asm2(struct trie_Queue *q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index)
  69. {
  70. // Create a new LL node
  71. struct trie_QNode *temp = trie_newNode(rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  72. // If queue is empty, then new node is front and rear both
  73. if (q->rear == NULL)
  74. {
  75. q->front = q->rear = temp;
  76. return;
  77. }
  78. // Add the new node at the end of queue and change rear
  79. if (q->rear) {
  80. q->rear->rule_next = rule_name;
  81. q->rear->rule_next_index = rule_name_index;
  82. }
  83. q->rear->next = temp;
  84. q->rear = temp;
  85. }
  86. void trie_store_asm3(struct trie_Queue *q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index)
  87. {
  88. // Create a new LL node
  89. struct trie_QNode *temp = trie_newNode(rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  90. // If queue is empty, then new node is front and rear both
  91. if (q->rear == NULL)
  92. {
  93. q->front = q->rear = temp;
  94. return;
  95. }
  96. // Add the new node at the end of queue and change rear
  97. q->rear->next = temp;
  98. q->rear = temp;
  99. }
  100. struct trie_QNode * trie_load_asm(struct trie_Queue **q)
  101. {
  102. // If queue is empty, return NULL.
  103. if ((q) == NULL) return NULL;
  104. if ((*q) == NULL) return NULL;
  105. if ((*q)->front == NULL)
  106. return NULL;
  107. // Store previous front and move front one node ahead
  108. struct trie_QNode *temp = (*q)->front;
  109. (*q)->front = (*q)->front->next;
  110. // If front becomes NULL, then change rear also as NULL
  111. if ((*q)->front == NULL)
  112. (*q)->rear = NULL;
  113. return temp;
  114. }
  115. int trie_queue_add(struct trie_Queue **q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index) {
  116. if (!(*q)) (*q) = trie_createQueue();
  117. trie_store_asm((*q), rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  118. return 0;
  119. }
  120. int trie_queue_add2(struct trie_Queue **q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index) {
  121. if (!(*q)) (*q) = trie_createQueue();
  122. trie_store_asm2((*q), rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  123. return 0;
  124. }
  125. int trie_queue_add3(struct trie_Queue **q, char * rule_name, int rule_name_index, char * key, int index, char * rule_next, int rule_next_index) {
  126. if (!(*q)) (*q) = trie_createQueue();
  127. trie_store_asm3((*q), rule_name,rule_name_index,key,index,rule_next,rule_next_index);
  128. return 0;
  129. }
  130. char * trie_root = "root";
  131. char * trie_sub_root = "sub_root";
  132. char * trie_rule_prefix = "rule_";
  133. struct trie_Queue * trie_trie = NULL;
  134. int rule_index = -1;
  135. int actual_index = 0;
  136. bool passed_first = false;
  137. bool passed_next = false;
  138. int update_on_next_pass = false;
  139. int final_rule = 0;
  140. int rule_index_tmp = 0;
  141. int actual_index_tmp = 0;
  142. bool part_of_branch = false;
  143. int itterations = 0;
  144. char * getbranchcontents(char * s) {
  145. char * c = malloc(500);
  146. memset(c,0,500);
  147. int ci = 0;
  148. for(; *s; s++) {
  149. if (*s == 'b') {
  150. continue;
  151. }
  152. if (*s == 'e' || *s == 'C') {
  153. break;
  154. }
  155. if (*s == ' ' || *s == '\n' || *s == '\t') continue;
  156. if (*s == 's') {
  157. s++;
  158. c[ci] = *s;
  159. ci++;
  160. }
  161. }
  162. printf("c = %s\n", c);
  163. return c;
  164. }
  165. parse_branch(char * co) {
  166. itterations++;
  167. int actual_index_tmp = 0;
  168. char * zrule_name;
  169. int zrule_name_index;
  170. char * zkey;
  171. int zindex;
  172. if (itterations == 1) {
  173. zrule_name = trie_trie->rear->rule_name;
  174. zrule_name_index = trie_trie->rear->rule_name_index;
  175. zkey = trie_trie->rear->key;
  176. zindex = trie_trie->rear->index;
  177. rule_index_tmp = rule_index+1;
  178. final_rule = rule_index_tmp;
  179. }
  180. else if (itterations == 2) rule_index_tmp++;
  181. actual_index_tmp = actual_index+1;
  182. printf("zrule_name = %s\n", zrule_name);
  183. printf("zkey = %s\n", zkey);
  184. printf("insert(%s%d, \"%s\", %d, %s%d);\n", zrule_name, zrule_name_index, zkey, zindex, trie_rule_prefix, rule_index_tmp-(itterations==1?0:1));
  185. trie_queue_add3(&trie_trie, zrule_name, zrule_name_index, strdup(zkey), zindex, trie_rule_prefix, rule_index_tmp-(itterations==1?0:1));
  186. for (int level = 0; co[level]; level++) {
  187. char tmp[2];
  188. tmp[0] = co[level];
  189. tmp[1] = '\0';
  190. printf("insert(%s%d, \"%s\", %d, %s%d);\n", trie_rule_prefix, rule_index_tmp-1, tmp, actual_index_tmp, trie_rule_prefix, co[level+1]?rule_index_tmp:final_rule);
  191. if (itterations == 1) {
  192. if (co[level+1]) {
  193. puts("RULE");
  194. trie_queue_add(&trie_trie, trie_rule_prefix, rule_index_tmp-1, strdup(tmp), actual_index_tmp, trie_rule_prefix, rule_index_tmp);
  195. } else {
  196. puts("FINAL");
  197. trie_queue_add2(&trie_trie, trie_rule_prefix, rule_index_tmp-1, strdup(tmp), actual_index_tmp, trie_rule_prefix, final_rule);
  198. }
  199. } else {
  200. puts("RULE FINAL");
  201. trie_queue_add3(&trie_trie, trie_rule_prefix, rule_index_tmp-1, strdup(tmp), actual_index_tmp, trie_rule_prefix, co[level+1]?rule_index_tmp:final_rule);
  202. }
  203. rule_index_tmp++;
  204. if (itterations == 1 && co[level+1]) final_rule++;
  205. actual_index_tmp++;
  206. }
  207. }
  208. char * co = NULL;
  209. int main(int argc, char **argv) {
  210. /*
  211. def0:
  212. 35:NULL
  213. 85:NULL
  214. rule1 : |1|2|def0|6|
  215. */
  216. char * s = "\
  217. i:\
  218. s1\
  219. s2\
  220. b\
  221. C\
  222. s3\
  223. s5\
  224. C\
  225. s8\
  226. s5\
  227. e\
  228. s6"; // for the sake of simplicity branches contain full strings only, not individual characters
  229. rule_index++;
  230. for(; *s; s++) {
  231. if (*s == 'b') {
  232. puts("branch start");
  233. part_of_branch = true;
  234. itterations = 0;
  235. for(; *s; s++) {
  236. if (*s == 'C') {
  237. s++;
  238. co = getbranchcontents(s);
  239. parse_branch(co);
  240. free(co);
  241. };
  242. if (*s == 'e') break;
  243. }
  244. if (*s == 'e') s--;
  245. continue;
  246. }
  247. if (*s == 'e') {
  248. puts("branch end");
  249. part_of_branch = false;
  250. itterations = 0;
  251. if (final_rule) {
  252. rule_index = final_rule;
  253. update_on_next_pass = true;
  254. final_rule = 0;
  255. }
  256. continue;
  257. }
  258. if (*s == ' ' || *s == '\n' || *s == '\t') continue;
  259. if (*s == 'i') {
  260. passed_first = true;
  261. continue;
  262. }
  263. if (*s == 's' || *s == ':') {
  264. s++;
  265. printf("key: %c\n", *s);
  266. if (part_of_branch) {
  267. }
  268. if (!part_of_branch) {
  269. if (!passed_first) {
  270. if (passed_next) {
  271. if (update_on_next_pass/* && (tmp.type & STR_TYPE_DIGIT || tmp.type & STR_TYPE_BINARY)*/) {
  272. rule_index = rule_index_tmp-1;
  273. update_on_next_pass = false;
  274. }
  275. actual_index = 0;
  276. passed_next = false;
  277. char tmp[2];
  278. tmp[0] = *s;
  279. tmp[1] = '\0';
  280. trie_queue_add(&trie_trie, trie_sub_root, -1, strdup(tmp), actual_index, trie_rule_prefix, rule_index);
  281. }
  282. else {
  283. rule_index++;
  284. actual_index++;
  285. char tmp[2];
  286. tmp[0] = *s;
  287. tmp[1] = '\0';
  288. trie_queue_add(&trie_trie, trie_rule_prefix, rule_index-1, strdup(tmp), actual_index, trie_rule_prefix, rule_index);
  289. }
  290. } else {
  291. passed_first = false;
  292. passed_next = true;
  293. }
  294. }
  295. }
  296. }
  297. // trie_queue_add(&trie_trie, trie_sub_root, -1, NULL, 0, trie_rule_prefix, 0); // root>0
  298. // trie_queue_add(&trie_trie, trie_rule_prefix, 0, NULL, 1, trie_rule_prefix, 1); // 0>1
  299. // trie_queue_add(&trie_trie, trie_rule_prefix, 1, NULL, 1, trie_rule_prefix, 2); // 1>2
  300. // trie_queue_add2(&trie_trie, trie_rule_prefix, 2, NULL, 1, trie_rule_prefix, 3); // 2>3
  301. // trie_queue_add3(&trie_trie, trie_rule_prefix, 0, NULL, 1, trie_rule_prefix, 1); // 0>1
  302. // trie_queue_add(&trie_trie, trie_rule_prefix, 1, NULL, 1, trie_rule_prefix, 2); // 1>2
  303. // trie_queue_add2(&trie_trie, trie_rule_prefix, 2, NULL, 1, trie_rule_prefix, 3); // 2>3
  304. // trie_queue_add3(&trie_trie, trie_rule_prefix, 3, NULL, 1, NULL, -1); // 3>END
  305. struct trie_QNode * node = trie_newNode(NULL,0,NULL,0,NULL,0); // this gets freed anyway
  306. int nodes = 0;
  307. int start_indent = 0;
  308. int i = 0;
  309. for (i = 0; i < start_indent; i++) printf(" ");
  310. printf("struct TrieNode * %s = getNode(); \n", trie_root);
  311. for (i = 0; i < start_indent; i++) printf(" ");
  312. printf("struct TrieNode * %s = getNode(); \n", trie_sub_root);
  313. while (node != NULL) {
  314. // drain the list until empty
  315. if (node->key) free(node->key);
  316. free(node);
  317. node = trie_load_asm(&trie_trie);
  318. if (node == NULL) break;
  319. // pp(node)
  320. // pp(node->rule_name)
  321. // ps(node->rule_name)
  322. // pi(node->rule_name_index)
  323. // pp(node->key)
  324. // ps(node->key)
  325. // pi(node->index)
  326. // pp(node->rule_next)
  327. // ps(node->rule_next)
  328. // pi(node->rule_next_index)
  329. if (node->rule_next && node->rule_next_index != -1) {
  330. bool rule_does_not_exist = false;
  331. bool next_rule_does_not_exist = false;
  332. if (node->rule_name_index == -1) {
  333. // is sub_root
  334. if (rule_does_not_exist) {
  335. for (i = 0; i < start_indent+1; i++) printf(" ");
  336. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_name, node->rule_name_index);
  337. }
  338. if (next_rule_does_not_exist) {
  339. for (i = 0; i < start_indent+1; i++) printf(" ");
  340. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_next, node->rule_next_index);
  341. }
  342. for (i = 0; i < start_indent+1; i++) printf(" ");
  343. printf("insert(%s, \"%s\", %d, %s%d);\n", node->rule_name, node->key, node->index, node->rule_next, node->rule_next_index);
  344. }
  345. else {
  346. // is rule
  347. if (rule_does_not_exist) {
  348. for (i = 0; i < start_indent+2; i++) printf(" ");
  349. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_name, node->rule_name_index);
  350. }
  351. if (next_rule_does_not_exist) {
  352. for (i = 0; i < start_indent+2; i++) printf(" ");
  353. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_next, node->rule_next_index);
  354. }
  355. for (i = 0; i < start_indent+2; i++) printf(" ");
  356. printf("insert(%s%d, \"%s\", %d, %s%d);\n", node->rule_name, node->rule_name_index, node->key, node->index, node->rule_next, node->rule_next_index);
  357. }
  358. } else {
  359. if (node->rule_name_index == -1) {
  360. // is sub_root
  361. for (i = 0; i < start_indent+1; i++) printf(" ");
  362. printf("insert(%s, \"%s\", %d, NULL);\n", node->rule_name, node->key, node->index);
  363. } else {
  364. // is rule
  365. for (i = 0; i < start_indent+2; i++) printf(" ");
  366. printf("insert(%s%d, \"%s\", %d, NULL);\n", node->rule_name, node->rule_name_index, node->key, node->index);
  367. }
  368. }
  369. nodes++;
  370. }
  371. return 0;
  372. }
  373.