grammar.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. #include "__grammar.h"
  2. ParserMessage *makeParserMessage(char *file_dir, char *debug){
  3. ParserMessage *tmp = memCalloc(1, sizeof(ParserMessage));
  4. tmp->tm = makeTokenMessage(file_dir, debug);
  5. tmp->status = success;
  6. tmp->status_message = NULL;
  7. tmp->count = 0;
  8. #if OUT_LOG
  9. if (debug != NULL){
  10. char *debug_dir = memStrcat(debug, PASERS_LOG), *grammar_dir = memStrcat(debug, GRAMMAR_LOG);
  11. tmp->paser_debug = fopen(debug_dir, "w");
  12. tmp->grammar_debug = fopen(grammar_dir, "w");
  13. memFree(debug_dir);
  14. memFree(grammar_dir);
  15. }
  16. else{
  17. tmp->paser_debug = NULL;
  18. tmp->grammar_debug = NULL;
  19. }
  20. #else
  21. tmp->paser_debug = NULL;
  22. tmp->grammar_debug = NULL;
  23. #endif
  24. return tmp;
  25. }
  26. void freePasersMessage(ParserMessage *pm, bool self) {
  27. freeTokenMessage(pm->tm, true);
  28. #if OUT_LOG
  29. if (pm->paser_debug != NULL)
  30. fclose(pm->paser_debug);
  31. if (pm->grammar_debug != NULL)
  32. fclose(pm->grammar_debug);
  33. #endif
  34. memFree(pm->status_message);
  35. if (self){
  36. memFree(pm);
  37. }
  38. }
  39. /**
  40. * 命令表匹配
  41. * pasersCommandList :
  42. * | MATHER_EOF
  43. * | parserCommand MATHER_ENTER
  44. * | parserCommand MATHER_EOF
  45. */
  46. void pasersCommandList(ParserMessage *pm, Inter *inter, bool global, Statement *st) {
  47. int token_type, command_int, stop;
  48. struct Statement *base_st = st;
  49. while (true){
  50. readBackToken(token_type, pm);
  51. if (token_type == MATHER_EOF){
  52. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: <EOF>\n", NULL);
  53. delToken(pm);
  54. goto return_;
  55. }
  56. else if (token_type == MATHER_ENTER){
  57. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: <ENTER>\n", NULL);
  58. delToken(pm);
  59. continue;
  60. }
  61. else{
  62. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: call command\n", NULL);
  63. Token *command_token;
  64. parserCommand(CALLPASERSSIGNATURE);
  65. if (!call_success(pm)){
  66. goto return_;
  67. }
  68. readBackToken(command_int, pm);
  69. if (COMMAND != command_int){
  70. if (global){
  71. syntaxError(pm, "ERROR from command list(get parserCommand)", command_list_error);
  72. }
  73. goto return_;
  74. }
  75. popAheadToken(command_token, pm);
  76. readBackToken(stop, pm);
  77. if (stop == MATHER_ENTER || stop == MATHER_SEMICOLON){
  78. delToken(pm);
  79. }
  80. else if(stop == MATHER_EOF){
  81. PASS;
  82. }
  83. else{
  84. if (global) {
  85. syntaxError(pm, "ERROR from parserCommand list(get stop)", command_list_error);
  86. freeToken(command_token, true, true);
  87. }
  88. else{
  89. // 若非global模式, 即可以匹配大括号, token保留在ahead中
  90. connectStatement(base_st, command_token->data.st);
  91. freeToken(command_token, true, false);
  92. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  93. "Command List: get command success[at !global end]\n", NULL);
  94. }
  95. goto return_;
  96. }
  97. /*...do something for commandList...*/
  98. connectStatement(base_st, command_token->data.st);
  99. freeToken(command_token, true, false);
  100. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: get command success\n", NULL);
  101. }
  102. }
  103. return_:
  104. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: return\n", NULL);
  105. addStatementToken(COMMANDLIST, base_st, pm);
  106. }
  107. /**
  108. * 命令匹配
  109. * parserCommand:
  110. * | parserOperation
  111. */
  112. void parserCommand(PASERSSIGNATURE){
  113. int token_type;
  114. Statement *st = NULL;
  115. readBackToken(token_type, pm);
  116. if (MATHER_DEF == token_type){
  117. int def;
  118. Token *def_token = NULL;
  119. parserDef(CALLPASERSSIGNATURE);
  120. if (!call_success(pm))
  121. goto return_;
  122. readBackToken(def, pm);
  123. if (def != FUNCTION)
  124. goto return_;
  125. popAheadToken(def_token, pm);
  126. st = def_token->data.st;
  127. freeToken(def_token, true, false);
  128. }
  129. else{
  130. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: call operation\n", NULL);
  131. int command_type;
  132. Token *command_token = NULL;
  133. parserOperation(CALLPASERSSIGNATURE);
  134. if (!call_success(pm))
  135. goto return_;
  136. readBackToken(command_type, pm);
  137. if (command_type != OPERATION)
  138. goto return_;
  139. popAheadToken(command_token, pm);
  140. /*...do something for command...*/
  141. st = command_token->data.st;
  142. freeToken(command_token, true, false);
  143. }
  144. addStatementToken(COMMAND, st, pm);
  145. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: get command success\n", NULL);
  146. return_:
  147. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: get return\n", NULL);
  148. }
  149. void parserIf(PASERSSIGNATURE){
  150. // int if_type, code;
  151. // Token *if_token = NULL, *code_token = NULL;
  152. // struct Statement *st = NULL, *tmp = NULL;
  153. // StatementList *sl = NULL;
  154. //
  155. // checkToken(pm, MATHER_IF, return_);
  156. // callChild(pm, if_type, if_token, parserOperation, OPERATION, return_, error_);
  157. // if (if_type != OPERATION){
  158. // goto return_;
  159. // }
  160. // callChild(pm, code, code_token, parserCode, CODE, error_, error_);
  161. //
  162. // sl = makeConnectStatementList(sl, if_token->data.st, NULL, code_token->data.st);
  163. //
  164. // st = makeFunctionStatement(name_token->data.st, code_token->data.st);
  165. // addStatementToken(FUNCTION, st, pm);
  166. // freeToken(code_token, true, false);
  167. // freeToken(name_token, true, false);
  168. // return_: return;
  169. //
  170. // error_:
  171. // freeToken(if_token, true, true);
  172. // freeToken(code_token, true, true);
  173. // syntaxError(pm, "Don't get a if titile", syntax_error);
  174. // return;
  175. }
  176. void parserDef(PASERSSIGNATURE){
  177. int name_type, code;
  178. Token *name_token = NULL, *code_token = NULL;
  179. struct Statement *st = NULL;
  180. checkToken(pm, MATHER_DEF, return_);
  181. parserBaseValue(CALLPASERSSIGNATURE);
  182. if (!call_success(pm))
  183. goto return_;
  184. readBackToken(name_type, pm);
  185. if (name_type != BASEVALUE){
  186. syntaxError(pm, "Don't get a function name", syntax_error);
  187. goto return_;
  188. }
  189. popAheadToken(name_token, pm);
  190. checkToken(pm, MATHER_LP, error_);
  191. checkToken(pm, MATHER_RP, error_);
  192. parserCode(CALLPASERSSIGNATURE);
  193. if (!call_success(pm)){
  194. goto error_;
  195. }
  196. readBackToken(code, pm);
  197. if (code != CODE){
  198. freeToken(name_token, true, true);
  199. error_:
  200. syntaxError(pm, "Don't get a function code", syntax_error);
  201. goto return_;
  202. }
  203. popAheadToken(code_token, pm);
  204. st = makeFunctionStatement(name_token->data.st, code_token->data.st);
  205. addStatementToken(FUNCTION, st, pm);
  206. freeToken(code_token, true, false);
  207. freeToken(name_token, true, false);
  208. return_:
  209. return;
  210. }
  211. void parserCode(PASERSSIGNATURE){
  212. int code_type;
  213. Token *code_token = NULL;
  214. struct Statement *st = makeStatement();
  215. while (true){
  216. checkToken(pm, MATHER_LC, again_); // 若匹配的不是{则检查是否匹配到\n
  217. break; // 若匹配到{则跳出循环
  218. again_: checkToken(pm, MATHER_ENTER, return_); // 若不是\n则跳到return_
  219. }
  220. pasersCommandList(CALLPASERSSIGNATURE, false, st);
  221. if (!call_success(pm)){
  222. goto return_;
  223. }
  224. readBackToken(code_type, pm);
  225. if (code_type != COMMANDLIST){
  226. syntaxError(pm, "Not CommandList\n", syntax_error);
  227. goto return_;
  228. }
  229. popAheadToken(code_token, pm);
  230. checkToken(pm, MATHER_RC, error_);
  231. return_:
  232. addStatementToken(CODE, st, pm);
  233. freeToken(code_token, true, false);
  234. return;
  235. error_:
  236. syntaxError(pm, "Don't get the }", syntax_error);
  237. freeToken(code_token, true, true);
  238. return;
  239. }
  240. /**
  241. * 表达式匹配
  242. * parserOperation:
  243. * | parserAssignment
  244. */
  245. void parserOperation(PASERSSIGNATURE){
  246. int operation_type;
  247. Token *operation_token = NULL;
  248. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: call assignment\n", NULL);
  249. parserAssignment(CALLPASERSSIGNATURE);
  250. if (!call_success(pm))
  251. goto return_;
  252. readBackToken(operation_type, pm);
  253. if (operation_type != ASSIGNMENT){
  254. goto return_;
  255. }
  256. popAheadToken(operation_token, pm);
  257. /*...do something for operation...*/
  258. addStatementToken(OPERATION, operation_token->data.st, pm);
  259. freeToken(operation_token, true, false);
  260. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: get polynomial success\n", NULL);
  261. return_:
  262. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: return\n", NULL);
  263. }
  264. /**
  265. * 赋值表达式匹配
  266. * parserAssignment:
  267. * | parserPolynomial
  268. * | parserAssignment ASSIGNMENT parserPolynomial
  269. */
  270. bool switchAssignment(PASERSSIGNATURE, int symbol, Statement **st){
  271. switch (symbol) {
  272. case MATHER_ASSIGNMENT:
  273. *st = makeOperationStatement(ASS);
  274. break;
  275. default:
  276. return false;
  277. }
  278. return true;
  279. }
  280. void parserAssignment(PASERSSIGNATURE){
  281. return twoOperation(CALLPASERSSIGNATURE, parserPolynomial, switchAssignment, POLYNOMIAL, ASSIGNMENT,
  282. "polynomial", "assignment");
  283. }
  284. /**
  285. * 多项式匹配
  286. * parserPolynomial:
  287. * | parserBaseValue
  288. * | parserPolynomial ADD parserFactor
  289. * | parserPolynomial SUB parserFactor
  290. */
  291. bool switchPolynomial(PASERSSIGNATURE, int symbol, Statement **st){
  292. switch (symbol) {
  293. case MATHER_ADD:
  294. *st = makeOperationStatement(ADD);
  295. break;
  296. case MATHER_SUB:
  297. *st = makeOperationStatement(SUB);
  298. break;
  299. default:
  300. return false;
  301. }
  302. return true;
  303. }
  304. void parserPolynomial(PASERSSIGNATURE){
  305. return twoOperation(CALLPASERSSIGNATURE, parserFactor, switchPolynomial, FACTOR, POLYNOMIAL,
  306. "factor", "polynomial");
  307. }
  308. /**
  309. * 因式匹配
  310. * parserFactor:
  311. * | parserBaseValue [1]
  312. * | switchFactor ADD parserBaseValue
  313. * | switchFactor SUB parserBaseValue
  314. */
  315. bool switchFactor(PASERSSIGNATURE, int symbol, Statement **st){
  316. switch (symbol) {
  317. case MATHER_MUL:
  318. *st = makeOperationStatement(MUL);
  319. break;
  320. case MATHER_DIV:
  321. *st = makeOperationStatement(DIV);
  322. break;
  323. default:
  324. return false;
  325. }
  326. return true;
  327. }
  328. void parserFactor(PASERSSIGNATURE){
  329. return twoOperation(CALLPASERSSIGNATURE, parserCallBack, switchFactor, CALLBACK, FACTOR,
  330. "call back", "factor");
  331. }
  332. int tailCall(PASERSSIGNATURE, Token *left_token, Statement **st){
  333. int symbol;
  334. readBackToken(symbol, pm);
  335. if (symbol != MATHER_LP){
  336. return -1;
  337. }
  338. delToken(pm);
  339. checkToken(pm, MATHER_RP, error_);
  340. *st = makeCallStatement(left_token->data.st);
  341. return 1;
  342. error_:
  343. syntaxError(pm, "Don't get ) from call back", syntax_error);
  344. return 0;
  345. }
  346. void parserCallBack(PASERSSIGNATURE){
  347. return tailOperation(CALLPASERSSIGNATURE, parserBaseValue, tailCall, BASEVALUE, CALLBACK,
  348. "Base Value", "Call Back");
  349. }
  350. int getOperation(PASERSSIGNATURE, int right_type, Statement **st, char *name){
  351. int operation_type, rp;
  352. Token *operation_token;
  353. parserOperation(CALLPASERSSIGNATURE);
  354. if (!call_success(pm)){
  355. return 0;
  356. }
  357. readBackToken(operation_type, pm);
  358. if (operation_type != OPERATION){
  359. return 0;
  360. }
  361. popAheadToken(operation_token, pm);
  362. readBackToken(rp, pm);
  363. if (right_type != rp){
  364. return -1;
  365. }
  366. delToken(pm);
  367. *st = operation_token->data.st;
  368. freeToken(operation_token, true, false);
  369. return 1;
  370. }
  371. /**
  372. * 字面量匹配
  373. * parserBaseValue:
  374. * | MATHER_NUMBER
  375. * | MATHER_STRING
  376. */
  377. void parserBaseValue(PASERSSIGNATURE){
  378. int token_type;
  379. Token *value_token = NULL;
  380. struct Statement *st = NULL;
  381. readBackToken(token_type, pm);
  382. if(MATHER_NUMBER == token_type){
  383. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get number\n", NULL);
  384. popAheadToken(value_token, pm);
  385. char *stop;
  386. st = makeStatement();
  387. st->type = base_value;
  388. st->u.base_value.value = makeNumberValue(strtol(value_token->data.str, &stop, 10), inter);
  389. }
  390. else if(MATHER_STRING == token_type){
  391. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get string\n", NULL);
  392. popAheadToken(value_token, pm);
  393. st = makeStatement();
  394. st->type = base_value;
  395. st->u.base_value.value = makeStringValue(value_token->data.str, inter);
  396. }
  397. else if(MATHER_VAR == token_type){
  398. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get var\n", NULL);
  399. popAheadToken(value_token, pm);
  400. st = makeStatement();
  401. st->type = base_var;
  402. st->u.base_var.name = memStrcpy(value_token->data.str, 0, false, false);
  403. st->u.base_var.times = NULL;
  404. }
  405. else if(MATHER_LB == token_type){
  406. int var, tmp;
  407. Statement *tmp_st = NULL;
  408. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "base value: get operation\n", NULL);
  409. popAheadToken(value_token, pm);
  410. tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RB, &tmp_st, "base value");
  411. if (tmp == 0){
  412. freeToken(value_token, true, true);
  413. syntaxError(pm, "Don't get operation from list/var", syntax_error);
  414. goto return_;
  415. }
  416. else if(tmp == -1){
  417. freeToken(value_token, true, true);
  418. syntaxError(pm, "Don't get ] from list/var", syntax_error);
  419. goto return_;
  420. }
  421. readBackToken(var, pm);
  422. if (MATHER_VAR == var){
  423. Token *var_token;
  424. popAheadToken(var_token, pm);
  425. st = makeStatement();
  426. st->type = base_var;
  427. st->u.base_var.name = memStrcpy(var_token->data.str, 0, false, false);
  428. st->u.base_var.times = tmp_st;
  429. freeToken(var_token, true, false);
  430. }
  431. // TODO-szh list处理
  432. }
  433. else if(MATHER_LP == token_type){
  434. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "base value: get operation\n", NULL);
  435. popAheadToken(value_token, pm);
  436. int tmp = getOperation(CALLPASERSSIGNATURE, MATHER_RP, &st, "base value");
  437. if (tmp == 0){
  438. freeToken(value_token, true, true);
  439. syntaxError(pm, "Don't get operation from Base Value", syntax_error);
  440. goto return_;
  441. }
  442. else if(tmp == -1){
  443. freeToken(value_token, true, true);
  444. syntaxError(pm, "Don't get ) from Base Value", syntax_error);
  445. goto return_;
  446. }
  447. }
  448. else{
  449. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: else\n", NULL);
  450. goto return_;
  451. }
  452. freeToken(value_token, true, false);
  453. addStatementToken(BASEVALUE, st, pm);
  454. return_:
  455. return;
  456. }
  457. inline void twoOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*getSymbol)(PASERSSIGNATURE, int symbol, Statement **st), int type, int self_type, char *call_name, char *self_name){
  458. while(true){
  459. int left, symbol, right;
  460. Token *left_token = NULL, *symbol_token = NULL, *right_token = NULL;
  461. struct Statement *st = NULL;
  462. readBackToken(left, pm);
  463. if (left != self_type){
  464. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  465. callBack(CALLPASERSSIGNATURE);
  466. if (!call_success(pm))
  467. goto return_;
  468. readBackToken(left, pm);
  469. if (left != type){
  470. goto return_;
  471. }
  472. popAheadToken(left_token, pm);
  473. addStatementToken(self_type, left_token->data.st, pm);
  474. freeToken(left_token, true, false);
  475. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  476. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  477. continue;
  478. }
  479. popAheadToken(left_token, pm);
  480. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
  481. readBackToken(symbol, pm);
  482. if (getSymbol(CALLPASERSSIGNATURE, symbol, &st)){
  483. delToken(pm);
  484. }
  485. else{
  486. backToken_(pm, left_token);
  487. goto return_;
  488. }
  489. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  490. "%s: get symbol success\n%s: call %s[right]\n", self_name, self_name, call_name);
  491. callBack(CALLPASERSSIGNATURE); // 获得左值
  492. if (!call_success(pm)){
  493. freeToken(left_token, true, false);
  494. freeStatement(st);
  495. goto return_;
  496. }
  497. readBackToken(right, pm);
  498. if (right != type){ // 若非正确数值
  499. char *tmp1, *tmp2;
  500. tmp1 = memStrcat("ERROR from ", self_name);
  501. tmp2 = memStrcat(tmp1, "(get right)");
  502. syntaxError(pm, tmp2, syntax_error);
  503. memFree(tmp1);
  504. memFree(tmp2);
  505. freeToken(left_token, true, true);
  506. freeStatement(st);
  507. goto return_;
  508. }
  509. popAheadToken(right_token, pm);
  510. addToken_(pm, setOperationFromToken(st, left_token, right_token, self_type));
  511. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  512. }
  513. return_:
  514. return;
  515. }
  516. inline void tailOperation(PASERSSIGNATURE, void (*callBack)(PASERSSIGNATURE), int (*tailFunction)(PASERSSIGNATURE, Token *left_token, Statement **st), int type, int self_type, char *call_name, char *self_name){
  517. while(true){
  518. int left, symbol;
  519. Token *left_token = NULL, *symbol_token = NULL, *right_token = NULL;
  520. struct Statement *st = NULL;
  521. readBackToken(left, pm);
  522. if (left != self_type){
  523. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  524. callBack(CALLPASERSSIGNATURE);
  525. if (!call_success(pm))
  526. goto return_;
  527. readBackToken(left, pm);
  528. if (left != type){
  529. goto return_;
  530. }
  531. popAheadToken(left_token, pm);
  532. addStatementToken(self_type, left_token->data.st, pm);
  533. freeToken(left_token, true, false);
  534. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  535. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  536. continue;
  537. }
  538. popAheadToken(left_token, pm);
  539. int tail_status = tailFunction(CALLPASERSSIGNATURE, left_token, &st);
  540. if (tail_status == -1){
  541. backToken_(pm, left_token);
  542. goto return_;
  543. }
  544. else if(!tail_status){
  545. goto error_;
  546. }
  547. addStatementToken(self_type, st, pm);
  548. freeToken(left_token, true, false);
  549. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call tail success\n", self_name);
  550. continue;
  551. error_:
  552. freeToken(left_token, true, true);
  553. goto return_;
  554. }
  555. return_:
  556. return;
  557. }
  558. /**
  559. * syntax错误处理器
  560. * @param pm
  561. * @param message 错误信息
  562. * @param status 错误类型
  563. */
  564. void syntaxError(ParserMessage *pm, char *message, int status){
  565. if (pm->status != success)
  566. return;
  567. pm->status = status;
  568. pm->status_message = memStrcpy(message, 0, false, false);
  569. }