Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. %{
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. #define NUMB 256
  6. #define CONS 257
  7. #define APPEND 258
  8. #define DROP 259
  9. #define TAKE 260
  10. int yylval;
  11. %}
  12.  
  13. %%
  14.  
  15. [+,()\[\]\n] { return *yytext; }
  16.  
  17. [0-9]+ { yylval = atoi(yytext);
  18. return NUMB; }
  19.  
  20. "take" { return TAKE; }
  21. "@" { return APPEND; }
  22. "drop" { return DROP; }
  23. "::" { return CONS; }
  24.  
  25. %%
  26. ==============================================
  27.  
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #define NUMB 256
  32. #define CONS 257
  33. #define APPEND 258
  34. #define DROP 259
  35. #define TAKE 260
  36. extern int yylval;
  37.  
  38.  
  39. int symbol;
  40. typedef struct _cons{
  41. int car;
  42. struct _cons* cdr;
  43. }cons;
  44.  
  45. void print_list(cons *);
  46. cons *concat(cons *, cons *);
  47. cons *consf(int, cons *);
  48. cons *take(cons *, int);
  49. cons *drop(cons*, int);
  50. cons *cdr(cons *);
  51. int car (cons *);
  52. cons* L();
  53. cons* E();
  54. cons *Enum();
  55.  
  56.  
  57. void next_symbol() {
  58. symbol = yylex();
  59. }
  60.  
  61. void error(int nr) {
  62. printf("Syntax error: %d\n", nr);
  63. }
  64.  
  65. void parser()
  66. {
  67. print_list(E());
  68. //next_symbol();
  69. if (symbol != '\n')
  70. error(0);
  71. }
  72.  
  73. cons *E() {
  74. cons *firstL = L();
  75.  
  76. next_symbol();
  77. if (symbol == APPEND) {
  78. next_symbol();
  79. cons *rez = E();
  80. return concat(firstL, rez);
  81. } else {
  82. return firstL;
  83. }
  84. }
  85.  
  86. cons *L() {
  87. //cons *res = (cons *)malloc(sizeof(cons));
  88. if (symbol == '[') {
  89. next_symbol();
  90. cons *res = Enum();
  91. if (symbol == ']') {
  92. return res;
  93. }
  94. }
  95. else {
  96. if (symbol == TAKE) {
  97. next_symbol(); // read '('
  98. if (symbol == '(') {
  99. next_symbol(); // read nr
  100. int nr = yylval;
  101. next_symbol(); // read ','
  102. next_symbol(); // read '['
  103. cons *res = E();
  104.  
  105. if (symbol == ')') {
  106. return take(res, nr);
  107. } else {
  108. printf("%d", symbol);
  109. error(12);
  110. }
  111. } else {
  112. error(3);
  113. return NULL;
  114. }
  115. } else {
  116. if (symbol == DROP) {
  117. next_symbol(); // read '('
  118. if (symbol == '(') {
  119. next_symbol(); // read nr
  120. int nr = yylval;
  121. next_symbol(); // read ','
  122. next_symbol(); // read '['
  123.  
  124. cons *res = E();
  125.  
  126. if (symbol == ')') {
  127. return drop(res, nr);
  128. }
  129. }
  130. }
  131. else {
  132. if (symbol == NUMB) {
  133. int currentNr = yylval;
  134. next_symbol();
  135. if(symbol == CONS) {
  136. next_symbol();
  137. return consf(currentNr, L());
  138. }
  139. } else {
  140. error(33);
  141. return NULL;
  142. }
  143.  
  144. }
  145. }
  146. }
  147. }
  148.  
  149.  
  150.  
  151. cons *Enum() {
  152. if (symbol == NUMB) {
  153. int nr = yylval;
  154. next_symbol();
  155. if (symbol == ',') {
  156. next_symbol();
  157. cons *l = Enum();
  158. return consf(nr, l);
  159. } else{ // found ']'
  160. //next_symbol();
  161. return consf(nr, NULL);
  162. }
  163. }
  164. }
  165.  
  166.  
  167. int car (cons *l) {
  168. return l->car;
  169. }
  170.  
  171. cons *cdr(cons *l) {
  172. if (l == NULL)
  173. return NULL;
  174. return l->cdr;
  175. }
  176.  
  177. cons *take(cons *l, int nr) {
  178. cons *rez = (cons *)malloc(sizeof(cons));
  179. rez->car = l->car;
  180. l = l->cdr;
  181. nr--;
  182. for (int i = 0; i < nr; i++) {
  183. concat(rez, consf(l->car, NULL));
  184. l = l->cdr;
  185. }
  186. return rez;
  187. }
  188.  
  189. cons *drop(cons *l, int nr) {
  190. cons *rez = (cons *)malloc(sizeof(cons));
  191. rez->car = l->car;
  192. l = l->cdr;
  193. nr--;
  194. for (int i = 0; i < nr; i++) {
  195. concat(rez, consf(l->car, NULL));
  196. l = l->cdr;
  197. }
  198. return l;
  199. }
  200.  
  201. cons *consf(int el, cons *l) {
  202. cons *rez = (cons *)malloc(sizeof(cons));
  203. rez->car = el;
  204. rez->cdr = l;
  205. return rez;
  206. }
  207.  
  208. cons *concat(cons *l1, cons *l2) {
  209. cons *it = l1;
  210. if (l1 == NULL) {
  211. return l2;
  212. }
  213. while(it->cdr != NULL) {
  214. it = it->cdr;
  215. }
  216. it->cdr = l2;
  217. return l1;
  218. }
  219.  
  220. void print_list(cons *l) {
  221. printf("[");
  222. if(l == NULL) {
  223. printf("]\n");
  224. return;
  225. }
  226. while(l->cdr != NULL) {
  227. printf("%d ", l->car);
  228. l = l->cdr;
  229. }
  230. printf("%d]\n", l->car);
  231. }
  232.  
  233.  
  234. void main() {
  235. next_symbol();
  236. while (symbol != 0) {
  237. parser();
  238. next_symbol();
  239. }
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement