parse_y.y 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. %{
  2. #ifdef HAVE_CONFIG_H
  3. # include <config.h>
  4. #endif
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #define MAXLINE 1000
  9. #define INDENT_STRING " "
  10. #define PAPER_WIDTH 74
  11. int indent=0;
  12. int line=1;
  13. char *last_label=NULL;
  14. extern int yylex(void);
  15. extern char *yytext;
  16. extern void yyerror(const char *x);
  17. extern char *get_label(const char *label);
  18. extern void set_label(const char *label, const char *target);
  19. char *new_counter(const char *key);
  20. %}
  21. %union {
  22. int def;
  23. char *string;
  24. }
  25. %token NEW_COUNTER LABEL HASH CHAR NEWLINE NO_INDENT RIGHT
  26. %type <string> stuff text
  27. %start doc
  28. %%
  29. doc:
  30. | doc NEWLINE {
  31. printf("\n");
  32. ++line;
  33. }
  34. | doc stuff NEWLINE {
  35. if (strlen($2) > (PAPER_WIDTH-(indent ? strlen(INDENT_STRING):0))) {
  36. yyerror("line too long");
  37. }
  38. printf("%s%s\n", indent ? INDENT_STRING:"", $2);
  39. free($2);
  40. indent = 1;
  41. ++line;
  42. }
  43. | doc stuff RIGHT stuff NEWLINE {
  44. char fixed[PAPER_WIDTH+1];
  45. int len;
  46. len = PAPER_WIDTH-(strlen($2)+strlen($4));
  47. if (len >= 0) {
  48. memset(fixed, ' ', len);
  49. fixed[len] = '\0';
  50. } else {
  51. yyerror("line too wide");
  52. fixed[0] = '\0';
  53. }
  54. printf("%s%s%s\n", $2, fixed, $4);
  55. free($2);
  56. free($4);
  57. indent = 1;
  58. ++line;
  59. }
  60. | doc stuff RIGHT stuff RIGHT stuff NEWLINE {
  61. char fixed[PAPER_WIDTH+1];
  62. int len, l;
  63. len = PAPER_WIDTH-(strlen($2)+strlen($4));
  64. if (len < 0) {
  65. len = 0;
  66. yyerror("line too wide");
  67. }
  68. l = len/2;
  69. memset(fixed, ' ', l);
  70. fixed[l] = '\0';
  71. printf("%s%s%s", $2, fixed, $4);
  72. free($2);
  73. free($4);
  74. l = (len+1)/2;
  75. memset(fixed, ' ', l);
  76. fixed[l] = '\0';
  77. printf("%s%s\n", fixed, $6);
  78. free($6);
  79. indent = 1;
  80. ++line;
  81. }
  82. | doc stuff RIGHT stuff RIGHT stuff NEWLINE {
  83. char fixed[PAPER_WIDTH+1];
  84. int len, l;
  85. len = PAPER_WIDTH-(strlen($2)+strlen($4));
  86. if (len < 0) {
  87. len = 0;
  88. yyerror("line too wide");
  89. }
  90. l = len/2;
  91. memset(fixed, ' ', l);
  92. fixed[l] = '\0';
  93. printf("%s%s%s", $2, fixed, $4);
  94. free($2);
  95. free($4);
  96. l = (len+1)/2;
  97. memset(fixed, ' ', l);
  98. fixed[l] = '\0';
  99. printf("%s%s\n", fixed, $6);
  100. free($6);
  101. indent = 1;
  102. ++line;
  103. }
  104. ;
  105. stuff: {
  106. $$ = strdup("");
  107. }
  108. | stuff text {
  109. $$ = malloc(strlen($1)+strlen($2)+1);
  110. sprintf($$,"%s%s", $1, $2);
  111. free($1);
  112. free($2);
  113. }
  114. ;
  115. text: CHAR {
  116. $$ = strdup(yytext);
  117. }
  118. | text CHAR {
  119. $$ = malloc(strlen($1)+2);
  120. sprintf($$,"%s%s", $1, yytext);
  121. free($1);
  122. }
  123. | NO_INDENT {
  124. $$ = strdup("");
  125. indent = 0;
  126. }
  127. | HASH {
  128. $$ = strdup("#");
  129. }
  130. | LABEL {
  131. if (($$ = get_label(yytext)) == NULL) {
  132. set_label(yytext, last_label);
  133. $$ = strdup("");
  134. }
  135. }
  136. | NEW_COUNTER {
  137. $$ = new_counter(yytext);
  138. }
  139. ;
  140. %%
  141. typedef struct node_s {
  142. struct node_s *left, *right;
  143. const char *key;
  144. char *value;
  145. } *node_t;
  146. node_t label_root = NULL;
  147. node_t counter_root = NULL;
  148. static const char *find_key(node_t root, const char *key)
  149. {
  150. while (root) {
  151. int cmp = strcmp(key, root->key);
  152. if (cmp > 0) {
  153. root = root->right;
  154. } else if (cmp) {
  155. root = root->left;
  156. } else {
  157. return root->value;
  158. }
  159. }
  160. return NULL;
  161. }
  162. static node_t set_key(node_t root, const char *key, const char *value)
  163. {
  164. if (root) {
  165. int cmp = strcmp(key, root->key);
  166. if (cmp > 0) {
  167. root->right = set_key(root->right, key, value);
  168. } else if (cmp) {
  169. root->left = set_key(root->left, key, value);
  170. } else {
  171. free(root->value);
  172. root->value = strdup(value);
  173. }
  174. } else {
  175. root = malloc(sizeof(struct node_s));
  176. root->right = root->left = NULL;
  177. root->key = strdup(key);
  178. root->value = strdup(value);
  179. }
  180. return root;
  181. }
  182. void yyerror(const char *x)
  183. {
  184. fprintf(stderr, "line %d: %s\n", line, x);
  185. }
  186. char *get_label(const char *label)
  187. {
  188. const char *found = find_key(label_root, label);
  189. if (found) {
  190. return strdup(found);
  191. }
  192. return NULL;
  193. }
  194. void set_label(const char *label, const char *target)
  195. {
  196. if (target == NULL) {
  197. yyerror("no hanging value for label");
  198. target = "<??" ">"; /* avoid trigraph warning */
  199. }
  200. label_root = set_key(label_root, label, target);
  201. }
  202. char *new_counter(const char *key)
  203. {
  204. int i=0, j, ndollars = 0;
  205. const char *old;
  206. char *new;
  207. if (key[i++] != '#') {
  208. yyerror("bad index");
  209. return strdup("<???" ">"); /* avoid trigraph warning */
  210. }
  211. while (key[i] == '$') {
  212. ++ndollars;
  213. ++i;
  214. }
  215. key += i;
  216. old = find_key(counter_root, key);
  217. new = malloc(20*ndollars);
  218. if (old) {
  219. for (j=0; ndollars > 1 && old[j]; ) {
  220. if (old[j++] == '.' && --ndollars <= 0) {
  221. break;
  222. }
  223. }
  224. if (j) {
  225. strncpy(new, old, j);
  226. }
  227. if (old[j]) {
  228. i = atoi(old+j);
  229. } else {
  230. new[j++] = '.';
  231. i = 0;
  232. }
  233. } else {
  234. j=0;
  235. while (--ndollars > 0) {
  236. new[j++] = '0';
  237. new[j++] = '.';
  238. }
  239. i = 0;
  240. }
  241. new[j] = '\0';
  242. sprintf(new+j, "%d", ++i);
  243. counter_root = set_key(counter_root, key, new);
  244. if (last_label) {
  245. free(last_label);
  246. }
  247. last_label = strdup(new);
  248. return new;
  249. }
  250. int
  251. main(void)
  252. {
  253. return yyparse();
  254. }