Advertisement
ZOOOO

Untitled

Jan 27th, 2016
4,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. //лексический анализ
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Node {
  7. int k;
  8. int v;
  9. int balance;
  10. struct Node *parent, *left, *right;
  11. };
  12. typedef struct Node Node;
  13.  
  14. Node *replaceNode(Node *t, Node *x, Node *y) {
  15. if (x == t) {
  16. t = y;
  17. if (y)
  18. y->parent = NULL;
  19. } else {
  20. Node *p = x->parent;
  21. if (y)
  22. y->parent = p;
  23. if (p->left == x)
  24. p->left = y;
  25. else
  26. p->right = y;
  27. }
  28. return t;
  29. }
  30.  
  31. Node *rotateLeft(Node *t, Node *x) {
  32. Node *y = x->right;
  33. replaceNode(t, x, y);
  34. Node *b = y->left;
  35. if (b)
  36. b->parent = x;
  37. x->right = b;
  38. x->parent = y;
  39. y->left = x;
  40. x->balance--;
  41. if (y->balance > 0)
  42. x->balance -= y->balance;
  43. y->balance--;
  44. if (x->balance < 0)
  45. y->balance += x->balance;
  46. return t;
  47. }
  48.  
  49. Node *rotateRight(Node *t, Node *x) {
  50. Node *y = x->left;
  51. replaceNode(t, x, y);
  52. Node *b = y->right;
  53. if (b)
  54. b->parent = x;
  55. x->left = b;
  56. x->parent = y;
  57. y->right = x;
  58. x->balance++;
  59. if (y->balance < 0)
  60. x->balance += y->balance;
  61. y->balance++;
  62. if (x->balance > 0)
  63. y->balance -= x->balance;
  64. return t;
  65. }
  66.  
  67. Node *insert(Node *t, int k, int v) {
  68. Node *y = calloc(1, sizeof(Node));
  69. y->k = k;
  70. y->v = v;
  71. if (t == NULL) {
  72. t = y;
  73. } else {
  74. Node *x = t;
  75. while (1) {
  76. if (k < x->k) {
  77. if (!x->left) {
  78. x->left = y;
  79. y->parent = x;
  80. break;
  81. }
  82. x = x->left;
  83. } else {
  84. if (!x->right) {
  85. x->right = y;
  86. y->parent = x;
  87. break;
  88. }
  89. x = x->right;
  90. }
  91. }
  92. }
  93. return t;
  94. }
  95.  
  96. Node *insertAVL(Node *t, int k, int v) {
  97. Node *a = insert(t, k, v);
  98. while (1) {
  99. Node *x = a->parent;
  100. if (!x)
  101. break;
  102. if (a == x->left) {
  103. x->balance--;
  104. if (!x->balance)
  105. break;
  106. if (x->balance == -2) {
  107. if (a->balance == 1)
  108. rotateRight(t, a);
  109. rotateLeft(t, x);
  110. break;
  111. }
  112. } else {
  113. x->balance++;
  114. if (!x->balance)
  115. break;
  116. if (x->balance == 2) {
  117. if (a->balance == -1)
  118. rotateLeft(t, a);
  119. rotateRight(t, x);
  120. break;
  121. }
  122. }
  123. a = x;
  124. }
  125. return a;
  126. }
  127.  
  128. int getAVL(Node *t, int k) {
  129. Node *x = t;
  130. while (x && x->k != k)
  131. if (k < x->k)
  132. x = x->left;
  133. else
  134. x = x->right;
  135. if (x)
  136. return x->v;
  137. return -1;
  138. }
  139.  
  140. void freeAVL(Node *t) {
  141. if (!t)
  142. return;
  143. freeAVL(t->left);
  144. freeAVL(t->right);
  145. free(t);
  146. }
  147.  
  148. enum tok_type {
  149. TOK, NUM, VAR, SPC
  150. };
  151. typedef enum tok_type tok_type;
  152.  
  153. tok_type getType(char c) {
  154. if ('0' <= c && c <= '9')
  155. return NUM;
  156. if ('a' <= c && c <= 'z')
  157. return VAR;
  158. if ('A' <= c && c <= 'Z')
  159. return VAR;
  160. if(c == ' ')
  161. return SPC;
  162. return TOK;
  163. }
  164.  
  165. int getSpec(char c) {
  166. switch (c) {
  167. case '+':
  168. return 0;
  169. case '-':
  170. return 1;
  171. case '*':
  172. return 2;
  173. case '/':
  174. return 3;
  175. case '(':
  176. return 4;
  177. case ')':
  178. return 5;
  179. }
  180. return 0;
  181. }
  182.  
  183. int main(void) {
  184. int n;
  185. scanf("%d", &n);
  186. char str[n + 1];
  187. str[n] = '\0';
  188. fgetc(stdin);
  189. fgets(str, n + 1, stdin);
  190. Node *t = NULL;
  191. char *tok = str;
  192. int id = 0;
  193. while (*tok) {
  194. switch (getType(*tok)) {
  195. case SPC:
  196. while(getType(*tok) == SPC) tok++;
  197. break;
  198. case TOK:
  199. printf("SPEC ");
  200. printf("%d", getSpec(*tok));
  201. printf("\n");
  202. tok++;
  203. break;
  204. case NUM:
  205. printf("CONST ");
  206. while (getType(*tok) == NUM)
  207. printf("%c", *tok++);
  208. printf("\n");
  209. break;
  210. case VAR:
  211. printf("IDENT ");
  212. unsigned int tmp = 5381;
  213. while (getType(*tok) != TOK && getType(*tok) != SPC) {
  214. tmp = ((tmp << 5) + tmp) + *tok++;
  215. }
  216. if (!t) {
  217. printf("%d\n", id);
  218. t = insertAVL(t, tmp, id++);
  219. } else {
  220. int tid = getAVL(t, tmp);
  221. if (tid == -1) {
  222. printf("%d\n", id);
  223. t = insertAVL(t, tmp, id++);
  224. } else
  225. printf("%d\n", tid);
  226. }
  227. break;
  228. }
  229. }
  230. freeAVL(t);
  231. return 0;
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement