SHARE
TWEET

Untitled

a guest Feb 18th, 2020 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "Lexer.h"
  2. #include "Parser.h"
  3. #include "Attributes.h"
  4.  
  5. namespace Calculator {
  6. ATTRIBUTES(E) {
  7.     ATTRIBUTE_AND_GETTER(int, val)
  8. };
  9. ATTRIBUTES(EPrime) {
  10.     ATTRIBUTE_AND_GETTER(int, val)
  11.     ATTRIBUTE_AND_GETTER(int, acc)
  12. };
  13. ATTRIBUTES(T) {
  14.     ATTRIBUTE_AND_GETTER(int, val)
  15. };
  16. ATTRIBUTES(TPrime) {
  17.     ATTRIBUTE_AND_GETTER(int, val)
  18.     ATTRIBUTE_AND_GETTER(int, acc)
  19. };
  20. ATTRIBUTES(F) {
  21.     ATTRIBUTE_AND_GETTER(int, val)
  22. };
  23. START(E)
  24. TRANSLATING(ADD) {
  25.     ATTRIBUTE(int, x)
  26.     ATTRIBUTE(int, y)
  27.     ATTRIBUTE(int, res)
  28.     CODE {
  29.         LOCAL_ATTR(x)
  30.         LOCAL_ATTR(y)
  31.         LOCAL_ATTR(res)
  32. // user code begin
  33. res = x + y;
  34. // user code end
  35.     }
  36. };
  37. TRANSLATING(SUB) {
  38.     ATTRIBUTE(int, x)
  39.     ATTRIBUTE(int, y)
  40.     ATTRIBUTE(int, res)
  41.     CODE {
  42.         LOCAL_ATTR(x)
  43.         LOCAL_ATTR(y)
  44.         LOCAL_ATTR(res)
  45. // user code begin
  46. res = x - y;
  47. // user code end
  48.     }
  49. };
  50. TRANSLATING(MUL) {
  51.     ATTRIBUTE(int, x)
  52.     ATTRIBUTE(int, y)
  53.     ATTRIBUTE(int, res)
  54.     CODE {
  55.         LOCAL_ATTR(x)
  56.         LOCAL_ATTR(y)
  57.         LOCAL_ATTR(res)
  58. // user code begin
  59. res = x * y;
  60. // user code end
  61.     }
  62. };
  63. TRANSLATING(DIV) {
  64.     ATTRIBUTE(int, x)
  65.     ATTRIBUTE(int, y)
  66.     ATTRIBUTE(int, res)
  67.     CODE {
  68.         LOCAL_ATTR(x)
  69.         LOCAL_ATTR(y)
  70.         LOCAL_ATTR(res)
  71. // user code begin
  72. res = x / y;
  73. // user code end
  74.     }
  75. };
  76. TRANSLATING(MEDIAN) {
  77.     ATTRIBUTE(int, x)
  78.     ATTRIBUTE(int, y)
  79.     ATTRIBUTE(int, z)
  80.     ATTRIBUTE(int, res)
  81.     CODE {
  82.         LOCAL_ATTR(x)
  83.         LOCAL_ATTR(y)
  84.         LOCAL_ATTR(z)
  85.         LOCAL_ATTR(res)
  86. // user code begin
  87. if (y <= x && x <= z) {res = x;} else if (x <= y && y <= z) {res = y;} else {res = z;}
  88. // user code end
  89.     }
  90. };
  91. TRANSLATING(TOINT) {
  92.     ATTRIBUTE(std::string, x)
  93.     ATTRIBUTE(int, res)
  94.     CODE {
  95.         LOCAL_ATTR(x)
  96.         LOCAL_ATTR(res)
  97. // user code begin
  98. res = std::stoi(x);
  99. // user code end
  100.     }
  101. };
  102. GET_PARSER_INFO {
  103. return
  104.  {{
  105. { 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, },
  106. { 0, 0, 4, 2, 3, 0, 0, 0, 4, 4, 4, },
  107. { 5, 5, 0, 0, 0, 0, 0, 5, 0, 0, 0, },
  108. { 0, 0, 8, 8, 8, 6, 7, 0, 8, 8, 8, },
  109. { 9, 10, 0, 0, 0, 0, 0, 11, 0, 0, 0, },
  110. }
  111. ,
  112. {
  113. { },
  114. { 14, 13, },
  115. { 3, 14, 17, 13, },
  116. { 4, 14, 18, 13, },
  117. { 11, },
  118. { 16, 15, },
  119. { 5, 16, 19, 15, },
  120. { 6, 16, 20, 15, },
  121. { 11, },
  122. { 0, 22, },
  123. { 1, 12, 2, },
  124. { 7, 12, 9, 12, 9, 12, 8, 21, },
  125. }
  126. ,
  127. {
  128. ASSIGNER {},
  129. ASSIGNER {
  130.     NT0(E, 3)
  131.     NT(T, 1)
  132.     NT(EPrime, 2)
  133.     CONNECT(0, val, 2, val)
  134.     CONNECT(2, acc, 1, val)
  135. },
  136. ASSIGNER {
  137.     NT0(EPrime, 5)
  138.     T(1)
  139.     NT(T, 2)
  140.     NT(ADD, 3)
  141.     NT(EPrime, 4)
  142.     CONNECT(0, val, 4, val)
  143.     CONNECT(4, acc, 3, res)
  144.     CONNECT(3, y, 2, val)
  145.     CONNECT(3, x, 0, acc)
  146. },
  147. ASSIGNER {
  148.     NT0(EPrime, 5)
  149.     T(1)
  150.     NT(T, 2)
  151.     NT(SUB, 3)
  152.     NT(EPrime, 4)
  153.     CONNECT(0, val, 4, val)
  154.     CONNECT(4, acc, 3, res)
  155.     CONNECT(3, y, 2, val)
  156.     CONNECT(3, x, 0, acc)
  157. },
  158. ASSIGNER {
  159.     NT0(EPrime, 2)
  160.     T(1)
  161.     CONNECT(0, val, 0, acc)
  162. },
  163. ASSIGNER {
  164.     NT0(T, 3)
  165.     NT(F, 1)
  166.     NT(TPrime, 2)
  167.     CONNECT(0, val, 2, val)
  168.     CONNECT(2, acc, 1, val)
  169. },
  170. ASSIGNER {
  171.     NT0(TPrime, 5)
  172.     T(1)
  173.     NT(F, 2)
  174.     NT(MUL, 3)
  175.     NT(TPrime, 4)
  176.     CONNECT(0, val, 4, val)
  177.     CONNECT(4, acc, 3, res)
  178.     CONNECT(3, y, 2, val)
  179.     CONNECT(3, x, 0, acc)
  180. },
  181. ASSIGNER {
  182.     NT0(TPrime, 5)
  183.     T(1)
  184.     NT(F, 2)
  185.     NT(DIV, 3)
  186.     NT(TPrime, 4)
  187.     CONNECT(0, val, 4, val)
  188.     CONNECT(4, acc, 3, res)
  189.     CONNECT(3, y, 2, val)
  190.     CONNECT(3, x, 0, acc)
  191. },
  192. ASSIGNER {
  193.     NT0(TPrime, 2)
  194.     T(1)
  195.     CONNECT(0, val, 0, acc)
  196. },
  197. ASSIGNER {
  198.     NT0(F, 3)
  199.     T(1)
  200.     NT(TOINT, 2)
  201.     CONNECT(0, val, 2, res)
  202.     CONNECT(2, x, 1, value)
  203. },
  204. ASSIGNER {
  205.     NT0(F, 4)
  206.     T(1)
  207.     NT(E, 2)
  208.     T(3)
  209.     CONNECT(0, val, 2, val)
  210. },
  211. ASSIGNER {
  212.     NT0(F, 9)
  213.     T(1)
  214.     NT(E, 2)
  215.     T(3)
  216.     NT(E, 4)
  217.     T(5)
  218.     NT(E, 6)
  219.     T(7)
  220.     NT(MEDIAN, 8)
  221.     CONNECT(0, val, 8, res)
  222.     CONNECT(8, x, 2, val)
  223.     CONNECT(8, y, 4, val)
  224.     CONNECT(8, z, 6, val)
  225. },
  226. },
  227. ATTRIBUTE_FABRIC {
  228. switch (id) {
  229.     case 0:
  230.     case 1:
  231.     case 2:
  232.     case 3:
  233.     case 4:
  234.     case 5:
  235.     case 6:
  236.     case 7:
  237.     case 8:
  238.     case 9:
  239.     case 10:
  240.     case 11:
  241.         RET_T
  242.     case 12: RET_NT(E)
  243.     case 13: RET_NT(EPrime)
  244.     case 14: RET_NT(T)
  245.     case 15: RET_NT(TPrime)
  246.     case 16: RET_NT(F)
  247.     case 17: RET_NT(ADD)
  248.     case 18: RET_NT(SUB)
  249.     case 19: RET_NT(MUL)
  250.     case 20: RET_NT(DIV)
  251.     case 21: RET_NT(MEDIAN)
  252.     case 22: RET_NT(TOINT)
  253.     default: IMPOSSIBLE}
  254. }
  255. };
  256. }
  257. GET_LEXER_INFO {
  258. return
  259.  {{
  260.  "[0-9]+",
  261.  "\\(",
  262.  "\\)",
  263.  "\\+",
  264.  "\\-",
  265.  "\\*",
  266.  "/",
  267.  "\\{",
  268.  "\\}",
  269.  ",",
  270. }};
  271. }
  272. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top