grammar.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #include "__virtualmath.h"
  2. #define readBackToken(status, pm) do{ \
  3. status = safeGetToken(pm->tm); \
  4. backToken(pm->tm->ts); \
  5. } while(0) /*预读token*/
  6. #define popAheadToken(token_var, pm) do{ \
  7. safeGetToken(pm->tm); \
  8. token_var = popToken(pm->tm->ts); \
  9. } while(0) /*弹出预读的token*/
  10. #define addStatementToken(type, st, pm) do{\
  11. Token *tmp_new_token; \
  12. tmp_new_token = makeStatementToken(type, st); \
  13. addToken(pm->tm->ts, tmp_new_token); \
  14. backToken(pm->tm->ts); \
  15. } while(0)
  16. #define backToken_(pm, token) do{ \
  17. addToken(pm->tm->ts, token); \
  18. backToken(pm->tm->ts); \
  19. }while(0)
  20. #define call_success(pm) (pm->status == success)
  21. void parserCommand(PASERSSIGNATURE);
  22. void parserOperation(PASERSSIGNATURE);
  23. void parserPolynomial(PASERSSIGNATURE);
  24. void parserBaseValue(PASERSSIGNATURE);
  25. void syntaxError(ParserMessage *pm, char *message, int status);
  26. ParserMessage *makeParserMessage(char *file_dir){
  27. ParserMessage *tmp = memCalloc(1, sizeof(ParserMessage));
  28. tmp->tm = makeTokenMessage(file_dir);
  29. tmp->status = success;
  30. tmp->status_message = NULL;
  31. return tmp;
  32. }
  33. void freePasersMessage(ParserMessage *pm, bool self) {
  34. freeTokenMessage(pm->tm, true);
  35. memFree(pm->status_message);
  36. if (self){
  37. memFree(pm);
  38. }
  39. }
  40. /**
  41. * 命令表匹配
  42. * pasersCommandList :
  43. * | MATHER_EOF
  44. * | parserCommand MATHER_ENTER
  45. * | parserCommand MATHER_EOF
  46. */
  47. void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *st) {
  48. int token_type, command_int, stop;
  49. struct Statement *base_st = st;
  50. while (true){
  51. readBackToken(token_type, pm);
  52. if (token_type == MATHER_EOF){
  53. // printf("get EOF\n");
  54. Token *tmp;
  55. popAheadToken(tmp, pm);
  56. freeToken(tmp, true, false);
  57. goto return_;
  58. }
  59. else if (token_type == MATHER_ENTER){
  60. // 处理空语句
  61. Token *tmp;
  62. popAheadToken(tmp, pm);
  63. freeToken(tmp, true, false);
  64. continue;
  65. }
  66. else{
  67. Token *command_token,*stop_token;
  68. parserCommand(CALLPASERSSIGNATURE);
  69. if (!call_success(pm)){
  70. goto return_;
  71. }
  72. readBackToken(command_int, pm);
  73. if (COMMAND != command_int){
  74. if (global){
  75. syntaxError(pm, "ERROR from command list(get parserCommand)", command_list_error);
  76. }
  77. goto return_;
  78. }
  79. popAheadToken(command_token, pm);
  80. readBackToken(stop, pm);
  81. if (stop == MATHER_ENTER){
  82. popAheadToken(stop_token, pm);
  83. freeToken(stop_token, true, false);
  84. }
  85. else if(stop == MATHER_EOF){
  86. popAheadToken(stop_token, pm);
  87. backToken_(pm, stop_token);
  88. }
  89. else{
  90. syntaxError(pm, "ERROR from parserCommand list(get stop)", command_list_error);
  91. freeToken(command_token, true, true);
  92. goto return_;
  93. }
  94. /*...do something for commandList...*/
  95. // printf("do something for commandList\n");
  96. connectStatement(base_st, command_token->data.st);
  97. freeToken(command_token, true, false);
  98. }
  99. }
  100. return_:
  101. addStatementToken(COMMANDLIST, base_st, pm);
  102. }
  103. /**
  104. * 命令匹配
  105. * parserCommand:
  106. * | parserOperation
  107. */
  108. void parserCommand(PASERSSIGNATURE){
  109. int token_type;
  110. Statement *st = NULL;
  111. readBackToken(token_type, pm);
  112. if (false){
  113. PASS
  114. }
  115. else{
  116. int command_int;
  117. Token *command_token;
  118. parserOperation(CALLPASERSSIGNATURE);
  119. if (!call_success(pm)){
  120. goto return_;
  121. }
  122. readBackToken(command_int, pm);
  123. if (command_int != OPERATION){
  124. goto return_;
  125. }
  126. popAheadToken(command_token, pm);
  127. /*...do something for command...*/
  128. // printf("do something for command\n");
  129. st = command_token->data.st;
  130. freeToken(command_token, true, false);
  131. }
  132. addStatementToken(COMMAND, st, pm);
  133. return_:
  134. return;
  135. }
  136. /**
  137. * 表达式匹配
  138. * parserOperation:
  139. * | parserPolynomial
  140. */
  141. void parserOperation(PASERSSIGNATURE){
  142. int operation_int;
  143. parserPolynomial(CALLPASERSSIGNATURE);
  144. if (!call_success(pm)){
  145. goto return_;
  146. }
  147. readBackToken(operation_int, pm);
  148. if (operation_int != POLYNOMIAL){
  149. goto return_;
  150. }
  151. Token *operation_token;
  152. popAheadToken(operation_token, pm);
  153. /*...do something for operation...*/
  154. // printf("do something for operation\n");
  155. addStatementToken(OPERATION, operation_token->data.st, pm);
  156. freeToken(operation_token, true, false);
  157. return_:
  158. return;
  159. }
  160. /**
  161. * 多项式匹配
  162. * parserPolynomial:
  163. * | parserBaseValue [1]
  164. * | parserPolynomial ADD parserBaseValue
  165. * | parserPolynomial SUB parserBaseValue
  166. */
  167. void parserPolynomial(PASERSSIGNATURE){
  168. while(true){
  169. int left, symbol, right;
  170. Token *left_token, *symbol_token, *right_token;
  171. struct Statement *st = NULL;
  172. readBackToken(left, pm);
  173. if (left != POLYNOMIAL){
  174. // 情况[1]
  175. parserBaseValue(CALLPASERSSIGNATURE); // 获得左值
  176. if (!call_success(pm)){
  177. goto return_;
  178. }
  179. readBackToken(left, pm);
  180. if (left != BASEVALUE){ // 若非正确数值
  181. goto return_;
  182. }
  183. popAheadToken(left_token, pm);
  184. addStatementToken(POLYNOMIAL, left_token->data.st, pm);
  185. freeToken(left_token, true, false);
  186. continue;
  187. // printf("polynomial: get base value\n");
  188. }
  189. popAheadToken(left_token, pm);
  190. readBackToken(symbol, pm);
  191. switch (symbol) {
  192. case MATHER_ADD:
  193. // printf("polynomial: get a add symbol\n");
  194. popAheadToken(symbol_token, pm);
  195. freeToken(symbol_token, true, false);
  196. symbol_token = NULL;
  197. st = makeStatement();
  198. st->type = operation;
  199. st->u.operation.OperationType = ADD;
  200. break;
  201. case MATHER_SUB:
  202. // printf("polynomial: get a sub symbol\n");
  203. popAheadToken(symbol_token, pm);
  204. freeToken(symbol_token, true, false);
  205. symbol_token = NULL;
  206. st = makeStatement();
  207. st->type = operation;
  208. st->u.operation.OperationType = SUB;
  209. break;
  210. default:
  211. // printf("polynomial: get another symbol\n");
  212. backToken_(pm, left_token);
  213. goto return_;
  214. }
  215. parserBaseValue(CALLPASERSSIGNATURE); // 获得左值
  216. if (!call_success(pm)){
  217. freeToken(left_token, true, false);
  218. freeStatement(st);
  219. goto return_;
  220. }
  221. readBackToken(right, pm);
  222. if (right != BASEVALUE){ // 若非正确数值
  223. syntaxError(pm, "ERROR from parserPolynomial(get right)", syntax_error);
  224. freeToken(left_token, true, true);
  225. freeStatement(st);
  226. goto return_;
  227. }
  228. popAheadToken(right_token, pm);
  229. st->u.operation.left = left_token->data.st;
  230. st->u.operation.right = right_token->data.st;
  231. freeToken(left_token, true, false);
  232. freeToken(right_token, true, false);
  233. addStatementToken(POLYNOMIAL, st, pm);
  234. // printf("polynomial: push token\n");
  235. }
  236. return_:
  237. return;
  238. }
  239. /**
  240. * 字面量匹配
  241. * parserBaseValue:
  242. * | MATHER_NUMBER
  243. * | MATHER_STRING
  244. */
  245. void parserBaseValue(PASERSSIGNATURE){
  246. int token_type;
  247. struct Statement *st = NULL;
  248. readBackToken(token_type, pm);
  249. if(MATHER_NUMBER == token_type){
  250. // 匹配到正常字面量
  251. Token *value_token;
  252. char *stop;
  253. popAheadToken(value_token, pm);
  254. st = makeStatement();
  255. st->type = base_value;
  256. st->u.base_value.value = makeNumberValue(strtol(value_token->data.str, &stop, 10), inter);
  257. freeToken(value_token, true, false);
  258. }
  259. else if(MATHER_STRING == token_type){
  260. Token *value_token;
  261. popAheadToken(value_token, pm);
  262. st = makeStatement();
  263. st->type = base_value;
  264. st->u.base_value.value = makeStringValue(value_token->data.str, inter);
  265. freeToken(value_token, true, false);
  266. }
  267. else{
  268. goto return_;
  269. }
  270. addStatementToken(BASEVALUE, st, pm);
  271. return_:
  272. return;
  273. }
  274. /**
  275. * syntax错误处理器
  276. * @param pm
  277. * @param message 错误信息
  278. * @param status 错误类型
  279. */
  280. void syntaxError(ParserMessage *pm, char *message, int status){
  281. pm->status = status;
  282. pm->status_message = memStrcpy(message, 0, false, false);
  283. }