syntax.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. #include"token.h"
  2. #include"lex.h"
  3. #include"../inter/interpreter.h"
  4. void factor(int *status, token_node *list);
  5. void power(int *status, token_node *list);
  6. void number(int *status, token_node *list);
  7. void element(int *status, token_node *list);
  8. void var_token(int *status, token_node *list);
  9. void polynomial(int *status, token_node *list);
  10. void bit_move(int *status, token_node *list);
  11. void command(int *status, token_node *list);
  12. void while_(int *status, token_node *list);
  13. void if_(int *status, token_node *list);
  14. void for_(int *status, token_node *list);
  15. void elif_(int *status, token_node *list);
  16. void block_(int *status, token_node *list);
  17. void top_exp(int *status, token_node *list);
  18. void negative(int *status, token_node *list);
  19. void bit_not(int *status, token_node *list);
  20. void bit_notor(int *status, token_node *list);
  21. void bit_or(int *status, token_node *list);
  22. void bit_and(int *status, token_node *list);
  23. void compare(int *status, token_node *list);
  24. void bool_and(int *status, token_node *list);
  25. void bool_or(int *status, token_node *list);
  26. void bool_not(int *status, token_node *list);
  27. void paser_error(char *text);
  28. /*
  29. command_list : command
  30. | command_list command
  31. */
  32. void command_list(int *status, token_node *list){ // 多项式
  33. fprintf(status_log, "[info][grammar] mode status: top_exp\n", text);
  34. token left, right, new_token;
  35. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  36. if(left.type == NON_command_list){ // 模式2
  37. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  38. get_right_token(status, list, command, right); // 回调右边
  39. if(right.type == NON_command){
  40. new_token.type = NON_command_list;
  41. new_token.data_type = empty;
  42. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  43. return command_list(status, list); // 回调自己
  44. }
  45. else{ // 递归跳出[EOF_token]
  46. printf("right.type = %d\n", right.type);
  47. fprintf(status_log, "[info][grammar] (command_list)out\n");
  48. back_one_token(list, left);
  49. back_again(list, right);
  50. return;
  51. }
  52. }
  53. else if(left.type == EOF_token){ // 递归跳出的条件
  54. fprintf(status_log, "[info][grammar] (command_list)out\n");
  55. return;
  56. }
  57. else{ // 模式1
  58. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  59. back_one_token(list, left);
  60. get_base_token(status, list, command, new_token);
  61. if(new_token.type != NON_command){
  62. back_one_token(list, new_token); // 往回[不匹配类型]
  63. return;
  64. }
  65. new_token.type = NON_command_list;
  66. add_node(list, new_token);
  67. return command_list(status, list); // 回调自己
  68. }
  69. }
  70. /*
  71. command : top_exp <ENTER>
  72. */
  73. void command(int *status, token_node *list){ // 多项式
  74. fprintf(status_log, "[info][grammar] mode status: command\n", text);
  75. token left, new_token;
  76. left = pop_node(list); // 先弹出一个token 检查token
  77. if(left.type == WHILE_PASER){ // 是while类型的数据
  78. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  79. back_one_token(list, left);
  80. get_base_token(status, list, while_, new_token);
  81. get_stop_token();
  82. push_statement(statement_base, new_token);
  83. }
  84. if(left.type == IF_PASER){ // 是while类型的数据
  85. fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
  86. back_one_token(list, left);
  87. get_base_token(status, list, if_, new_token);
  88. get_stop_token();
  89. push_statement(statement_base, new_token);
  90. }
  91. if(left.type == FOR_PASER){ // 是while类型的数据
  92. fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
  93. back_one_token(list, left);
  94. get_base_token(status, list, for_, new_token);
  95. get_stop_token();
  96. push_statement(statement_base, new_token);
  97. }
  98. else if(left.type == ENTER_PASER){
  99. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  100. }
  101. else if(left.type == EOF_token){
  102. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  103. back_one_token(list, left);
  104. goto return_back;
  105. }
  106. else{ // 表达式
  107. fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
  108. back_one_token(list, left);
  109. get_base_token(status, list, top_exp, new_token);
  110. if(new_token.type != NON_top_exp){
  111. back_one_token(list, new_token); // 往回[不匹配类型]
  112. return;
  113. }
  114. get_stop_token();
  115. push_statement(statement_base, new_token);
  116. }
  117. new_token.type = NON_command;
  118. add_node(list, new_token);
  119. return_back:
  120. return; // 回调自己
  121. }
  122. /*
  123. if_ : IF LB top_exp RB block
  124. */
  125. void if_(int *status, token_node *list){
  126. fprintf(status_log, "[info][grammar] mode status: if_\n");
  127. token if_t, lb_t, exp_t, rb_t, block_t, next_t, child_t, new_token;
  128. if_t = pop_node(list);
  129. if(if_t.type == IF_PASER){
  130. get_pop_token(status, list, lb_t);
  131. if(lb_t.type != LB_PASER){
  132. paser_error("Don't get '('");
  133. }
  134. get_right_token(status,list,top_exp,exp_t);
  135. if(exp_t.type != NON_top_exp){ // 不是表达式
  136. paser_error("Don't get 'top_exp'");
  137. }
  138. get_pop_token(status, list, rb_t);
  139. if(rb_t.type != RB_PASER){
  140. paser_error("Don't get ')'");
  141. }
  142. get_right_token(status,list,block_,block_t);
  143. if(block_t.type != NON_block){ // 不是表达式
  144. paser_error("Don't get '{'");
  145. }
  146. statement *if_tmp = make_statement();
  147. if_tmp->type = if_branch;
  148. if_tmp->code.if_branch.done = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  149. // 检查else和elseif
  150. el_again:
  151. get_pop_token(status,list,next_t);
  152. if(next_t.type == ENTER_PASER){ // 忽略Enter
  153. goto el_again;
  154. }
  155. printf("next_t.type = %d\n", next_t.type);
  156. if(next_t.type == ELIF_PASER || next_t.type == ELSE_PASER){ // elif
  157. back_one_token(list, next_t);
  158. get_base_token(status,list,elif_,child_t);
  159. if(child_t.type == NON_elif){
  160. append_elif(child_t.data.if_list_base, if_tmp->code.if_branch.done);
  161. goto el_again;
  162. }
  163. }
  164. else{
  165. back_one_token(list, next_t);
  166. }
  167. new_token.type = NON_if;
  168. new_token.data_type = statement_value;
  169. new_token.data.statement_value = if_tmp;
  170. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  171. return;
  172. }
  173. else{
  174. back_one_token(list, if_t);
  175. return;
  176. }
  177. }
  178. /*
  179. elif_ : ELIF LB top_exp RB block
  180. */
  181. void elif_(int *status, token_node *list){
  182. fprintf(status_log, "[info][grammar] mode status: elif_\n");
  183. token elif_t, lb_t, exp_t, rb_t, block_t, next_t, new_token;
  184. elif_t = pop_node(list);
  185. if(elif_t.type == ELIF_PASER){
  186. get_pop_token(status, list, lb_t);
  187. if(lb_t.type != LB_PASER){
  188. paser_error("Don't get '('");
  189. }
  190. get_right_token(status,list,top_exp,exp_t);
  191. if(exp_t.type != NON_top_exp){ // 不是表达式
  192. paser_error("Don't get 'top_exp'");
  193. }
  194. get_pop_token(status, list, rb_t);
  195. if(rb_t.type != RB_PASER){
  196. paser_error("Don't get ')'");
  197. }
  198. get_right_token(status,list,block_,block_t);
  199. if(block_t.type != NON_block){ // 不是表达式
  200. paser_error("Don't get '{'");
  201. }
  202. if_list *if_tmp = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  203. new_token.type = NON_elif;
  204. new_token.data_type = if_list_base;
  205. new_token.data.if_list_base = if_tmp;
  206. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  207. return;
  208. }
  209. if(elif_t.type == ELSE_PASER){ // else
  210. get_right_token(status,list,block_,block_t);
  211. if(block_t.type != NON_block){ // 不是表达式
  212. paser_error("Don't get '{'");
  213. }
  214. if_list *if_tmp = make_if(NULL, block_t.data.statement_value);
  215. new_token.type = NON_elif;
  216. new_token.data_type = if_list_base;
  217. new_token.data.if_list_base = if_tmp;
  218. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  219. return;
  220. }
  221. else{
  222. back_one_token(list, elif_t);
  223. return;
  224. }
  225. }
  226. /*
  227. for_ : FOR LB top_exp COMMA top_exp COMMA top_exp RB block
  228. */
  229. void for_(int *status, token_node *list){
  230. fprintf(status_log, "[info][grammar] mode status: while_\n");
  231. token for_t, exp_1, exp_2, exp_3, block_t, lb_t, rb_t, comma_t,new_token;
  232. statement *exp_a, *exp_b, *exp_c;
  233. for_t = pop_node(list);
  234. if(for_t.type == FOR_PASER){
  235. get_pop_token(status, list, lb_t);
  236. if(lb_t.type != LB_PASER){
  237. paser_error("Don't get '('");
  238. }
  239. get_pop_token(status, list, exp_1);
  240. if(exp_1.type == COMMA_PASER){
  241. exp_a = NULL; // exp_1 = NULL;
  242. }
  243. else{
  244. back_one_token(list, exp_1);
  245. get_base_token(status,list,top_exp,exp_1); // 不是使用right token,不需要执行safe_get_token
  246. if(exp_1.type != NON_top_exp){ // 不是表达式
  247. paser_error("Don't get 'top_exp'");
  248. }
  249. get_pop_token(status, list, comma_t);
  250. if(comma_t.type != COMMA_PASER){
  251. paser_error("Don't get ';' in for cycle");
  252. }
  253. exp_a = exp_1.data.statement_value;
  254. }
  255. get_pop_token(status, list, exp_2);
  256. if(exp_2.type == COMMA_PASER){
  257. exp_b = NULL; // exp_1 = NULL;
  258. }
  259. else{
  260. back_one_token(list, exp_2);
  261. get_base_token(status,list,top_exp,exp_2); // 不是使用right token,不需要执行safe_get_token
  262. if(exp_2.type != NON_top_exp){ // 不是表达式
  263. paser_error("Don't get 'top_exp'");
  264. }
  265. get_pop_token(status, list, comma_t);
  266. if(comma_t.type != COMMA_PASER){
  267. paser_error("Don't get ';' in for cycle");
  268. }
  269. exp_b = exp_2.data.statement_value;
  270. }
  271. get_pop_token(status, list, exp_3);
  272. if(exp_3.type == RB_PASER){
  273. exp_c = NULL; // exp_1 = NULL;
  274. back_one_token(list, exp_3);
  275. }
  276. else{
  277. back_one_token(list, exp_3);
  278. get_base_token(status,list,top_exp,exp_3); // 不是使用right token,不需要执行safe_get_token
  279. if(exp_3.type != NON_top_exp){ // 不是表达式
  280. paser_error("Don't get 'top_exp'");
  281. }
  282. exp_c = exp_3.data.statement_value;
  283. }
  284. get_pop_token(status, list, rb_t);
  285. if(rb_t.type != RB_PASER){
  286. paser_error("Don't get ')'");
  287. }
  288. get_right_token(status,list,block_,block_t);
  289. if(block_t.type != NON_block){ // 不是表达式
  290. paser_error("Don't get '{'");
  291. }
  292. statement *for_tmp = make_statement();
  293. for_tmp->type = for_cycle;
  294. for_tmp->code.for_cycle.first = exp_a;
  295. for_tmp->code.for_cycle.condition = exp_b;
  296. for_tmp->code.for_cycle.after = exp_c;
  297. for_tmp->code.for_cycle.done = block_t.data.statement_value;
  298. new_token.type = NON_for;
  299. new_token.data_type = statement_value;
  300. new_token.data.statement_value = for_tmp;
  301. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  302. return;
  303. }
  304. else{
  305. back_one_token(list, for_t);
  306. return;
  307. }
  308. }
  309. /*
  310. while_ : WHILE LB top_exp RB block
  311. */
  312. void while_(int *status, token_node *list){
  313. fprintf(status_log, "[info][grammar] mode status: while_\n");
  314. token while_t, lb_t, exp_t, rb_t, block_t, new_token;
  315. while_t = pop_node(list);
  316. if(while_t.type == WHILE_PASER){
  317. get_pop_token(status, list, lb_t);
  318. if(lb_t.type != LB_PASER){
  319. paser_error("Don't get '('");
  320. }
  321. get_right_token(status,list,top_exp,exp_t);
  322. if(exp_t.type != NON_top_exp){ // 不是表达式
  323. paser_error("Don't get 'top_exp'");
  324. }
  325. get_pop_token(status, list, rb_t);
  326. if(rb_t.type != RB_PASER){
  327. paser_error("Don't get ')'");
  328. }
  329. get_right_token(status,list,block_,block_t);
  330. if(block_t.type != NON_block){ // 不是表达式
  331. paser_error("Don't get '{'");
  332. }
  333. statement *while_tmp = make_statement();
  334. while_tmp->type = while_cycle;
  335. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  336. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  337. new_token.type = NON_while;
  338. new_token.data_type = statement_value;
  339. new_token.data.statement_value = while_tmp;
  340. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  341. return;
  342. }
  343. else{
  344. back_one_token(list, while_t);
  345. return;
  346. }
  347. }
  348. /*
  349. block_ : LP command_list RB
  350. */
  351. void block_(int *status, token_node *list){
  352. fprintf(status_log, "[info][grammar] mode status: block_\n");
  353. token lp_t, rp_t, new_token, command_list_t;
  354. lp_t = pop_node(list);
  355. if(lp_t.type == LP_PASER){
  356. statement *block_tmp = make_statement();
  357. statement_base = append_statement_list(block_tmp, statement_base);
  358. get_right_token(status,list,command_list,command_list_t); // 要把command_list也弹出来
  359. statement_base = free_statement_list(statement_base); // 重新释放
  360. get_pop_token(status, list, rp_t);
  361. if(rp_t.type != RP_PASER){
  362. paser_error("Don't get '}'");
  363. }
  364. new_token.type = NON_block;
  365. new_token.data_type = statement_value;
  366. new_token.data.statement_value = block_tmp;
  367. add_node(list, new_token); // 压入节点
  368. return;
  369. }
  370. else{
  371. back_one_token(list, lp_t);
  372. return;
  373. }
  374. }
  375. /*
  376. top_exp : polynomial
  377. */
  378. void top_exp(int *status, token_node *list){
  379. fprintf(status_log, "[info][grammar] mode status: top_exp\n");
  380. token exp;
  381. get_base_token(status,list,bool_or,exp);
  382. if(exp.type != NON_bool_or){
  383. back_one_token(list, exp);
  384. return;
  385. }
  386. exp.type = NON_top_exp;
  387. add_node(list, exp); // 压入节点
  388. return;
  389. }
  390. /*
  391. bool_or : bool_and
  392. | bool_or AND bool_and
  393. */
  394. void bool_or(int *status, token_node *list){ // 因试分解
  395. fprintf(status_log, "[info][grammar] mode status: bool_or\n");
  396. token left, right, symbol, new_token;
  397. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  398. if(left.type == NON_bool_or){ // 模式2/3
  399. fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
  400. get_pop_token(status, list, symbol);
  401. if(symbol.type == OR_PASER){ // 模式2/3
  402. get_right_token(status, list, bool_and, right); // 回调右边
  403. if(right.type != NON_bool_and){
  404. paser_error("Don't get a compare");
  405. }
  406. // 逻辑操作
  407. new_token.type = NON_bool_or;
  408. new_token.data_type = statement_value;
  409. statement *code_tmp = make_statement();
  410. code_tmp->type = operation;
  411. code_tmp->code.operation.type = OR_func;
  412. code_tmp->code.operation.left_exp = left.data.statement_value;
  413. code_tmp->code.operation.right_exp = right.data.statement_value;
  414. new_token.data.statement_value = code_tmp;
  415. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  416. return bool_or(status, list); // 回调自己
  417. }
  418. else{ // 递归跳出
  419. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  420. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  421. back_one_token(list, left);
  422. back_again(list, symbol);
  423. return;
  424. }
  425. }
  426. else{ // 模式1
  427. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  428. back_one_token(list, left);
  429. get_base_token(status, list, bool_and, new_token);
  430. if(new_token.type != NON_bool_and){
  431. back_one_token(list, new_token); // 往回[不匹配类型]
  432. return;
  433. }
  434. new_token.type = NON_bool_or;
  435. add_node(list, new_token);
  436. return bool_or(status, list); // 回调自己
  437. }
  438. }
  439. /*
  440. bool_and : bool_not
  441. | bool_and AND bool_not
  442. */
  443. void bool_and(int *status, token_node *list){ // 因试分解
  444. fprintf(status_log, "[info][grammar] mode status: bool_and\n");
  445. token left, right, symbol, new_token;
  446. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  447. if(left.type == NON_bool_and){ // 模式2/3
  448. fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
  449. get_pop_token(status, list, symbol);
  450. if(symbol.type == AND_PASER){ // 模式2/3
  451. get_right_token(status, list, bool_not, right); // 回调右边
  452. if(right.type != NON_bool_not){
  453. paser_error("Don't get a bool_not");
  454. }
  455. // 逻辑操作
  456. new_token.type = NON_bool_and;
  457. new_token.data_type = statement_value;
  458. statement *code_tmp = make_statement();
  459. code_tmp->type = operation;
  460. code_tmp->code.operation.type = AND_func;
  461. code_tmp->code.operation.left_exp = left.data.statement_value;
  462. code_tmp->code.operation.right_exp = right.data.statement_value;
  463. new_token.data.statement_value = code_tmp;
  464. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  465. return bool_and(status, list); // 回调自己
  466. }
  467. else{ // 递归跳出
  468. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  469. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  470. back_one_token(list, left);
  471. back_again(list, symbol);
  472. return;
  473. }
  474. }
  475. else{ // 模式1
  476. fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
  477. back_one_token(list, left);
  478. get_base_token(status, list, bool_not, new_token);
  479. if(new_token.type != NON_bool_not){
  480. back_one_token(list, new_token); // 往回[不匹配类型]
  481. return;
  482. }
  483. new_token.type = NON_bool_and;
  484. add_node(list, new_token);
  485. return bool_and(status, list); // 回调自己
  486. }
  487. }
  488. /*
  489. bool_not : compare
  490. | BITNOT compare
  491. */
  492. void bool_not(int *status, token_node *list){
  493. fprintf(status_log, "[info][grammar] mode status: negative\n");
  494. token left, right, new_token;
  495. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  496. if(left.type == NOT_PASER){ // 模式2
  497. fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
  498. get_right_token(status, list, compare, right); // 回调右边
  499. if(right.type != NON_compare){
  500. paser_error("Don't get a compare");
  501. }
  502. // 逻辑操作
  503. new_token.type = NON_bool_not;
  504. new_token.data_type = statement_value;
  505. statement *code_tmp = make_statement();
  506. code_tmp->type = operation;
  507. code_tmp->code.operation.type = NOT_func;
  508. code_tmp->code.operation.left_exp = NULL;
  509. code_tmp->code.operation.right_exp = right.data.statement_value;
  510. new_token.data.statement_value = code_tmp;
  511. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  512. return; // 回调自己
  513. }
  514. else{ // 模式1
  515. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  516. back_one_token(list, left);
  517. get_base_token(status, list, compare, new_token);
  518. if(new_token.type != NON_compare){
  519. back_one_token(list, new_token); // 往回[不匹配类型]
  520. return;
  521. }
  522. new_token.type = NON_bool_not;
  523. add_node(list, new_token);
  524. return;
  525. }
  526. }
  527. void compare(int *status, token_node *list){ // 多项式
  528. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  529. token left, right, symbol, new_token;
  530. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  531. if(left.type == NON_compare){ // 模式2/3
  532. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  533. get_pop_token(status, list, symbol);
  534. if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
  535. symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){ // 模式2/3
  536. get_right_token(status, list, bit_notor, right); // 回调右边
  537. if(right.type != NON_bit_notor){
  538. paser_error("Don't get a bit_notor");
  539. }
  540. new_token.type = NON_compare;
  541. new_token.data_type = statement_value;
  542. statement *code_tmp = make_statement();
  543. code_tmp->type = operation;
  544. if(symbol.type == EQEQ_PASER){
  545. code_tmp->code.operation.type = EQUAL_func;
  546. }
  547. else if(symbol.type == MOREEQ_PASER){
  548. code_tmp->code.operation.type = MOREEQ_func;
  549. }
  550. else if(symbol.type == LESSEQ_PASER){
  551. code_tmp->code.operation.type = LESSEQ_func;
  552. }
  553. else if(symbol.type == MORE_PASER){
  554. code_tmp->code.operation.type = MORE_func;
  555. }
  556. else if(symbol.type == LESS_PASER){
  557. code_tmp->code.operation.type = LESS_func;
  558. }
  559. else{
  560. code_tmp->code.operation.type = NOTEQ_func;
  561. }
  562. code_tmp->code.operation.left_exp = left.data.statement_value;
  563. code_tmp->code.operation.right_exp = right.data.statement_value;
  564. new_token.data.statement_value = code_tmp;
  565. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  566. return compare(status, list); // 回调自己
  567. }
  568. else{ // 递归跳出
  569. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  570. back_one_token(list, left);
  571. back_again(list, symbol);
  572. return;
  573. }
  574. }
  575. else{ // 模式1
  576. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  577. back_one_token(list, left);
  578. get_base_token(status, list, bit_notor, new_token);
  579. if(new_token.type != NON_bit_notor){
  580. back_one_token(list, new_token); // 往回[不匹配类型]
  581. return;
  582. }
  583. new_token.type = NON_compare;
  584. add_node(list, new_token);
  585. return compare(status, list); // 回调自己
  586. }
  587. }
  588. /*
  589. bit_notor : bit_or
  590. | bit_notor BITOR bit_or
  591. */
  592. void bit_notor(int *status, token_node *list){ // 因试分解
  593. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  594. token left, right, symbol, new_token;
  595. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  596. if(left.type == NON_bit_notor){ // 模式2/3
  597. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  598. get_pop_token(status, list, symbol);
  599. if(symbol.type == BITNOTOR_PASER){ // 模式2/3
  600. get_right_token(status, list, bit_or, right); // 回调右边
  601. if(right.type != NON_bit_or){
  602. paser_error("Don't get a bit_or");
  603. }
  604. // 逻辑操作
  605. new_token.type = NON_bit_notor;
  606. new_token.data_type = statement_value;
  607. statement *code_tmp = make_statement();
  608. code_tmp->type = operation;
  609. code_tmp->code.operation.type = BITNOTOR_func;
  610. code_tmp->code.operation.left_exp = left.data.statement_value;
  611. code_tmp->code.operation.right_exp = right.data.statement_value;
  612. new_token.data.statement_value = code_tmp;
  613. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  614. return bit_notor(status, list); // 回调自己
  615. }
  616. else{ // 递归跳出
  617. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  618. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  619. back_one_token(list, left);
  620. back_again(list, symbol);
  621. return;
  622. }
  623. }
  624. else{ // 模式1
  625. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  626. back_one_token(list, left);
  627. get_base_token(status, list, bit_or, new_token);
  628. if(new_token.type != NON_bit_or){
  629. back_one_token(list, new_token); // 往回[不匹配类型]
  630. return;
  631. }
  632. new_token.type = NON_bit_notor;
  633. add_node(list, new_token);
  634. return bit_notor(status, list); // 回调自己
  635. }
  636. }
  637. /*
  638. bit_or : bit_and
  639. | bit_or BITOR bit_and
  640. */
  641. void bit_or(int *status, token_node *list){ // 因试分解
  642. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  643. token left, right, symbol, new_token;
  644. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  645. if(left.type == NON_bit_or){ // 模式2/3
  646. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  647. get_pop_token(status, list, symbol);
  648. if(symbol.type == BITOR_PASER){ // 模式2/3
  649. get_right_token(status, list, bit_and, right); // 回调右边
  650. if(right.type != NON_bit_and){
  651. paser_error("Don't get a bit_and");
  652. }
  653. // 逻辑操作
  654. new_token.type = NON_bit_or;
  655. new_token.data_type = statement_value;
  656. statement *code_tmp = make_statement();
  657. code_tmp->type = operation;
  658. code_tmp->code.operation.type = BITOR_func;
  659. code_tmp->code.operation.left_exp = left.data.statement_value;
  660. code_tmp->code.operation.right_exp = right.data.statement_value;
  661. new_token.data.statement_value = code_tmp;
  662. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  663. return bit_or(status, list); // 回调自己
  664. }
  665. else{ // 递归跳出
  666. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  667. fprintf(status_log, "[info][grammar] (bit_or)out\n");
  668. back_one_token(list, left);
  669. back_again(list, symbol);
  670. return;
  671. }
  672. }
  673. else{ // 模式1
  674. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  675. back_one_token(list, left);
  676. get_base_token(status, list, bit_and, new_token);
  677. if(new_token.type != NON_bit_and){
  678. back_one_token(list, new_token); // 往回[不匹配类型]
  679. return;
  680. }
  681. new_token.type = NON_bit_or;
  682. add_node(list, new_token);
  683. return bit_or(status, list); // 回调自己
  684. }
  685. }
  686. /*
  687. bit_and : bit_move
  688. | bit_and BITAND bit_move
  689. */
  690. void bit_and(int *status, token_node *list){ // 因试分解
  691. fprintf(status_log, "[info][grammar] mode status: bit_and\n");
  692. token left, right, symbol, new_token;
  693. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  694. if(left.type == NON_bit_and){ // 模式2/3
  695. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  696. get_pop_token(status, list, symbol);
  697. if(symbol.type == BITAND_PASER){ // 模式2/3
  698. get_right_token(status, list, bit_move, right); // 回调右边
  699. if(right.type != NON_bit_move){
  700. paser_error("Don't get a bit_move");
  701. }
  702. // 逻辑操作
  703. new_token.type = NON_bit_and;
  704. new_token.data_type = statement_value;
  705. statement *code_tmp = make_statement();
  706. code_tmp->type = operation;
  707. code_tmp->code.operation.type = BITAND_func;
  708. code_tmp->code.operation.left_exp = left.data.statement_value;
  709. code_tmp->code.operation.right_exp = right.data.statement_value;
  710. new_token.data.statement_value = code_tmp;
  711. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  712. return bit_and(status, list); // 回调自己
  713. }
  714. else{ // 递归跳出
  715. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  716. fprintf(status_log, "[info][grammar] (bit_and)out\n");
  717. back_one_token(list, left);
  718. back_again(list, symbol);
  719. return;
  720. }
  721. }
  722. else{ // 模式1
  723. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  724. back_one_token(list, left);
  725. get_base_token(status, list, bit_move, new_token);
  726. if(new_token.type != NON_bit_move){
  727. back_one_token(list, new_token); // 往回[不匹配类型]
  728. return;
  729. }
  730. new_token.type = NON_bit_and;
  731. add_node(list, new_token);
  732. return bit_and(status, list); // 回调自己
  733. }
  734. }
  735. /*
  736. bit_move : power
  737. | bit_move BITRIGHT factor
  738. | bit_move BITLEFT factor
  739. */
  740. void bit_move(int *status, token_node *list){ // 因试分解
  741. fprintf(status_log, "[info][grammar] mode status: factor\n");
  742. token left, right, symbol, new_token;
  743. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  744. if(left.type == NON_bit_move){ // 模式2/3
  745. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  746. get_pop_token(status, list, symbol);
  747. if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
  748. get_right_token(status, list, polynomial, right); // 回调右边
  749. if(right.type != NON_polynomial){
  750. paser_error("Don't get a polynomial");
  751. }
  752. // 逻辑操作
  753. new_token.type = NON_bit_move;
  754. new_token.data_type = statement_value;
  755. statement *code_tmp = make_statement();
  756. code_tmp->type = operation;
  757. if(symbol.type == BITRIGHT_PASER){
  758. code_tmp->code.operation.type = BITRIGHT_func;
  759. }
  760. else{
  761. code_tmp->code.operation.type = BITLEFT_func;
  762. }
  763. code_tmp->code.operation.left_exp = left.data.statement_value;
  764. code_tmp->code.operation.right_exp = right.data.statement_value;
  765. new_token.data.statement_value = code_tmp;
  766. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  767. return bit_move(status, list); // 回调自己
  768. }
  769. else{ // 递归跳出
  770. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  771. fprintf(status_log, "[info][grammar] (bit_move)out\n");
  772. back_one_token(list, left);
  773. back_again(list, symbol);
  774. return;
  775. }
  776. }
  777. else{ // 模式1
  778. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  779. back_one_token(list, left);
  780. get_base_token(status, list, polynomial, new_token);
  781. if(new_token.type != NON_polynomial){
  782. back_one_token(list, new_token); // 往回[不匹配类型]
  783. return;
  784. }
  785. new_token.type = NON_bit_move;
  786. add_node(list, new_token);
  787. return bit_move(status, list); // 回调自己
  788. }
  789. }
  790. /*
  791. polynomial : factor
  792. | polynomial ADD factor
  793. | polynomial SUB factor
  794. */
  795. void polynomial(int *status, token_node *list){ // 多项式
  796. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  797. token left, right, symbol, new_token;
  798. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  799. if(left.type == NON_polynomial){ // 模式2/3
  800. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  801. get_pop_token(status, list, symbol);
  802. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  803. get_right_token(status, list, factor, right); // 回调右边
  804. if(right.type != NON_factor){
  805. paser_error("Don't get a factor");
  806. }
  807. new_token.type = NON_polynomial;
  808. new_token.data_type = statement_value;
  809. statement *code_tmp = make_statement();
  810. code_tmp->type = operation;
  811. if(symbol.type == ADD_PASER){
  812. code_tmp->code.operation.type = ADD_func;
  813. }
  814. else{
  815. code_tmp->code.operation.type = SUB_func;
  816. }
  817. code_tmp->code.operation.left_exp = left.data.statement_value;
  818. code_tmp->code.operation.right_exp = right.data.statement_value;
  819. new_token.data.statement_value = code_tmp;
  820. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  821. return polynomial(status, list); // 回调自己
  822. }
  823. else{ // 递归跳出
  824. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  825. back_one_token(list, left);
  826. back_again(list, symbol);
  827. return;
  828. }
  829. }
  830. else{ // 模式1
  831. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  832. back_one_token(list, left);
  833. get_base_token(status, list, factor, new_token);
  834. if(new_token.type != NON_factor){
  835. back_one_token(list, new_token); // 往回[不匹配类型]
  836. return;
  837. }
  838. new_token.type = NON_polynomial;
  839. add_node(list, new_token);
  840. return polynomial(status, list); // 回调自己
  841. }
  842. }
  843. /*
  844. factor : power
  845. | factor MUL power
  846. | factor DIV power
  847. */
  848. void factor(int *status, token_node *list){ // 因试分解
  849. fprintf(status_log, "[info][grammar] mode status: factor\n");
  850. token left, right, symbol, new_token;
  851. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  852. if(left.type == NON_factor){ // 模式2/3
  853. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  854. get_pop_token(status, list, symbol);
  855. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
  856. get_right_token(status, list, negative, right); // 回调右边
  857. if(right.type != NON_negative){
  858. paser_error("Don't get a value");
  859. }
  860. // 逻辑操作
  861. new_token.type = NON_factor;
  862. new_token.data_type = statement_value;
  863. statement *code_tmp = make_statement();
  864. code_tmp->type = operation;
  865. if(symbol.type == MUL_PASER){
  866. code_tmp->code.operation.type = MUL_func;
  867. }
  868. else{
  869. code_tmp->code.operation.type = DIV_func;
  870. }
  871. code_tmp->code.operation.left_exp = left.data.statement_value;
  872. code_tmp->code.operation.right_exp = right.data.statement_value;
  873. new_token.data.statement_value = code_tmp;
  874. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  875. return factor(status, list); // 回调自己
  876. }
  877. else{ // 递归跳出
  878. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  879. fprintf(status_log, "[info][grammar] (factor)out\n");
  880. back_one_token(list, left);
  881. back_again(list, symbol);
  882. return;
  883. }
  884. }
  885. else{ // 模式1
  886. fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
  887. back_one_token(list, left);
  888. get_base_token(status, list, negative, new_token);
  889. if(new_token.type != NON_negative){
  890. back_one_token(list, new_token); // 往回[不匹配类型]
  891. return;
  892. }
  893. new_token.type = NON_factor;
  894. add_node(list, new_token);
  895. return factor(status, list); // 回调自己
  896. }
  897. }
  898. /*
  899. negative : bit_not
  900. | BITNOT bit_not
  901. */
  902. void negative(int *status, token_node *list){
  903. fprintf(status_log, "[info][grammar] mode status: negative\n");
  904. token left, right, new_token;
  905. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  906. if(left.type == SUB_PASER){ // 模式2
  907. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  908. get_right_token(status, list, bit_not, right); // 回调右边
  909. if(right.type != NON_bit_not){
  910. paser_error("Don't get a bit_not");
  911. }
  912. // 逻辑操作
  913. new_token.type = NON_negative;
  914. new_token.data_type = statement_value;
  915. statement *code_tmp = make_statement();
  916. code_tmp->type = operation;
  917. code_tmp->code.operation.type = NEGATIVE_func;
  918. code_tmp->code.operation.left_exp = NULL;
  919. code_tmp->code.operation.right_exp = right.data.statement_value;
  920. new_token.data.statement_value = code_tmp;
  921. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  922. return; // 回调自己
  923. }
  924. else{ // 模式1
  925. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  926. back_one_token(list, left);
  927. get_base_token(status, list, bit_not, new_token);
  928. if(new_token.type != NON_bit_not){
  929. back_one_token(list, new_token); // 往回[不匹配类型]
  930. return;
  931. }
  932. new_token.type = NON_negative;
  933. add_node(list, new_token);
  934. return;
  935. }
  936. }
  937. /*
  938. bit_not : power
  939. | BITNOT power
  940. */
  941. void bit_not(int *status, token_node *list){
  942. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  943. token left, right, new_token;
  944. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  945. if(left.type == BITNOT_PASER){ // 模式2
  946. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  947. get_right_token(status, list, power, right); // 回调右边
  948. if(right.type != NON_power){
  949. paser_error("Don't get a power");
  950. }
  951. // 逻辑操作
  952. new_token.type = NON_bit_not;
  953. new_token.data_type = statement_value;
  954. statement *code_tmp = make_statement();
  955. code_tmp->type = operation;
  956. code_tmp->code.operation.type = BITNOT_func;
  957. code_tmp->code.operation.left_exp = NULL;
  958. code_tmp->code.operation.right_exp = right.data.statement_value;
  959. new_token.data.statement_value = code_tmp;
  960. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  961. return; // 回调自己
  962. }
  963. else{ // 模式1
  964. fprintf(status_log, "[info][grammar] (bit_not)back one token to (power)\n");
  965. back_one_token(list, left);
  966. get_base_token(status, list, power, new_token);
  967. if(new_token.type != NON_power){
  968. back_one_token(list, new_token); // 往回[不匹配类型]
  969. return;
  970. }
  971. new_token.type = NON_bit_not;
  972. add_node(list, new_token);
  973. return;
  974. }
  975. }
  976. /*
  977. power : element
  978. | power POW element
  979. | power LOG element
  980. | power SQRT element
  981. */
  982. void power(int *status, token_node *list){
  983. fprintf(status_log, "[info][grammar] mode status: power\n");
  984. token left, right, symbol, new_token;
  985. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  986. if(left.type == NON_power){ // 模式2/3
  987. fprintf(status_log, "[info][grammar] (power)reduce right\n");
  988. get_pop_token(status, list, symbol);
  989. if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
  990. get_right_token(status, list, element, right); // 回调右边
  991. if(right.type != NON_element){
  992. paser_error("Don't get a value");
  993. }
  994. // 逻辑操作
  995. new_token.type = NON_power;
  996. new_token.data_type = statement_value;
  997. statement *code_tmp = make_statement();
  998. code_tmp->type = operation;
  999. if(symbol.type == POW_PASER){
  1000. code_tmp->code.operation.type = POW_func;
  1001. }
  1002. else if(symbol.type == LOG_PASER){
  1003. code_tmp->code.operation.type = LOG_func;
  1004. }
  1005. else{
  1006. code_tmp->code.operation.type = SQRT_func;
  1007. }
  1008. code_tmp->code.operation.left_exp = left.data.statement_value;
  1009. code_tmp->code.operation.right_exp = right.data.statement_value;
  1010. new_token.data.statement_value = code_tmp;
  1011. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1012. return power(status, list); // 回调自己
  1013. }
  1014. else{ // 递归跳出
  1015. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1016. fprintf(status_log, "[info][grammar] (power)out\n");
  1017. back_one_token(list, left);
  1018. back_again(list, symbol);
  1019. return;
  1020. }
  1021. }
  1022. else{ // 模式1
  1023. fprintf(status_log, "[info][grammar] (power)back one token to (element)\n");
  1024. back_one_token(list, left);
  1025. get_base_token(status, list, element, new_token);
  1026. if(new_token.type != NON_element){
  1027. back_one_token(list, new_token); // 往回[不匹配类型]
  1028. return;
  1029. }
  1030. new_token.type = NON_power;
  1031. add_node(list, new_token);
  1032. return power(status, list); // 回调自己
  1033. }
  1034. }
  1035. /*
  1036. element : number
  1037. | LB top_exp RB
  1038. */
  1039. void element(int *status, token_node *list){ // 数字归约
  1040. fprintf(status_log, "[info][grammar] mode status: element\n");
  1041. token gett, new_token;
  1042. gett = pop_node(list); // 取得一个token
  1043. if(gett.type == LB_PASER){ // 模式3
  1044. fprintf(status_log, "[info][grammar] (element)get LB\n");
  1045. get_right_token(status, list, top_exp, new_token);
  1046. if(new_token.type != NON_top_exp){
  1047. paser_error("Don't get 'top_exp'");
  1048. }
  1049. token rb;
  1050. get_pop_token(status, list ,rb);
  1051. if(rb.type != RB_PASER){ // 匹配失败
  1052. paser_error("Don't get ')'");
  1053. }
  1054. new_token.type = NON_element;
  1055. add_node(list, new_token); // 压入节点
  1056. return;
  1057. }
  1058. else if(gett.type == VAR_PASER){ // a
  1059. back_one_token(list, gett);
  1060. get_base_token(status, list, var_token, new_token);
  1061. if(new_token.type != NON_base_var){
  1062. back_one_token(list, new_token); // 往回[不匹配类型]
  1063. return;
  1064. }
  1065. new_token.type = NON_element;
  1066. add_node(list, new_token);
  1067. return;
  1068. }
  1069. else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
  1070. back_one_token(list, gett);
  1071. token exp_token, rb, tmp_var;
  1072. get_right_token(status, list, top_exp, exp_token);
  1073. if(exp_token.type != NON_top_exp){
  1074. paser_error("Don't get 'top_exp'");
  1075. }
  1076. get_pop_token(status, list ,rb);
  1077. if(rb.type != RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
  1078. paser_error("Don't get ']'");
  1079. }
  1080. get_pop_token(status, list ,tmp_var);
  1081. if(tmp_var.type == VAR_PASER){ // a
  1082. back_one_token(list, tmp_var);
  1083. get_base_token(status, list, var_token, new_token);
  1084. if(new_token.type != NON_base_var){
  1085. paser_error("Don't get var");
  1086. }
  1087. new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
  1088. }
  1089. new_token.type = NON_element;
  1090. add_node(list, new_token);
  1091. return;
  1092. }
  1093. else{
  1094. fprintf(status_log, "[info][grammar] (element)back one token to (number)\n");
  1095. back_one_token(list, gett);
  1096. get_base_token(status, list, number, new_token);
  1097. if(new_token.type != NON_base_value){
  1098. back_one_token(list, new_token); // 往回[不匹配类型]
  1099. return;
  1100. }
  1101. new_token.type = NON_element;
  1102. add_node(list, new_token);
  1103. return;
  1104. }
  1105. }
  1106. /*
  1107. var_token : VAR
  1108. */
  1109. void var_token(int *status, token_node *list){ // 数字归约
  1110. fprintf(status_log, "[info][grammar] mode status: var_token\n");
  1111. token gett, new_token;
  1112. gett = pop_node(list); // 取得一个token
  1113. if(gett.type == VAR_PASER){ // var类型
  1114. new_token.type = NON_base_var;
  1115. GWARF_value tmp_value;
  1116. tmp_value.type = INT_value;
  1117. tmp_value.value.int_value = atoi(gett.data.text);
  1118. statement *code_tmp = make_statement();
  1119. code_tmp->type = base_var;
  1120. code_tmp->code.base_var.var_name = gett.data.text;
  1121. code_tmp->code.base_var.from = NULL;
  1122. new_token.data.statement_value = code_tmp;
  1123. new_token.data_type = statement_value;
  1124. fprintf(status_log, "[info][grammar] (var_token)out\n");
  1125. add_node(list, new_token); // 压入节点
  1126. return;
  1127. }
  1128. else{ // 不是期望值
  1129. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  1130. back_one_token(list, gett);
  1131. return;
  1132. }
  1133. }
  1134. /*
  1135. number : INT_PASER
  1136. | DOUBLE_PASER
  1137. | LB top_exp RB
  1138. */
  1139. void number(int *status, token_node *list){ // 数字归约
  1140. fprintf(status_log, "[info][grammar] mode status: number\n");
  1141. token gett, new_token;
  1142. gett = pop_node(list); // 取得一个token
  1143. if(gett.type == INT_PASER){ // int类型
  1144. new_token.type = NON_base_value;
  1145. GWARF_value tmp_value;
  1146. tmp_value.type = INT_value;
  1147. tmp_value.value.int_value = atoi(gett.data.text);
  1148. statement *code_tmp = make_statement();
  1149. code_tmp->type = call;
  1150. code_tmp->code.call.func = pack_call_name("int", NULL);
  1151. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1152. new_token.data.statement_value = code_tmp;
  1153. new_token.data_type = statement_value;
  1154. fprintf(status_log, "[info][grammar] (number)get int number: %d\n", tmp_value.value.int_value);
  1155. }
  1156. else if(gett.type == DOUBLE_PASER){
  1157. new_token.type = NON_base_value;
  1158. GWARF_value tmp_value;
  1159. tmp_value.type = NUMBER_value;
  1160. tmp_value.value.double_value = atof(gett.data.text);
  1161. statement *code_tmp = make_statement();
  1162. code_tmp->type = call;
  1163. code_tmp->code.call.func = pack_call_name("double", NULL);
  1164. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1165. new_token.data.statement_value = code_tmp;
  1166. new_token.data_type = statement_value;
  1167. fprintf(status_log, "[info][grammar] (number)get double number: %f\n", new_token.data.d_number);
  1168. }
  1169. else{ // 不是期望值
  1170. fprintf(status_log, "[info][grammar] (number)back one token\n");
  1171. back_one_token(list, gett);
  1172. return;
  1173. }
  1174. free(gett.data.text); // 释放字符串
  1175. fprintf(status_log, "[info][grammar] (number)add one token\n");
  1176. add_node(list, new_token); // 压入节点
  1177. }
  1178. void paser_error(char *text){
  1179. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  1180. printf("[error][grammar] paser error : %s\n\n", text);
  1181. exit(1);
  1182. }