Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1.  
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <ctype.h>
  6. #include "tokenizer.h"
  7.  
  8.  
  9. //-----------------TOKENIZE LOGIC-----------------
  10. ops *
  11. tokenize(char *c) {
  12. ops *op = malloc(sizeof(ops));
  13. check_memory(op, "op", "tokenize");
  14.  
  15. size_t token_len = strnlen(c, MAX_TOKEN_LENGTH);
  16. op->value = malloc(token_len + 1);
  17. strncpy(op->value, c, token_len);
  18.  
  19. //if its a token
  20. size_t len = strnlen(c, MAX_TOKEN_LENGTH);
  21. unsigned int found = 0;
  22.  
  23. for (int i = 0; i < token_co; i++) {
  24. if (tokens[i].len == len) {
  25. if (strcmp(tokens[i].name, c) == 0) {
  26. op->type = tokens[i].type;
  27. op->prec = tokens[i].prec;
  28. op->asoc = tokens[i].asoc;
  29. found = 1;
  30. }
  31. }
  32. }
  33.  
  34. //if its a number
  35. if (!found && is_number(c)) {
  36. op->asoc = -1;
  37. op->prec = NONE_P;
  38. op->type = CONSTANT;
  39. found = 1;
  40. }
  41.  
  42. return found ? op : NULL;
  43.  
  44. }
  45.  
  46. int is_number(const char *str)
  47. {
  48. char *p = str;
  49. while (*p != '\0') {
  50. if ((*p >= '0' && *p <= '9') || *p == '.') {
  51. *p++;
  52. }
  53. else {
  54. return 0;
  55. }
  56. }
  57. return 1;
  58. }
  59.  
  60. int check_memory(void *pt, const char* name, const char* function) {
  61. if (!pt) {
  62. printf("Memory allocation has failed on variable: %s in function: %s\n", name, function);
  63. exit(1);
  64. }
  65. }
  66.  
  67. //-----------------STACK LOGIC-----------------
  68.  
  69. stack *
  70. create_stack(unsigned int size) {
  71. stack *stack = malloc(sizeof(stack));
  72. check_memory(stack, "stack", "create_stack");
  73.  
  74. stack->s = calloc(sizeof(ops*), size);
  75. check_memory(stack->s, "stack->s", "create_stack");
  76.  
  77. stack->count = 0;
  78. stack->size = size;
  79.  
  80. return stack;
  81. }
  82.  
  83. void
  84. s_push(stack *st, ops *o) {
  85. st->s[st->count++] = o;
  86. }
  87.  
  88. ops *
  89. s_pop(stack *st) {
  90. ops *op = st->s[st->count - 1];
  91. st->count--;
  92. st->s[st->count] = NULL;
  93. return op;
  94. }
  95.  
  96. ops *
  97. s_peek(stack *st) {
  98. return st->s[st->count - 1];
  99. }
  100.  
  101. void
  102. free_stack(stack *st) {
  103. for (int i = 0; i < st->count;i++) {
  104. if (st->s[i]) {
  105. //clear ops struct memory
  106. if (st->s[i]->value) {
  107. free(st->s[i]->value);
  108. }
  109. //clear pointer to stack node
  110. free(st->s[i]);
  111. }
  112. }
  113. if (st) {
  114. //clear stack
  115. free(st);
  116. }
  117. }
  118.  
  119.  
  120. //-----------------QUEUE LOGIC-----------------
  121.  
  122. queue *create_queue(unsigned int size) {
  123. queue *que = malloc(sizeof(queue));
  124. check_memory(que, "que", "create_queue");
  125.  
  126. que->count = 0;
  127. que->size = size;
  128. que->s1 = create_stack(size);
  129. que->s2 = create_stack(size);
  130.  
  131. return que;
  132. }
  133. void q_enqueue(queue *q, ops *o) {
  134. while (q->s1->count != 0) {
  135. s_push(q->s2, s_pop(q->s1));
  136. }
  137.  
  138. s_push(q->s1, o);
  139.  
  140. while (q->s2->count != 0) {
  141. s_push(q->s1, s_pop(q->s2));
  142. }
  143. }
  144. ops *q_dequeue(queue *q) {
  145. if (q->s1->count > 0) {
  146. return s_pop(q->s1);
  147. }
  148. else {
  149. return NULL;
  150. }
  151. }
  152. ops *q_peek(queue *q) {
  153. if (q->s1->count > 0) {
  154. return s_peek(q->s1);
  155. }
  156. else {
  157. return NULL;
  158. }
  159. }
  160. void free_queue(queue *q) {
  161. free_stack(q->s1);
  162. free_stack(q->s2);
  163. if (q) {
  164. free(q);
  165. }
  166. }
  167.  
  168. //-----------------TOKENIZER LOGIC-----------------
  169.  
  170. char *next_token(char *exp, char **save) {
  171. char *pt = exp, *tok = NULL;
  172.  
  173. if (exp == NULL) {
  174. pt = *save;
  175. }
  176.  
  177. //ignore space OR column
  178. while (*pt == ' ' || *pt == ',') {
  179. pt++;
  180. }
  181.  
  182. //check for tokens
  183. for (int i = 0; i < token_co; i++) {
  184. if (*pt == tokens[i].name[0]) {
  185. if (tokens[i].len > 1) {
  186. size_t st = 1, en = tokens[i].len - 1;
  187. while (st <= en) {
  188. if (*++pt != tokens[i].name[st++]) {
  189. //ERROR
  190. return NULL;
  191. }
  192. }
  193. tok = malloc(tokens[i].len + 1);
  194. check_memory(tok, "tok", "next_token");
  195.  
  196. strncpy(tok, tokens[i].name, tokens[i].len + 1);
  197. }
  198. //token size is only 1
  199. else {
  200. tok = malloc(2);
  201. check_memory(tok, "tok", "next_token");
  202. memcpy(tok, tokens[i].name, 2);
  203. break;
  204. }
  205. }
  206. }
  207.  
  208. //check for numbers
  209. char buffer[MAX_TOKEN_LENGTH];
  210. memset(buffer, '\0', MAX_TOKEN_LENGTH);
  211. size_t i = 0;
  212.  
  213. while (isdigit(*pt) || *pt == '.') {
  214. buffer[i++] = *pt++;
  215. }
  216.  
  217. if (i > 0) {
  218. tok = malloc(i + 1);
  219. check_memory(tok, "tok", "next_token");
  220. strncpy(tok, buffer, i + 1);
  221. //need to recall one pointer back because of upper while loop
  222. *pt--;
  223. }
  224.  
  225. *save = pt + 1;
  226. return tok;
  227. }
  228. ----------------------------------------------------------------------------------
  229.  
  230. #ifndef _TOKENIZER_H_INCLUDED_
  231. #define _TOKENIZER_H_INCLUDED_
  232.  
  233. //------------TOKENIZER------------
  234.  
  235. #define MAX_TOKEN_LENGTH 20
  236. #define MAX_STACK_SIZE 256
  237.  
  238. typedef enum { L_AS = 1, R_AS = 2, NONE_P = 3 } associativity;
  239. typedef enum { FUNCTION = 1, NUMBER = 2, OPERATOR = 3, CONSTANT = 4, LEFT_P = 5, RIGHT_P = 6 } token_type;
  240.  
  241. typedef struct {
  242. const char *name;
  243. const int len, prec;
  244. const associativity asoc;
  245. const token_type type;
  246. } token;
  247.  
  248. typedef struct {
  249. char *value;
  250. int prec;
  251. associativity asoc;
  252. token_type type;
  253. } ops;
  254.  
  255. static token tokens[] = {
  256. { "+", 1, 2, L_AS, OPERATOR },
  257. { "-", 1, 2, L_AS, OPERATOR },
  258. { "/", 1, 3, L_AS, OPERATOR },
  259. { "*", 1, 3, L_AS, OPERATOR },
  260. { "^", 1, 4, R_AS, OPERATOR },
  261. { "sin", 3, -1, NONE_P, FUNCTION },
  262. { "cos", 3, -1, NONE_P, FUNCTION },
  263. { "tan", 3, -1, NONE_P, FUNCTION },
  264. { "max", 3, -1, NONE_P, FUNCTION },
  265. { "(", 1, -1, NONE_P, LEFT_P },
  266. { ")", 1, -1, NONE_P, RIGHT_P },
  267. { "x", 1, -1, NONE_P, CONSTANT },
  268. { "e", 1, -1, NONE_P, CONSTANT },
  269. { "pi", 2, -1, NONE_P, CONSTANT },
  270. { "log", 3, -1, NONE_P, FUNCTION },
  271. { "ln", 2, -1, NONE_P, FUNCTION }
  272. };
  273.  
  274. static unsigned int token_co = sizeof(tokens) / sizeof(tokens[0]);
  275.  
  276. char *next_token(const char *exp, char **save);
  277. ops *tokenize(char *c);
  278. int is_number(const char *str);
  279. int check_memory(void *pt, const char* name, const char* function);
  280.  
  281. //------------STACK------------
  282. typedef struct {
  283. ops **s;
  284. unsigned int size;
  285. unsigned int count;
  286. } stack;
  287.  
  288. stack *create_stack(unsigned int size);
  289. void s_push(stack *st, ops *o);
  290. ops *s_pop(stack *st);
  291. ops *s_peek(stack *st);
  292. void free_stack(stack *st);
  293.  
  294. //------------QUEUE------------
  295.  
  296. typedef struct {
  297. stack *s1, *s2;
  298. unsigned int size;
  299. unsigned int count;
  300. } queue;
  301.  
  302. queue *create_queue(unsigned int size);
  303. void q_enqueue(queue *q, ops *o);
  304. ops *q_dequeue(queue *q);
  305. ops *q_peek(queue *q);
  306. void free_queue(queue *q);
  307.  
  308.  
  309.  
  310.  
  311. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement