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(2);
  146. memset(c,0,2);
  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. int zrule_name_index;
  166. char * zkey;
  167. int zindex;
  168. void parse_branch(char * co) {
  169. itterations++;
  170. int actual_index_tmp = 0;
  171. if (itterations == 1) {
  172. zrule_name_index = trie_trie->rear->rule_name_index;
  173. zkey = strdup(trie_trie->rear->key);
  174. zindex = trie_trie->rear->index;
  175. rule_index_tmp = rule_index+1;
  176. final_rule = rule_index_tmp;
  177. }
  178. else if (itterations == 2) rule_index_tmp++;
  179. actual_index_tmp = actual_index+1;
  180. printf("zkey = %s\n", zkey);
  181. printf("insert(%s%d, \"%s\", %d, %s%d);\n", trie_rule_prefix, zrule_name_index, zkey, zindex, trie_rule_prefix, rule_index_tmp-(itterations==1?0:1));
  182. trie_queue_add3(&trie_trie, trie_rule_prefix, zrule_name_index, strdup(zkey), zindex, trie_rule_prefix, rule_index_tmp-(itterations==1?0:1));
  183. for (int level = 0; co[level]; level++) {
  184. char tmp[2];
  185. tmp[0] = co[level];
  186. tmp[1] = '\0';
  187. 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);
  188. if (itterations == 1) {
  189. if (co[level+1]) {
  190. puts("RULE");
  191. trie_queue_add(&trie_trie, trie_rule_prefix, rule_index_tmp-1, strdup(tmp), actual_index_tmp, trie_rule_prefix, rule_index_tmp);
  192. } else {
  193. puts("FINAL");
  194. trie_queue_add2(&trie_trie, trie_rule_prefix, rule_index_tmp-1, strdup(tmp), actual_index_tmp, trie_rule_prefix, final_rule);
  195. }
  196. } else {
  197. puts("RULE FINAL");
  198. 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);
  199. }
  200. rule_index_tmp++;
  201. if (itterations == 1 && co[level+1]) final_rule++;
  202. actual_index_tmp++;
  203. }
  204. }
  205. char * co = NULL;
  206. int main(int argc, char **argv) {
  207. /*
  208. def0:
  209. 35:NULL
  210. 85:NULL
  211. rule1 : |1|2|def0|6|
  212. */
  213. char * s = "\
  214. i:\
  215. s1\
  216. s2\
  217. b\
  218. C\
  219. s3\
  220. s5\
  221. C\
  222. s8\
  223. s5\
  224. e\
  225. s6"; // for the sake of simplicity branches contain full strings only, not individual characters
  226. rule_index++;
  227. for(; *s; s++) {
  228. if (*s == 'b') {
  229. puts("branch start");
  230. part_of_branch = true;
  231. itterations = 0;
  232. for(; *s; s++) {
  233. if (*s == 'C') {
  234. s++;
  235. co = getbranchcontents(s);
  236. parse_branch(co);
  237. free(co);
  238. };
  239. if (*s == 'e') break;
  240. }
  241. if (*s == 'e') s--;
  242. continue;
  243. }
  244. if (*s == 'e') {
  245. puts("branch end");
  246. part_of_branch = false;
  247. itterations = 0;
  248. zrule_name_index = 0;
  249. free(zkey);
  250. zkey = NULL;
  251. zindex = 0;
  252. if (final_rule) {
  253. rule_index = final_rule;
  254. update_on_next_pass = true;
  255. final_rule = 0;
  256. }
  257. continue;
  258. }
  259. if (*s == ' ' || *s == '\n' || *s == '\t') continue;
  260. if (*s == 'i') {
  261. passed_first = true;
  262. continue;
  263. }
  264. if (*s == 's' || *s == ':') {
  265. s++;
  266. printf("key: %c\n", *s);
  267. if (part_of_branch) {
  268. }
  269. if (!part_of_branch) {
  270. if (!passed_first) {
  271. if (passed_next) {
  272. if (update_on_next_pass/* && (tmp.type & STR_TYPE_DIGIT || tmp.type & STR_TYPE_BINARY)*/) {
  273. rule_index = rule_index_tmp-1;
  274. update_on_next_pass = false;
  275. }
  276. actual_index = 0;
  277. passed_next = false;
  278. char tmp[2];
  279. tmp[0] = *s;
  280. tmp[1] = '\0';
  281. trie_queue_add(&trie_trie, trie_sub_root, -1, strdup(tmp), actual_index, trie_rule_prefix, rule_index);
  282. }
  283. else {
  284. rule_index++;
  285. actual_index++;
  286. char tmp[2];
  287. tmp[0] = *s;
  288. tmp[1] = '\0';
  289. trie_queue_add(&trie_trie, trie_rule_prefix, rule_index-1, strdup(tmp), actual_index, trie_rule_prefix, rule_index);
  290. }
  291. } else {
  292. passed_first = false;
  293. passed_next = true;
  294. }
  295. }
  296. }
  297. }
  298. // trie_queue_add(&trie_trie, trie_sub_root, -1, NULL, 0, trie_rule_prefix, 0); // root>0
  299. // trie_queue_add(&trie_trie, trie_rule_prefix, 0, NULL, 1, trie_rule_prefix, 1); // 0>1
  300. // trie_queue_add(&trie_trie, trie_rule_prefix, 1, NULL, 1, trie_rule_prefix, 2); // 1>2
  301. // trie_queue_add2(&trie_trie, trie_rule_prefix, 2, NULL, 1, trie_rule_prefix, 3); // 2>3
  302. // trie_queue_add3(&trie_trie, trie_rule_prefix, 0, NULL, 1, trie_rule_prefix, 1); // 0>1
  303. // trie_queue_add(&trie_trie, trie_rule_prefix, 1, NULL, 1, trie_rule_prefix, 2); // 1>2
  304. // trie_queue_add2(&trie_trie, trie_rule_prefix, 2, NULL, 1, trie_rule_prefix, 3); // 2>3
  305. // trie_queue_add3(&trie_trie, trie_rule_prefix, 3, NULL, 1, NULL, -1); // 3>END
  306. struct trie_QNode * node = trie_newNode(NULL,0,NULL,0,NULL,0); // this gets freed anyway
  307. int nodes = 0;
  308. int start_indent = 0;
  309. int i = 0;
  310. for (i = 0; i < start_indent; i++) printf(" ");
  311. printf("struct TrieNode * %s = getNode(); \n", trie_root);
  312. for (i = 0; i < start_indent; i++) printf(" ");
  313. printf("struct TrieNode * %s = getNode(); \n", trie_sub_root);
  314. while (node != NULL) {
  315. // drain the list until empty
  316. if (node->key) free(node->key);
  317. free(node);
  318. node = trie_load_asm(&trie_trie);
  319. if (node == NULL) break;
  320. // pp(node)
  321. // pp(node->rule_name)
  322. // ps(node->rule_name)
  323. // pi(node->rule_name_index)
  324. // pp(node->key)
  325. // ps(node->key)
  326. // pi(node->index)
  327. // pp(node->rule_next)
  328. // ps(node->rule_next)
  329. // pi(node->rule_next_index)
  330. if (node->rule_next && node->rule_next_index != -1) {
  331. bool rule_does_not_exist = false;
  332. bool next_rule_does_not_exist = false;
  333. if (node->rule_name_index == -1) {
  334. // is sub_root
  335. if (rule_does_not_exist) {
  336. for (i = 0; i < start_indent+1; i++) printf(" ");
  337. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_name, node->rule_name_index);
  338. }
  339. if (next_rule_does_not_exist) {
  340. for (i = 0; i < start_indent+1; i++) printf(" ");
  341. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_next, node->rule_next_index);
  342. }
  343. for (i = 0; i < start_indent+1; i++) printf(" ");
  344. printf("insert(%s, \"%s\", %d, %s%d);\n", node->rule_name, node->key, node->index, node->rule_next, node->rule_next_index);
  345. }
  346. else {
  347. // is rule
  348. if (rule_does_not_exist) {
  349. for (i = 0; i < start_indent+2; i++) printf(" ");
  350. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_name, node->rule_name_index);
  351. }
  352. if (next_rule_does_not_exist) {
  353. for (i = 0; i < start_indent+2; i++) printf(" ");
  354. printf("struct TrieNode * %s%d = getNode(); \n", node->rule_next, node->rule_next_index);
  355. }
  356. for (i = 0; i < start_indent+2; i++) printf(" ");
  357. 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);
  358. }
  359. } else {
  360. if (node->rule_name_index == -1) {
  361. // is sub_root
  362. for (i = 0; i < start_indent+1; i++) printf(" ");
  363. printf("insert(%s, \"%s\", %d, NULL);\n", node->rule_name, node->key, node->index);
  364. } else {
  365. // is rule
  366. for (i = 0; i < start_indent+2; i++) printf(" ");
  367. printf("insert(%s%d, \"%s\", %d, NULL);\n", node->rule_name, node->rule_name_index, node->key, node->index);
  368. }
  369. }
  370. nodes++;
  371. }
  372. free(trie_trie);
  373. return 0;
  374. }
  375.