grammar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. Token *tmp;
  54. popAheadToken(tmp, pm);
  55. freeToken(tmp, true, false);
  56. goto return_;
  57. }
  58. else if (token_type == MATHER_ENTER){
  59. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: <ENTER>\n", NULL);
  60. Token *tmp;
  61. popAheadToken(tmp, pm);
  62. freeToken(tmp, true, false);
  63. continue;
  64. }
  65. else{
  66. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: call command\n", NULL);
  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. if (global) {
  91. syntaxError(pm, "ERROR from parserCommand list(get stop)", command_list_error);
  92. freeToken(command_token, true, true);
  93. }
  94. else{
  95. // 若非global模式, 即可以匹配大括号
  96. popAheadToken(stop_token, pm);
  97. backToken_(pm, stop_token);
  98. connectStatement(base_st, command_token->data.st);
  99. freeToken(command_token, true, false);
  100. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  101. "Command List: get command success[at !global end]\n", NULL);
  102. }
  103. goto return_;
  104. }
  105. /*...do something for commandList...*/
  106. connectStatement(base_st, command_token->data.st);
  107. freeToken(command_token, true, false);
  108. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: get command success\n", NULL);
  109. }
  110. }
  111. return_:
  112. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command List: return\n", NULL);
  113. addStatementToken(COMMANDLIST, base_st, pm);
  114. }
  115. /**
  116. * 命令匹配
  117. * parserCommand:
  118. * | parserOperation
  119. */
  120. void parserCommand(PASERSSIGNATURE){
  121. int token_type;
  122. Statement *st = NULL;
  123. readBackToken(token_type, pm);
  124. if (false){
  125. PASS;
  126. }
  127. else{
  128. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Command: call operation\n", NULL);
  129. int command_int;
  130. Token *command_token;
  131. parserOperation(CALLPASERSSIGNATURE);
  132. if (!call_success(pm)){
  133. goto return_;
  134. }
  135. readBackToken(command_int, pm);
  136. if (command_int != OPERATION){
  137. goto return_;
  138. }
  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. /**
  150. * 表达式匹配
  151. * parserOperation:
  152. * | parserAssignment
  153. */
  154. void parserOperation(PASERSSIGNATURE){
  155. int operation_int;
  156. Token *operation_token;
  157. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: call assignment\n", NULL);
  158. callChild(pm, operation_int, operation_token, parserAssignment, ASSIGNMENT, return_);
  159. /*...do something for operation...*/
  160. addStatementToken(OPERATION, operation_token->data.st, pm);
  161. freeToken(operation_token, true, false);
  162. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: get polynomial success\n", NULL);
  163. return_:
  164. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Operation: return\n", NULL);
  165. }
  166. /**
  167. * 赋值表达式匹配
  168. * parserAssignment:
  169. * | parserPolynomial
  170. * | parserAssignment ASSIGNMENT parserPolynomial
  171. */
  172. bool switchAssignment(PASERSSIGNATURE, int symbol, Statement **st){
  173. switch (symbol) {
  174. case MATHER_ASSIGNMENT:
  175. *st = makeOperationStatement(ASS);
  176. break;
  177. default:
  178. return false;
  179. }
  180. return true;
  181. }
  182. void parserAssignment(PASERSSIGNATURE){
  183. return twoOperation(CALLPASERSSIGNATURE, parserPolynomial, switchAssignment, POLYNOMIAL, ASSIGNMENT,
  184. "polynomial", "assignment");
  185. }
  186. /**
  187. * 多项式匹配
  188. * parserPolynomial:
  189. * | parserBaseValue
  190. * | parserPolynomial ADD parserFactor
  191. * | parserPolynomial SUB parserFactor
  192. */
  193. bool switchPolynomial(PASERSSIGNATURE, int symbol, Statement **st){
  194. switch (symbol) {
  195. case MATHER_ADD:
  196. *st = makeOperationStatement(ADD);
  197. break;
  198. case MATHER_SUB:
  199. *st = makeOperationStatement(SUB);
  200. break;
  201. default:
  202. return false;
  203. }
  204. return true;
  205. }
  206. void parserPolynomial(PASERSSIGNATURE){
  207. return twoOperation(CALLPASERSSIGNATURE, parserFactor, switchPolynomial, FACTOR, POLYNOMIAL,
  208. "factor", "polynomial");
  209. }
  210. /**
  211. * 因式匹配
  212. * parserFactor:
  213. * | parserBaseValue [1]
  214. * | switchFactor ADD parserBaseValue
  215. * | switchFactor SUB parserBaseValue
  216. */
  217. bool switchFactor(PASERSSIGNATURE, int symbol, Statement **st){
  218. switch (symbol) {
  219. case MATHER_MUL:
  220. *st = makeOperationStatement(MUL);
  221. break;
  222. case MATHER_DIV:
  223. *st = makeOperationStatement(DIV);
  224. break;
  225. default:
  226. return false;
  227. }
  228. return true;
  229. }
  230. void parserFactor(PASERSSIGNATURE){
  231. return twoOperation(CALLPASERSSIGNATURE, parserBaseValue, switchFactor, BASEVALUE, FACTOR,
  232. "base value", "factor");
  233. }
  234. /**
  235. * 字面量匹配
  236. * parserBaseValue:
  237. * | MATHER_NUMBER
  238. * | MATHER_STRING
  239. */
  240. void parserBaseValue(PASERSSIGNATURE){
  241. int token_type;
  242. Token *value_token;
  243. struct Statement *st = NULL;
  244. readBackToken(token_type, pm);
  245. if(MATHER_NUMBER == token_type){
  246. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get number\n", NULL);
  247. popAheadToken(value_token, pm);
  248. char *stop;
  249. st = makeStatement();
  250. st->type = base_value;
  251. st->u.base_value.value = makeNumberValue(strtol(value_token->data.str, &stop, 10), inter);
  252. }
  253. else if(MATHER_STRING == token_type){
  254. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get string\n", NULL);
  255. popAheadToken(value_token, pm);
  256. st = makeStatement();
  257. st->type = base_value;
  258. st->u.base_value.value = makeStringValue(value_token->data.str, inter);
  259. }
  260. else if(MATHER_VAR == token_type){
  261. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: get var\n", NULL);
  262. popAheadToken(value_token, pm);
  263. st = makeStatement();
  264. st->type = base_var;
  265. st->u.base_var.name = memStrcpy(value_token->data.str, 0, false, false);
  266. st->u.base_var.times = NULL;
  267. }
  268. else{
  269. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Base Value: else\n", NULL);
  270. goto return_;
  271. }
  272. freeToken(value_token, true, false);
  273. addStatementToken(BASEVALUE, st, pm);
  274. return_:
  275. return;
  276. }
  277. 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){
  278. while(true){
  279. int left, symbol, right;
  280. Token *left_token, *symbol_token, *right_token;
  281. struct Statement *st = NULL;
  282. readBackToken(left, pm);
  283. if (left != self_type){
  284. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call %s(left)\n", self_name, call_name);
  285. callChild(pm, left, left_token, callBack, type, return_);
  286. addStatementToken(self_type, left_token->data.st, pm);
  287. freeToken(left_token, true, false);
  288. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  289. "%s: get %s(left) success[push %s]\n", self_name, call_name, self_name);
  290. continue;
  291. }
  292. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "%s: call symbol\n", self_name);
  293. popAheadToken(left_token, pm);
  294. readBackToken(symbol, pm);
  295. if (getSymbol(CALLPASERSSIGNATURE, symbol, &st)){
  296. delToken(pm);
  297. }
  298. else{
  299. backToken_(pm, left_token);
  300. goto return_;
  301. }
  302. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG,
  303. "%s: get symbol success\n%s: call %s[right]\n", self_name, self_name, call_name);
  304. callBack(CALLPASERSSIGNATURE); // 获得左值
  305. if (!call_success(pm)){
  306. freeToken(left_token, true, false);
  307. freeStatement(st);
  308. goto return_;
  309. }
  310. readBackToken(right, pm);
  311. if (right != type){ // 若非正确数值
  312. char *tmp1, *tmp2;
  313. tmp1 = memStrcat("ERROR from ", self_name);
  314. tmp2 = memStrcat(tmp1, "(get right)");
  315. syntaxError(pm, tmp2, syntax_error);
  316. memFree(tmp1);
  317. memFree(tmp2);
  318. freeToken(left_token, true, true);
  319. freeStatement(st);
  320. goto return_;
  321. }
  322. popAheadToken(right_token, pm);
  323. addToken_(pm, setOperationFromToken(st, left_token, right_token, self_type));
  324. writeLog_(pm->grammar_debug, GRAMMAR_DEBUG, "Polynomial: get base value(right) success[push polynomial]\n", NULL);
  325. }
  326. return_:
  327. return;
  328. }
  329. /**
  330. * syntax错误处理器
  331. * @param pm
  332. * @param message 错误信息
  333. * @param status 错误类型
  334. */
  335. void syntaxError(ParserMessage *pm, char *message, int status){
  336. if (pm->status != success)
  337. return;
  338. pm->status = status;
  339. pm->status_message = memStrcpy(message, 0, false, false);
  340. }