syntax.c 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897
  1. #include"token.h"
  2. #include"lex.h"
  3. #include"../inter/interpreter.h"
  4. void factor(p_status *status, token_node *list);
  5. void power(p_status *status, token_node *list);
  6. void call_down(p_status *status, token_node *list);
  7. void number(p_status *status, token_node *list);
  8. void element(p_status *status, token_node *list);
  9. void var_token(p_status *status, token_node *list);
  10. void polynomial(p_status *status, token_node *list);
  11. void bit_move(p_status *status, token_node *list);
  12. void command(p_status *status, token_node *list);
  13. void while_(p_status *status, token_node *list);
  14. void if_(p_status *status, token_node *list);
  15. void for_(p_status *status, token_node *list);
  16. void elif_(p_status *status, token_node *list);
  17. void block_(p_status *status, token_node *list);
  18. void top_exp(p_status *status, token_node *list);
  19. void negative(p_status *status, token_node *list);
  20. void bit_not(p_status *status, token_node *list);
  21. void bit_notor(p_status *status, token_node *list);
  22. void bit_or(p_status *status, token_node *list);
  23. void bit_and(p_status *status, token_node *list);
  24. void compare(p_status *status, token_node *list);
  25. void bool_and(p_status *status, token_node *list);
  26. void bool_or(p_status *status, token_node *list);
  27. void bool_not(p_status *status, token_node *list);
  28. void eq_number(p_status *status, token_node *list);
  29. void call_back_(p_status *status, token_node *list);
  30. void def_(p_status *status, token_node *list);
  31. void ctrl_(p_status *status, token_node *list);
  32. void var_ctrl_(p_status *status, token_node *list);
  33. void return_(p_status *status, token_node *list);
  34. void formal_parameter(p_status *status, token_node *list);
  35. void paser_error(char *text);
  36. /*
  37. command_list : command
  38. | command_list command
  39. */
  40. void command_list(p_status *status, token_node *list){ // 多项式
  41. fprintf(status_log, "[info][grammar] mode status: command_list\n", text);
  42. token left, right, new_token;
  43. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  44. if(left.type == NON_command_list){ // 模式2
  45. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  46. get_right_token(status, list, command, right); // 回调右边
  47. if(right.type == NON_command){
  48. new_token.type = NON_command_list;
  49. new_token.data_type = empty;
  50. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  51. return command_list(status, list); // 回调自己
  52. }
  53. else{ // 递归跳出[EOF_token]
  54. printf("right.type = %d\n", right.type);
  55. fprintf(status_log, "[info][grammar] (command_list)out\n");
  56. back_one_token(list, left);
  57. back_again(list, right);
  58. return;
  59. }
  60. }
  61. else if(left.type == EOF_token){ // 递归跳出的条件
  62. fprintf(status_log, "[info][grammar] (command_list)out again\n");
  63. back_one_token(list, left);
  64. return;
  65. }
  66. else{ // 模式1
  67. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  68. back_one_token(list, left);
  69. get_base_token(status, list, command, new_token);
  70. if(new_token.type != NON_command){
  71. back_one_token(list, new_token); // 往回[不匹配类型]
  72. return;
  73. }
  74. new_token.type = NON_command_list;
  75. add_node(list, new_token);
  76. return command_list(status, list); // 回调自己
  77. }
  78. }
  79. /*
  80. command : top_exp <ENTER>
  81. */
  82. void command(p_status *status, token_node *list){ // 多项式
  83. fprintf(status_log, "[info][grammar] mode status: command\n", text);
  84. token left, new_token;
  85. left = pop_node(list); // 先弹出一个token 检查token
  86. if(left.type == WHILE_PASER){ // 是while类型的数据
  87. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  88. back_one_token(list, left);
  89. get_base_token(status, list, while_, new_token);
  90. get_stop_token();
  91. push_statement(statement_base, new_token);
  92. }
  93. else if(left.type == IF_PASER){
  94. fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
  95. back_one_token(list, left);
  96. get_base_token(status, list, if_, new_token);
  97. get_stop_token();
  98. push_statement(statement_base, new_token);
  99. }
  100. else if(left.type == FOR_PASER){
  101. fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
  102. back_one_token(list, left);
  103. get_base_token(status, list, for_, new_token);
  104. get_stop_token();
  105. push_statement(statement_base, new_token);
  106. }
  107. else if(left.type == DEF_PASER){
  108. fprintf(status_log, "[info][grammar] (command)back one token to (def_)\n");
  109. back_one_token(list, left);
  110. get_base_token(status, list, def_, new_token);
  111. get_stop_token();
  112. push_statement(statement_base, new_token);
  113. }
  114. else if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  115. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  116. fprintf(status_log, "[info][grammar] (command)back one token to (ctrl_)\n");
  117. back_one_token(list, left);
  118. get_base_token(status, list, ctrl_, new_token);
  119. get_stop_token();
  120. push_statement(statement_base, new_token);
  121. }
  122. else if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  123. fprintf(status_log, "[info][grammar] (command)back one token to (var_ctrl_)\n");
  124. back_one_token(list, left);
  125. get_base_token(status, list, var_ctrl_, new_token);
  126. get_stop_token();
  127. push_statement(statement_base, new_token);
  128. }
  129. else if(left.type == RETURN_PASER){
  130. fprintf(status_log, "[info][grammar] (command)back one token to (return_)\n");
  131. back_one_token(list, left);
  132. get_base_token(status, list, return_, new_token);
  133. get_stop_token();
  134. push_statement(statement_base, new_token);
  135. }
  136. else if(left.type == ENTER_PASER){
  137. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  138. }
  139. else if(left.type == EOF_token){
  140. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  141. back_one_token(list, left);
  142. goto return_back;
  143. }
  144. else{ // 表达式
  145. fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
  146. back_one_token(list, left);
  147. get_base_token(status, list, top_exp, new_token);
  148. if(new_token.type != NON_top_exp){
  149. back_one_token(list, new_token); // 往回[不匹配类型]
  150. return;
  151. }
  152. get_stop_token();
  153. push_statement(statement_base, new_token);
  154. }
  155. new_token.type = NON_command;
  156. add_node(list, new_token);
  157. return_back:
  158. return; // 回调自己
  159. }
  160. /*
  161. if_ : IF LB top_exp RB block
  162. */
  163. void if_(p_status *status, token_node *list){
  164. fprintf(status_log, "[info][grammar] mode status: if_\n");
  165. token if_t, lb_t, exp_t, rb_t, block_t, next_t, child_t, new_token;
  166. if_t = pop_node(list);
  167. if(if_t.type == IF_PASER){
  168. get_pop_token(status, list, lb_t);
  169. if(lb_t.type != LB_PASER){
  170. paser_error("Don't get '('");
  171. }
  172. get_right_token(status,list,top_exp,exp_t);
  173. if(exp_t.type != NON_top_exp){ // 不是表达式
  174. paser_error("Don't get 'top_exp'");
  175. }
  176. get_pop_token(status, list, rb_t);
  177. if(rb_t.type != RB_PASER){
  178. paser_error("Don't get ')'");
  179. }
  180. get_right_token(status,list,block_,block_t);
  181. if(block_t.type != NON_block){ // 不是表达式
  182. paser_error("Don't get '{'");
  183. }
  184. statement *if_tmp = make_statement();
  185. if_tmp->type = if_branch;
  186. if_tmp->code.if_branch.done = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  187. // 检查else和elseif
  188. el_again:
  189. get_pop_token(status,list,next_t);
  190. if(next_t.type == ENTER_PASER){ // 忽略Enter
  191. goto el_again;
  192. }
  193. printf("next_t.type = %d\n", next_t.type);
  194. if(next_t.type == ELIF_PASER || next_t.type == ELSE_PASER){ // elif
  195. back_one_token(list, next_t);
  196. get_base_token(status,list,elif_,child_t);
  197. if(child_t.type == NON_elif){
  198. append_elif(child_t.data.if_list_base, if_tmp->code.if_branch.done);
  199. goto el_again;
  200. }
  201. }
  202. else{
  203. back_again(list, next_t); // 下一次读取需要用safe_get_token
  204. }
  205. new_token.type = NON_if;
  206. new_token.data_type = statement_value;
  207. new_token.data.statement_value = if_tmp;
  208. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  209. token tmp_enter;
  210. tmp_enter.type = ENTER_PASER;
  211. tmp_enter.data_type = empty;
  212. back_again(list, tmp_enter); // push入一个ENTER
  213. return;
  214. }
  215. else{
  216. back_one_token(list, if_t);
  217. return;
  218. }
  219. }
  220. /*
  221. elif_ : ELIF LB top_exp RB block
  222. */
  223. void elif_(p_status *status, token_node *list){
  224. fprintf(status_log, "[info][grammar] mode status: elif_\n");
  225. token elif_t, lb_t, exp_t, rb_t, block_t, next_t, new_token;
  226. elif_t = pop_node(list);
  227. if(elif_t.type == ELIF_PASER){
  228. get_pop_token(status, list, lb_t);
  229. if(lb_t.type != LB_PASER){
  230. paser_error("Don't get '('");
  231. }
  232. get_right_token(status,list,top_exp,exp_t);
  233. if(exp_t.type != NON_top_exp){ // 不是表达式
  234. paser_error("Don't get 'top_exp'");
  235. }
  236. get_pop_token(status, list, rb_t);
  237. if(rb_t.type != RB_PASER){
  238. paser_error("Don't get ')'");
  239. }
  240. get_right_token(status,list,block_,block_t);
  241. if(block_t.type != NON_block){ // 不是表达式
  242. paser_error("Don't get '{'");
  243. }
  244. if_list *if_tmp = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  245. new_token.type = NON_elif;
  246. new_token.data_type = if_list_base;
  247. new_token.data.if_list_base = if_tmp;
  248. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  249. return;
  250. }
  251. if(elif_t.type == ELSE_PASER){ // else
  252. get_right_token(status,list,block_,block_t);
  253. if(block_t.type != NON_block){ // 不是表达式
  254. paser_error("Don't get '{'");
  255. }
  256. if_list *if_tmp = make_if(NULL, block_t.data.statement_value);
  257. new_token.type = NON_elif;
  258. new_token.data_type = if_list_base;
  259. new_token.data.if_list_base = if_tmp;
  260. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  261. return;
  262. }
  263. else{
  264. back_one_token(list, elif_t);
  265. return;
  266. }
  267. }
  268. /*
  269. for_ : FOR LB top_exp COMMA top_exp COMMA top_exp RB block
  270. */
  271. void for_(p_status *status, token_node *list){
  272. fprintf(status_log, "[info][grammar] mode status: while_\n");
  273. token for_t, exp_1, exp_2, exp_3, block_t, lb_t, rb_t, comma_t,new_token;
  274. statement *exp_a, *exp_b, *exp_c;
  275. for_t = pop_node(list);
  276. if(for_t.type == FOR_PASER){
  277. get_pop_token(status, list, lb_t);
  278. if(lb_t.type != LB_PASER){
  279. paser_error("Don't get '('");
  280. }
  281. get_pop_token(status, list, exp_1);
  282. if(exp_1.type == COMMA_PASER){
  283. exp_a = NULL; // exp_1 = NULL;
  284. }
  285. else{
  286. back_one_token(list, exp_1);
  287. get_base_token(status,list,top_exp,exp_1); // 不是使用right token,不需要执行safe_get_token
  288. if(exp_1.type != NON_top_exp){ // 不是表达式
  289. paser_error("Don't get 'top_exp'");
  290. }
  291. get_pop_token(status, list, comma_t);
  292. if(comma_t.type != COMMA_PASER){
  293. paser_error("Don't get ';' in for cycle");
  294. }
  295. exp_a = exp_1.data.statement_value;
  296. }
  297. get_pop_token(status, list, exp_2);
  298. if(exp_2.type == COMMA_PASER){
  299. exp_b = NULL; // exp_1 = NULL;
  300. }
  301. else{
  302. back_one_token(list, exp_2);
  303. get_base_token(status,list,top_exp,exp_2); // 不是使用right token,不需要执行safe_get_token
  304. if(exp_2.type != NON_top_exp){ // 不是表达式
  305. paser_error("Don't get 'top_exp'");
  306. }
  307. get_pop_token(status, list, comma_t);
  308. if(comma_t.type != COMMA_PASER){
  309. paser_error("Don't get ';' in for cycle");
  310. }
  311. exp_b = exp_2.data.statement_value;
  312. }
  313. get_pop_token(status, list, exp_3);
  314. if(exp_3.type == RB_PASER){
  315. exp_c = NULL; // exp_1 = NULL;
  316. back_one_token(list, exp_3);
  317. }
  318. else{
  319. back_one_token(list, exp_3);
  320. get_base_token(status,list,top_exp,exp_3); // 不是使用right token,不需要执行safe_get_token
  321. if(exp_3.type != NON_top_exp){ // 不是表达式
  322. paser_error("Don't get 'top_exp'");
  323. }
  324. exp_c = exp_3.data.statement_value;
  325. }
  326. get_pop_token(status, list, rb_t);
  327. if(rb_t.type != RB_PASER){
  328. paser_error("Don't get ')'");
  329. }
  330. get_right_token(status,list,block_,block_t);
  331. if(block_t.type != NON_block){ // 不是表达式
  332. paser_error("Don't get '{'");
  333. }
  334. statement *for_tmp = make_statement();
  335. for_tmp->type = for_cycle;
  336. for_tmp->code.for_cycle.first = exp_a;
  337. for_tmp->code.for_cycle.condition = exp_b;
  338. for_tmp->code.for_cycle.after = exp_c;
  339. for_tmp->code.for_cycle.done = block_t.data.statement_value;
  340. new_token.type = NON_for;
  341. new_token.data_type = statement_value;
  342. new_token.data.statement_value = for_tmp;
  343. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  344. return;
  345. }
  346. else{
  347. back_one_token(list, for_t);
  348. return;
  349. }
  350. }
  351. /*
  352. def_ : DEF LB RB block
  353. */
  354. void def_(p_status *status, token_node *list){
  355. fprintf(status_log, "[info][grammar] mode status: def_\n");
  356. token def_t, lb_t, rb_t, block_t, name_t, parameter_t, new_token;
  357. parameter *p_list;
  358. def_t = pop_node(list);
  359. if(def_t.type == DEF_PASER){
  360. status->is_func = true;
  361. get_right_token(status,list,top_exp,name_t); // 避免了top_exp把括号捕捉为call_back,不过,可以使用list设置status参数从而让call_back不捕捉[未实现]
  362. status->is_func = false;
  363. if(name_t.type != NON_top_exp){ // 不是表达式
  364. paser_error("Don't get 'top_exp'");
  365. }
  366. get_pop_token(status, list, lb_t);
  367. if(lb_t.type != LB_PASER){
  368. printf("lb_t.type = %d\n", lb_t.type);
  369. paser_error("Don't get '('");
  370. }
  371. get_pop_token(status, list, rb_t);
  372. if(rb_t.type != RB_PASER){ // 带参数
  373. back_one_token(list, rb_t);
  374. get_base_token(status,list,formal_parameter,parameter_t); // 不需要safe_get_token
  375. if(parameter_t.type != NON_parameter){
  376. paser_error("Don't get formal_parameter");
  377. }
  378. get_pop_token(status, list, rb_t);
  379. printf("rb_t.type = %d\n", rb_t.type);
  380. if(rb_t.type != RB_PASER){
  381. paser_error("Don't get ')'[1]");
  382. }
  383. p_list = parameter_t.data.parameter_list;
  384. }
  385. else{
  386. p_list = NULL; // 没参数
  387. }
  388. get_right_token(status,list,block_,block_t);
  389. if(block_t.type != NON_block){ // 不是表达式
  390. paser_error("Don't get '{'");
  391. }
  392. statement *def_tmp = make_statement();
  393. def_tmp->type = def;
  394. def_tmp->code.def.var = name_t.data.statement_value;
  395. def_tmp->code.def.parameter_list = p_list;
  396. def_tmp->code.def.done = block_t.data.statement_value;
  397. new_token.type = NON_def;
  398. new_token.data_type = statement_value;
  399. new_token.data.statement_value = def_tmp;
  400. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  401. return;
  402. }
  403. else{
  404. back_one_token(list, def_t);
  405. return;
  406. }
  407. }
  408. void formal_parameter(p_status *status, token_node *list){ // 因试分解
  409. fprintf(status_log, "[info][grammar] mode status: formal_parameter\n");
  410. token left, next, comma, before, eq, value_token, new_token;
  411. int mode = only_value;
  412. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  413. if(left.type == NON_parameter){ // 模式2/3
  414. fprintf(status_log, "[info][grammar] (formal_parameter)reduce right\n");
  415. get_pop_token(status, list, comma);
  416. if(comma.type == COMMA_PASER){
  417. get_pop_token(status, list, before);
  418. if(before.type == MUL_PASER){
  419. mode = put_args;
  420. }
  421. else if(before.type == POW_PASER){
  422. mode = put_kwargs;
  423. }
  424. else{
  425. mode = only_value;
  426. back_again(list,before); // 回退
  427. }
  428. status->is_parameter = true;
  429. get_right_token(status, list, top_exp, next);
  430. status->is_parameter = false;
  431. if(next.type != NON_top_exp){
  432. paser_error("Don't get a top_exp");
  433. return;
  434. }
  435. new_token = left;
  436. parameter *tmp = NULL;
  437. get_pop_token(status, list, eq);
  438. if(eq.type == EQ_PASER){ // name_value模式
  439. get_right_token(status, list, top_exp, value_token);
  440. if(value_token.type != NON_top_exp){
  441. paser_error("Don't get a top_exp");
  442. return;
  443. }
  444. tmp = append_parameter_name(next.data.statement_value, new_token.data.parameter_list);
  445. tmp->type = name_value;
  446. tmp->u.value = value_token.data.statement_value;
  447. }
  448. else{
  449. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  450. tmp = append_parameter_value(next.data.statement_value, new_token.data.parameter_list);
  451. tmp->type = mode;
  452. tmp->u.var = tmp->u.value;
  453. }
  454. add_node(list, new_token);
  455. return formal_parameter(status, list); // 回调自己
  456. }
  457. else{ // 递归跳出
  458. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  459. fprintf(status_log, "[info][grammar] (formal_parameter)out\n");
  460. back_one_token(list, left);
  461. back_again(list, comma);
  462. return;
  463. }
  464. }
  465. else if(left.type == POW_PASER || left.type == MUL_PASER){ // 模式1
  466. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)[**/*]\n");
  467. status->is_parameter = true;
  468. get_right_token(status, list, top_exp, next); // 不需要back_one_token
  469. status->is_parameter = false;
  470. if(next.type != NON_top_exp){
  471. back_one_token(list, next); // 往回[不匹配类型]
  472. return;
  473. }
  474. new_token.type = NON_parameter;
  475. new_token.data_type = parameter_list;
  476. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  477. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  478. if(left.type == POW_PASER){
  479. new_token.data.parameter_list->type = put_kwargs;
  480. }
  481. else{
  482. new_token.data.parameter_list->type = put_args;
  483. }
  484. add_node(list, new_token);
  485. return formal_parameter(status, list); // 回调自己
  486. }
  487. else{ // 模式1
  488. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)\n");
  489. back_one_token(list, left);
  490. status->is_parameter = true;
  491. get_base_token(status, list, top_exp, next);
  492. status->is_parameter = false;
  493. if(next.type != NON_top_exp){
  494. back_one_token(list, next); // 往回[不匹配类型]
  495. return;
  496. }
  497. new_token.type = NON_parameter;
  498. new_token.data_type = parameter_list;
  499. parameter *tmp = NULL;
  500. get_pop_token(status, list, eq);
  501. if(eq.type == EQ_PASER){ // name_value模式
  502. get_right_token(status, list, top_exp, value_token);
  503. if(value_token.type != NON_top_exp){
  504. paser_error("Don't get a top_exp");
  505. return;
  506. }
  507. puts("DEFAULT");
  508. printf("next.data.statement_value.type = %d\n", next.data.statement_value->type);
  509. new_token.data.parameter_list = make_parameter_name(next.data.statement_value);
  510. new_token.data.parameter_list->u.value = value_token.data.statement_value;
  511. new_token.data.parameter_list->type = name_value;
  512. }
  513. else{
  514. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  515. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  516. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  517. new_token.data.parameter_list->type = only_value;
  518. }
  519. add_node(list, new_token);
  520. return formal_parameter(status, list); // 回调自己
  521. }
  522. }
  523. /*
  524. while_ : WHILE LB top_exp RB block
  525. */
  526. void while_(p_status *status, token_node *list){
  527. fprintf(status_log, "[info][grammar] mode status: while_\n");
  528. token while_t, lb_t, exp_t, rb_t, block_t, new_token;
  529. while_t = pop_node(list);
  530. if(while_t.type == WHILE_PASER){
  531. get_pop_token(status, list, lb_t);
  532. if(lb_t.type != LB_PASER){
  533. paser_error("Don't get '('");
  534. }
  535. get_right_token(status,list,top_exp,exp_t);
  536. if(exp_t.type != NON_top_exp){ // 不是表达式
  537. paser_error("Don't get 'top_exp'");
  538. }
  539. get_pop_token(status, list, rb_t);
  540. if(rb_t.type != RB_PASER){
  541. paser_error("Don't get ')'");
  542. }
  543. get_right_token(status,list,block_,block_t);
  544. if(block_t.type != NON_block){ // 不是表达式
  545. paser_error("Don't get '{'");
  546. }
  547. statement *while_tmp = make_statement();
  548. while_tmp->type = while_cycle;
  549. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  550. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  551. new_token.type = NON_while;
  552. new_token.data_type = statement_value;
  553. new_token.data.statement_value = while_tmp;
  554. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  555. return;
  556. }
  557. else{
  558. back_one_token(list, while_t);
  559. return;
  560. }
  561. }
  562. /*
  563. block_ : LP command_list RB
  564. */
  565. void block_(p_status *status, token_node *list){
  566. fprintf(status_log, "[info][grammar] mode status: block_\n");
  567. token lp_t, rp_t, new_token, command_list_t;
  568. lp_t = pop_node(list);
  569. again:
  570. if(lp_t.type == LP_PASER){
  571. statement *block_tmp = make_statement();
  572. statement_base = append_statement_list(block_tmp, statement_base);
  573. get_right_token(status,list,command_list,command_list_t); // 要把command_list也弹出来
  574. statement_base = free_statement_list(statement_base); // 重新释放
  575. get_pop_token(status, list, rp_t);
  576. if(rp_t.type != RP_PASER){
  577. paser_error("Don't get '}'");
  578. }
  579. new_token.type = NON_block;
  580. new_token.data_type = statement_value;
  581. new_token.data.statement_value = block_tmp;
  582. add_node(list, new_token); // 压入节点
  583. fprintf(status_log, "[tag 1]\n");
  584. return;
  585. }
  586. if(lp_t.type == ENTER_PASER){
  587. get_pop_token(status,list,lp_t);
  588. goto again;
  589. }
  590. else{
  591. printf("lp_t.type = %d != %d\n", lp_t.type, LP_PASER);
  592. back_one_token(list, lp_t);
  593. return;
  594. }
  595. }
  596. // var_ctrl_ 包含诸如:global, nolocal,之类的
  597. void var_ctrl_(p_status *status, token_node *list){
  598. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  599. token left, var, right, new_token;
  600. statement *times = NULL;
  601. char *var_name = NULL;
  602. left = pop_node(list);
  603. if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  604. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  605. get_right_token(status, list, top_exp, var); // 取得base_var
  606. if(var.type != NON_top_exp && var.data.statement_value->type != base_var){
  607. paser_error("Don't get var");
  608. }
  609. else{
  610. var_name = malloc(sizeof(var.data.statement_value->code.base_var.var_name));
  611. strcpy(var_name, var.data.statement_value->code.base_var.var_name);
  612. times = var.data.statement_value->code.base_var.from;
  613. // TODO:: 本质上没有完全释放
  614. free(var.data.statement_value->code.base_var.var_name);
  615. free(var.data.statement_value);
  616. }
  617. if(left.type == DEFAULT_PASER){ // 设置times
  618. get_right_token(status, list, top_exp, right); // 回调右边
  619. if(right.type != NON_top_exp){
  620. back_again(list, right); // 不是期望的数字
  621. }
  622. else{
  623. times = right.data.statement_value;
  624. }
  625. }
  626. // 逻辑操作
  627. new_token.type = NON_ctrl;
  628. new_token.data_type = statement_value;
  629. statement *code_tmp = make_statement();
  630. switch (left.type)
  631. {
  632. case GLOBAL_PASER:
  633. code_tmp->type = set_global;
  634. code_tmp->code.set_global.name = var_name;
  635. break;
  636. case DEFAULT_PASER:
  637. code_tmp->type = set_default;
  638. code_tmp->code.set_default.name = var_name;
  639. code_tmp->code.set_default.times = times;
  640. break;
  641. case NOLOCAL_PASER:
  642. code_tmp->type = set_nonlocal;
  643. code_tmp->code.set_nonlocal.name = var_name;
  644. break;
  645. default:
  646. break;
  647. }
  648. new_token.data.statement_value = code_tmp;
  649. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  650. return; // 回调自己
  651. }
  652. else{ // 模式1
  653. back_one_token(list, left);
  654. return;
  655. }
  656. }
  657. void return_(p_status *status, token_node *list){
  658. fprintf(status_log, "[info][grammar] mode status: return_\n");
  659. token left, right, value, new_token;
  660. statement *times = NULL, *back_value = NULL;
  661. left = pop_node(list);
  662. if(left.type == RETURN_PASER){
  663. fprintf(status_log, "[info][grammar] (return_)reduce right\n");
  664. get_right_token(status, list, top_exp, value); // 回调右边
  665. if(value.type != NON_top_exp){
  666. back_again(list, value); // 不是期望的数字,就默认使用NULL,并且回退
  667. times = NULL;
  668. back_value = NULL;
  669. goto not_times;
  670. }
  671. else{
  672. back_value = value.data.statement_value;
  673. }
  674. get_right_token(status, list, top_exp, right); // 回调右边
  675. if(right.type != NON_top_exp){
  676. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  677. times = NULL;
  678. }
  679. else{
  680. times = right.data.statement_value;
  681. }
  682. not_times:
  683. new_token.type = NON_return;
  684. new_token.data_type = statement_value;
  685. statement *code_tmp = make_statement();
  686. code_tmp->type = return_code;
  687. code_tmp->code.return_code.times = times;
  688. code_tmp->code.return_code.value = back_value;
  689. new_token.data.statement_value = code_tmp;
  690. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  691. return; // 回调自己
  692. }
  693. else{ // 模式1
  694. back_one_token(list, left);
  695. return;
  696. }
  697. }
  698. // ctrl_包含诸如:break,broken,之类的
  699. void ctrl_(p_status *status, token_node *list){
  700. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  701. token left, right, new_token;
  702. statement *times = NULL;
  703. left = pop_node(list);
  704. if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  705. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  706. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  707. if(left.type != REGO_PASER && left.type != REWENT_PASER){
  708. get_right_token(status, list, top_exp, right); // 回调右边
  709. if(right.type != NON_top_exp){
  710. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  711. times = NULL;
  712. }
  713. else{
  714. times = right.data.statement_value;
  715. }
  716. }
  717. // 逻辑操作
  718. new_token.type = NON_ctrl;
  719. new_token.data_type = statement_value;
  720. statement *code_tmp = make_statement();
  721. switch (left.type)
  722. {
  723. case BREAK_PASER:
  724. code_tmp->type = break_cycle;
  725. code_tmp->code.break_cycle.times = times;
  726. break;
  727. case BROKEN_PASER:
  728. code_tmp->type = broken;
  729. code_tmp->code.broken.times = times;
  730. break;
  731. case CONTINUE_PASER:
  732. code_tmp->type = continue_cycle;
  733. code_tmp->code.continue_cycle.times = times;
  734. break;
  735. case CONTINUED_PASER:
  736. code_tmp->type = continued;
  737. code_tmp->code.continued.times = times;
  738. break;
  739. case RESTART_PASER:
  740. code_tmp->type = restart;
  741. code_tmp->code.restart.times = times;
  742. break;
  743. case RESTARTED_PASER:
  744. code_tmp->type = restarted;
  745. code_tmp->code.restarted.times = times;
  746. break;
  747. case REGO_PASER:
  748. code_tmp->type = rego;
  749. break;
  750. case REWENT_PASER:
  751. code_tmp->type = rewent;
  752. break;
  753. default:
  754. break;
  755. }
  756. new_token.data.statement_value = code_tmp;
  757. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  758. return; // 回调自己
  759. }
  760. else{ // 模式1
  761. back_one_token(list, left);
  762. return;
  763. }
  764. }
  765. /*
  766. top_exp : polynomial
  767. */
  768. void top_exp(p_status *status, token_node *list){
  769. fprintf(status_log, "[info][grammar] mode status: top_exp\n");
  770. token exp;
  771. get_base_token(status,list,eq_number,exp);
  772. if(exp.type != NON_eq){
  773. back_one_token(list, exp);
  774. return;
  775. }
  776. exp.type = NON_top_exp;
  777. add_node(list, exp); // 压入节点
  778. return;
  779. }
  780. /*
  781. eq_number : bool_or
  782. | eq_number AND bool_or
  783. */
  784. void eq_number(p_status *status, token_node *list){ // 因试分解
  785. fprintf(status_log, "[info][grammar] mode status: eq_number\n");
  786. token left, right, symbol, new_token;
  787. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  788. if(left.type == NON_eq){ // 模式2/3
  789. fprintf(status_log, "[info][grammar] (eq_number)reduce right\n");
  790. get_pop_token(status, list, symbol);
  791. if(symbol.type == EQ_PASER && !status->is_parameter){ // 模式2/3
  792. get_right_token(status, list, call_back_, right); // 回调右边
  793. if(right.type != NON_call){
  794. paser_error("Don't get a call_back_");
  795. }
  796. // 逻辑操作
  797. new_token.type = NON_eq;
  798. new_token.data_type = statement_value;
  799. statement *code_tmp = make_statement();
  800. code_tmp->type = operation;
  801. code_tmp->code.operation.type = ASSIGnMENT_func;
  802. code_tmp->code.operation.left_exp = left.data.statement_value;
  803. code_tmp->code.operation.right_exp = right.data.statement_value;
  804. new_token.data.statement_value = code_tmp;
  805. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  806. return eq_number(status, list); // 回调自己
  807. }
  808. else{ // 递归跳出
  809. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  810. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  811. back_one_token(list, left);
  812. back_again(list, symbol);
  813. return;
  814. }
  815. }
  816. else{ // 模式1
  817. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  818. back_one_token(list, left);
  819. get_base_token(status, list, call_back_, new_token);
  820. if(new_token.type != NON_call){
  821. back_one_token(list, new_token); // 往回[不匹配类型]
  822. return;
  823. }
  824. new_token.type = NON_eq;
  825. add_node(list, new_token);
  826. return eq_number(status, list); // 回调自己
  827. }
  828. }
  829. /*
  830. call_back_ : bool_or
  831. | call_back_ LB RB
  832. */
  833. void call_back_(p_status *status, token_node *list){ // 因试分解
  834. fprintf(status_log, "[info][grammar] mode status: call_back_\n");
  835. token left, symbol, rb_t, new_token, parameter_t;
  836. parameter *p_list;
  837. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  838. if(left.type == NON_call){
  839. fprintf(status_log, "[info][grammar] (call_back_)reduce right\n");
  840. get_pop_token(status, list, symbol);
  841. if(symbol.type == LB_PASER && !status->is_func){
  842. get_pop_token(status, list, rb_t);
  843. if(rb_t.type != RB_PASER){ // 带参数
  844. back_again(list, rb_t);
  845. get_right_token(status,list,formal_parameter,parameter_t);
  846. if(parameter_t.type != NON_parameter){
  847. paser_error("Don't get formal_parameter");
  848. }
  849. printf("rb_t.type = %d\n", rb_t.type);
  850. get_pop_token(status, list, rb_t);
  851. if(rb_t.type != RB_PASER){
  852. paser_error("Don't get ')'[2]");
  853. }
  854. p_list = parameter_t.data.parameter_list;
  855. }
  856. else{
  857. p_list = NULL; // 没参数
  858. }
  859. // 逻辑操作
  860. new_token.type = NON_call;
  861. new_token.data_type = statement_value;
  862. statement *code_tmp = make_statement();
  863. code_tmp->type = call;
  864. code_tmp->code.call.func = left.data.statement_value;
  865. code_tmp->code.call.parameter_list = p_list;
  866. new_token.data.statement_value = code_tmp;
  867. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  868. return call_back_(status, list); // 回调自己
  869. }
  870. else{ // 递归跳出
  871. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  872. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  873. back_one_token(list, left);
  874. back_again(list, symbol);
  875. return;
  876. }
  877. }
  878. else{ // 模式1
  879. fprintf(status_log, "[info][grammar] (call_back_)back one token to (bool_or)\n");
  880. back_one_token(list, left);
  881. get_base_token(status, list, bool_or, new_token);
  882. if(new_token.type != NON_bool_or){
  883. back_one_token(list, new_token); // 往回[不匹配类型]
  884. return;
  885. }
  886. new_token.type = NON_call;
  887. add_node(list, new_token);
  888. return call_back_(status, list); // 回调自己
  889. }
  890. }
  891. /*
  892. bool_or : bool_and
  893. | bool_or AND bool_and
  894. */
  895. void bool_or(p_status *status, token_node *list){ // 因试分解
  896. fprintf(status_log, "[info][grammar] mode status: bool_or\n");
  897. token left, right, symbol, new_token;
  898. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  899. if(left.type == NON_bool_or){ // 模式2/3
  900. fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
  901. get_pop_token(status, list, symbol);
  902. if(symbol.type == OR_PASER){ // 模式2/3
  903. get_right_token(status, list, bool_and, right); // 回调右边
  904. if(right.type != NON_bool_and){
  905. paser_error("Don't get a compare");
  906. }
  907. // 逻辑操作
  908. new_token.type = NON_bool_or;
  909. new_token.data_type = statement_value;
  910. statement *code_tmp = make_statement();
  911. code_tmp->type = operation;
  912. code_tmp->code.operation.type = OR_func;
  913. code_tmp->code.operation.left_exp = left.data.statement_value;
  914. code_tmp->code.operation.right_exp = right.data.statement_value;
  915. new_token.data.statement_value = code_tmp;
  916. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  917. return bool_or(status, list); // 回调自己
  918. }
  919. else{ // 递归跳出
  920. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  921. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  922. back_one_token(list, left);
  923. back_again(list, symbol);
  924. return;
  925. }
  926. }
  927. else{ // 模式1
  928. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  929. back_one_token(list, left);
  930. get_base_token(status, list, bool_and, new_token);
  931. if(new_token.type != NON_bool_and){
  932. back_one_token(list, new_token); // 往回[不匹配类型]
  933. return;
  934. }
  935. new_token.type = NON_bool_or;
  936. add_node(list, new_token);
  937. return bool_or(status, list); // 回调自己
  938. }
  939. }
  940. /*
  941. bool_and : bool_not
  942. | bool_and AND bool_not
  943. */
  944. void bool_and(p_status *status, token_node *list){ // 因试分解
  945. fprintf(status_log, "[info][grammar] mode status: bool_and\n");
  946. token left, right, symbol, new_token;
  947. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  948. if(left.type == NON_bool_and){ // 模式2/3
  949. fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
  950. get_pop_token(status, list, symbol);
  951. if(symbol.type == AND_PASER){ // 模式2/3
  952. get_right_token(status, list, bool_not, right); // 回调右边
  953. if(right.type != NON_bool_not){
  954. paser_error("Don't get a bool_not");
  955. }
  956. // 逻辑操作
  957. new_token.type = NON_bool_and;
  958. new_token.data_type = statement_value;
  959. statement *code_tmp = make_statement();
  960. code_tmp->type = operation;
  961. code_tmp->code.operation.type = AND_func;
  962. code_tmp->code.operation.left_exp = left.data.statement_value;
  963. code_tmp->code.operation.right_exp = right.data.statement_value;
  964. new_token.data.statement_value = code_tmp;
  965. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  966. return bool_and(status, list); // 回调自己
  967. }
  968. else{ // 递归跳出
  969. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  970. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  971. back_one_token(list, left);
  972. back_again(list, symbol);
  973. return;
  974. }
  975. }
  976. else{ // 模式1
  977. fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
  978. back_one_token(list, left);
  979. get_base_token(status, list, bool_not, new_token);
  980. if(new_token.type != NON_bool_not){
  981. back_one_token(list, new_token); // 往回[不匹配类型]
  982. return;
  983. }
  984. new_token.type = NON_bool_and;
  985. add_node(list, new_token);
  986. return bool_and(status, list); // 回调自己
  987. }
  988. }
  989. /*
  990. bool_not : compare
  991. | BITNOT bool_not
  992. */
  993. void bool_not(p_status *status, token_node *list){
  994. fprintf(status_log, "[info][grammar] mode status: negative\n");
  995. token left, right, new_token;
  996. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  997. if(left.type == NOT_PASER){ // 模式2
  998. fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
  999. get_right_token(status, list, bool_not, right); // 回调右边
  1000. if(right.type != NON_bool_not){
  1001. paser_error("Don't get a bool_not");
  1002. }
  1003. // 逻辑操作
  1004. new_token.type = NON_bool_not;
  1005. new_token.data_type = statement_value;
  1006. statement *code_tmp = make_statement();
  1007. code_tmp->type = operation;
  1008. code_tmp->code.operation.type = NOT_func;
  1009. code_tmp->code.operation.left_exp = NULL;
  1010. code_tmp->code.operation.right_exp = right.data.statement_value;
  1011. new_token.data.statement_value = code_tmp;
  1012. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1013. return; // 回调自己
  1014. }
  1015. else{ // 模式1
  1016. fprintf(status_log, "[info][grammar] (negative)back one token to (compare)\n");
  1017. back_one_token(list, left);
  1018. get_base_token(status, list, compare, new_token);
  1019. if(new_token.type != NON_compare){
  1020. back_one_token(list, new_token); // 往回[不匹配类型]
  1021. return;
  1022. }
  1023. new_token.type = NON_bool_not;
  1024. add_node(list, new_token);
  1025. return;
  1026. }
  1027. }
  1028. void compare(p_status *status, token_node *list){ // 多项式
  1029. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1030. token left, right, symbol, new_token;
  1031. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1032. if(left.type == NON_compare){ // 模式2/3
  1033. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1034. get_pop_token(status, list, symbol);
  1035. if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
  1036. symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){ // 模式2/3
  1037. get_right_token(status, list, bit_notor, right); // 回调右边
  1038. if(right.type != NON_bit_notor){
  1039. paser_error("Don't get a bit_notor");
  1040. }
  1041. new_token.type = NON_compare;
  1042. new_token.data_type = statement_value;
  1043. statement *code_tmp = make_statement();
  1044. code_tmp->type = operation;
  1045. if(symbol.type == EQEQ_PASER){
  1046. code_tmp->code.operation.type = EQUAL_func;
  1047. }
  1048. else if(symbol.type == MOREEQ_PASER){
  1049. code_tmp->code.operation.type = MOREEQ_func;
  1050. }
  1051. else if(symbol.type == LESSEQ_PASER){
  1052. code_tmp->code.operation.type = LESSEQ_func;
  1053. }
  1054. else if(symbol.type == MORE_PASER){
  1055. code_tmp->code.operation.type = MORE_func;
  1056. }
  1057. else if(symbol.type == LESS_PASER){
  1058. code_tmp->code.operation.type = LESS_func;
  1059. }
  1060. else{
  1061. code_tmp->code.operation.type = NOTEQ_func;
  1062. }
  1063. code_tmp->code.operation.left_exp = left.data.statement_value;
  1064. code_tmp->code.operation.right_exp = right.data.statement_value;
  1065. new_token.data.statement_value = code_tmp;
  1066. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1067. return compare(status, list); // 回调自己
  1068. }
  1069. else{ // 递归跳出
  1070. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1071. back_one_token(list, left);
  1072. back_again(list, symbol);
  1073. return;
  1074. }
  1075. }
  1076. else{ // 模式1
  1077. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1078. back_one_token(list, left);
  1079. get_base_token(status, list, bit_notor, new_token);
  1080. if(new_token.type != NON_bit_notor){
  1081. back_one_token(list, new_token); // 往回[不匹配类型]
  1082. return;
  1083. }
  1084. new_token.type = NON_compare;
  1085. add_node(list, new_token);
  1086. return compare(status, list); // 回调自己
  1087. }
  1088. }
  1089. /*
  1090. bit_notor : bit_or
  1091. | bit_notor BITOR bit_or
  1092. */
  1093. void bit_notor(p_status *status, token_node *list){ // 因试分解
  1094. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1095. token left, right, symbol, new_token;
  1096. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1097. if(left.type == NON_bit_notor){ // 模式2/3
  1098. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1099. get_pop_token(status, list, symbol);
  1100. if(symbol.type == BITNOTOR_PASER){ // 模式2/3
  1101. get_right_token(status, list, bit_or, right); // 回调右边
  1102. if(right.type != NON_bit_or){
  1103. paser_error("Don't get a bit_or");
  1104. }
  1105. // 逻辑操作
  1106. new_token.type = NON_bit_notor;
  1107. new_token.data_type = statement_value;
  1108. statement *code_tmp = make_statement();
  1109. code_tmp->type = operation;
  1110. code_tmp->code.operation.type = BITNOTOR_func;
  1111. code_tmp->code.operation.left_exp = left.data.statement_value;
  1112. code_tmp->code.operation.right_exp = right.data.statement_value;
  1113. new_token.data.statement_value = code_tmp;
  1114. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1115. return bit_notor(status, list); // 回调自己
  1116. }
  1117. else{ // 递归跳出
  1118. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1119. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1120. back_one_token(list, left);
  1121. back_again(list, symbol);
  1122. return;
  1123. }
  1124. }
  1125. else{ // 模式1
  1126. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1127. back_one_token(list, left);
  1128. get_base_token(status, list, bit_or, new_token);
  1129. if(new_token.type != NON_bit_or){
  1130. back_one_token(list, new_token); // 往回[不匹配类型]
  1131. return;
  1132. }
  1133. new_token.type = NON_bit_notor;
  1134. add_node(list, new_token);
  1135. return bit_notor(status, list); // 回调自己
  1136. }
  1137. }
  1138. /*
  1139. bit_or : bit_and
  1140. | bit_or BITOR bit_and
  1141. */
  1142. void bit_or(p_status *status, token_node *list){ // 因试分解
  1143. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1144. token left, right, symbol, new_token;
  1145. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1146. if(left.type == NON_bit_or){ // 模式2/3
  1147. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1148. get_pop_token(status, list, symbol);
  1149. if(symbol.type == BITOR_PASER){ // 模式2/3
  1150. get_right_token(status, list, bit_and, right); // 回调右边
  1151. if(right.type != NON_bit_and){
  1152. paser_error("Don't get a bit_and");
  1153. }
  1154. // 逻辑操作
  1155. new_token.type = NON_bit_or;
  1156. new_token.data_type = statement_value;
  1157. statement *code_tmp = make_statement();
  1158. code_tmp->type = operation;
  1159. code_tmp->code.operation.type = BITOR_func;
  1160. code_tmp->code.operation.left_exp = left.data.statement_value;
  1161. code_tmp->code.operation.right_exp = right.data.statement_value;
  1162. new_token.data.statement_value = code_tmp;
  1163. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1164. return bit_or(status, list); // 回调自己
  1165. }
  1166. else{ // 递归跳出
  1167. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1168. fprintf(status_log, "[info][grammar] (bit_or)out\n");
  1169. back_one_token(list, left);
  1170. back_again(list, symbol);
  1171. return;
  1172. }
  1173. }
  1174. else{ // 模式1
  1175. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1176. back_one_token(list, left);
  1177. get_base_token(status, list, bit_and, new_token);
  1178. if(new_token.type != NON_bit_and){
  1179. back_one_token(list, new_token); // 往回[不匹配类型]
  1180. return;
  1181. }
  1182. new_token.type = NON_bit_or;
  1183. add_node(list, new_token);
  1184. return bit_or(status, list); // 回调自己
  1185. }
  1186. }
  1187. /*
  1188. bit_and : bit_move
  1189. | bit_and BITAND bit_move
  1190. */
  1191. void bit_and(p_status *status, token_node *list){ // 因试分解
  1192. fprintf(status_log, "[info][grammar] mode status: bit_and\n");
  1193. token left, right, symbol, new_token;
  1194. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1195. if(left.type == NON_bit_and){ // 模式2/3
  1196. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1197. get_pop_token(status, list, symbol);
  1198. if(symbol.type == BITAND_PASER){ // 模式2/3
  1199. get_right_token(status, list, bit_move, right); // 回调右边
  1200. if(right.type != NON_bit_move){
  1201. paser_error("Don't get a bit_move");
  1202. }
  1203. // 逻辑操作
  1204. new_token.type = NON_bit_and;
  1205. new_token.data_type = statement_value;
  1206. statement *code_tmp = make_statement();
  1207. code_tmp->type = operation;
  1208. code_tmp->code.operation.type = BITAND_func;
  1209. code_tmp->code.operation.left_exp = left.data.statement_value;
  1210. code_tmp->code.operation.right_exp = right.data.statement_value;
  1211. new_token.data.statement_value = code_tmp;
  1212. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1213. return bit_and(status, list); // 回调自己
  1214. }
  1215. else{ // 递归跳出
  1216. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1217. fprintf(status_log, "[info][grammar] (bit_and)out\n");
  1218. back_one_token(list, left);
  1219. back_again(list, symbol);
  1220. return;
  1221. }
  1222. }
  1223. else{ // 模式1
  1224. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1225. back_one_token(list, left);
  1226. get_base_token(status, list, bit_move, new_token);
  1227. if(new_token.type != NON_bit_move){
  1228. back_one_token(list, new_token); // 往回[不匹配类型]
  1229. return;
  1230. }
  1231. new_token.type = NON_bit_and;
  1232. add_node(list, new_token);
  1233. return bit_and(status, list); // 回调自己
  1234. }
  1235. }
  1236. /*
  1237. bit_move : power
  1238. | bit_move BITRIGHT factor
  1239. | bit_move BITLEFT factor
  1240. */
  1241. void bit_move(p_status *status, token_node *list){ // 因试分解
  1242. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1243. token left, right, symbol, new_token;
  1244. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1245. if(left.type == NON_bit_move){ // 模式2/3
  1246. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1247. get_pop_token(status, list, symbol);
  1248. if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
  1249. get_right_token(status, list, polynomial, right); // 回调右边
  1250. if(right.type != NON_polynomial){
  1251. paser_error("Don't get a polynomial");
  1252. }
  1253. // 逻辑操作
  1254. new_token.type = NON_bit_move;
  1255. new_token.data_type = statement_value;
  1256. statement *code_tmp = make_statement();
  1257. code_tmp->type = operation;
  1258. if(symbol.type == BITRIGHT_PASER){
  1259. code_tmp->code.operation.type = BITRIGHT_func;
  1260. }
  1261. else{
  1262. code_tmp->code.operation.type = BITLEFT_func;
  1263. }
  1264. code_tmp->code.operation.left_exp = left.data.statement_value;
  1265. code_tmp->code.operation.right_exp = right.data.statement_value;
  1266. new_token.data.statement_value = code_tmp;
  1267. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1268. return bit_move(status, list); // 回调自己
  1269. }
  1270. else{ // 递归跳出
  1271. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1272. fprintf(status_log, "[info][grammar] (bit_move)out\n");
  1273. back_one_token(list, left);
  1274. back_again(list, symbol);
  1275. return;
  1276. }
  1277. }
  1278. else{ // 模式1
  1279. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1280. back_one_token(list, left);
  1281. get_base_token(status, list, polynomial, new_token);
  1282. if(new_token.type != NON_polynomial){
  1283. back_one_token(list, new_token); // 往回[不匹配类型]
  1284. return;
  1285. }
  1286. new_token.type = NON_bit_move;
  1287. add_node(list, new_token);
  1288. return bit_move(status, list); // 回调自己
  1289. }
  1290. }
  1291. /*
  1292. polynomial : factor
  1293. | polynomial ADD factor
  1294. | polynomial SUB factor
  1295. */
  1296. void polynomial(p_status *status, token_node *list){ // 多项式
  1297. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1298. token left, right, symbol, new_token;
  1299. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1300. if(left.type == NON_polynomial){ // 模式2/3
  1301. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1302. get_pop_token(status, list, symbol);
  1303. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  1304. get_right_token(status, list, factor, right); // 回调右边
  1305. if(right.type != NON_factor){
  1306. paser_error("Don't get a factor");
  1307. }
  1308. new_token.type = NON_polynomial;
  1309. new_token.data_type = statement_value;
  1310. statement *code_tmp = make_statement();
  1311. code_tmp->type = operation;
  1312. if(symbol.type == ADD_PASER){
  1313. code_tmp->code.operation.type = ADD_func;
  1314. }
  1315. else{
  1316. code_tmp->code.operation.type = SUB_func;
  1317. }
  1318. code_tmp->code.operation.left_exp = left.data.statement_value;
  1319. code_tmp->code.operation.right_exp = right.data.statement_value;
  1320. new_token.data.statement_value = code_tmp;
  1321. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1322. return polynomial(status, list); // 回调自己
  1323. }
  1324. else{ // 递归跳出
  1325. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1326. back_one_token(list, left);
  1327. back_again(list, symbol);
  1328. return;
  1329. }
  1330. }
  1331. else{ // 模式1
  1332. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1333. back_one_token(list, left);
  1334. get_base_token(status, list, factor, new_token);
  1335. if(new_token.type != NON_factor){
  1336. back_one_token(list, new_token); // 往回[不匹配类型]
  1337. return;
  1338. }
  1339. new_token.type = NON_polynomial;
  1340. add_node(list, new_token);
  1341. return polynomial(status, list); // 回调自己
  1342. }
  1343. }
  1344. /*
  1345. factor : power
  1346. | factor MUL power
  1347. | factor DIV power
  1348. */
  1349. void factor(p_status *status, token_node *list){ // 因试分解
  1350. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1351. token left, right, symbol, new_token;
  1352. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1353. if(left.type == NON_factor){ // 模式2/3
  1354. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1355. get_pop_token(status, list, symbol);
  1356. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
  1357. get_right_token(status, list, negative, right); // 回调右边
  1358. if(right.type != NON_negative){
  1359. paser_error("Don't get a value");
  1360. }
  1361. // 逻辑操作
  1362. new_token.type = NON_factor;
  1363. new_token.data_type = statement_value;
  1364. statement *code_tmp = make_statement();
  1365. code_tmp->type = operation;
  1366. if(symbol.type == MUL_PASER){
  1367. code_tmp->code.operation.type = MUL_func;
  1368. }
  1369. else{
  1370. code_tmp->code.operation.type = DIV_func;
  1371. }
  1372. code_tmp->code.operation.left_exp = left.data.statement_value;
  1373. code_tmp->code.operation.right_exp = right.data.statement_value;
  1374. new_token.data.statement_value = code_tmp;
  1375. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1376. return factor(status, list); // 回调自己
  1377. }
  1378. else{ // 递归跳出
  1379. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1380. fprintf(status_log, "[info][grammar] (factor)out\n");
  1381. back_one_token(list, left);
  1382. back_again(list, symbol);
  1383. return;
  1384. }
  1385. }
  1386. else{ // 模式1
  1387. fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
  1388. back_one_token(list, left);
  1389. get_base_token(status, list, negative, new_token);
  1390. if(new_token.type != NON_negative){
  1391. back_one_token(list, new_token); // 往回[不匹配类型]
  1392. return;
  1393. }
  1394. new_token.type = NON_factor;
  1395. add_node(list, new_token);
  1396. return factor(status, list); // 回调自己
  1397. }
  1398. }
  1399. /*
  1400. negative : bit_not
  1401. | BITNOT bit_not
  1402. */
  1403. void negative(p_status *status, token_node *list){
  1404. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1405. token left, right, new_token;
  1406. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1407. if(left.type == SUB_PASER){ // 模式2
  1408. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1409. get_right_token(status, list, negative, right); // 回调右边
  1410. if(right.type != NON_negative){
  1411. paser_error("Don't get a negative");
  1412. }
  1413. // 逻辑操作
  1414. new_token.type = NON_negative;
  1415. new_token.data_type = statement_value;
  1416. statement *code_tmp = make_statement();
  1417. code_tmp->type = operation;
  1418. code_tmp->code.operation.type = NEGATIVE_func;
  1419. code_tmp->code.operation.left_exp = NULL;
  1420. code_tmp->code.operation.right_exp = right.data.statement_value;
  1421. new_token.data.statement_value = code_tmp;
  1422. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1423. return; // 回调自己
  1424. }
  1425. else{ // 模式1
  1426. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  1427. back_one_token(list, left);
  1428. get_base_token(status, list, bit_not, new_token);
  1429. if(new_token.type != NON_bit_not){
  1430. back_one_token(list, new_token); // 往回[不匹配类型]
  1431. return;
  1432. }
  1433. new_token.type = NON_negative;
  1434. add_node(list, new_token);
  1435. return;
  1436. }
  1437. }
  1438. /*
  1439. bit_not : power
  1440. | BITNOT power
  1441. */
  1442. void bit_not(p_status *status, token_node *list){
  1443. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  1444. token left, right, new_token;
  1445. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1446. if(left.type == BITNOT_PASER){ // 模式2
  1447. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1448. get_right_token(status, list, bit_not, right); // 回调右边
  1449. if(right.type != NON_bit_not){
  1450. paser_error("Don't get a bit_not");
  1451. }
  1452. // 逻辑操作
  1453. new_token.type = NON_bit_not;
  1454. new_token.data_type = statement_value;
  1455. statement *code_tmp = make_statement();
  1456. code_tmp->type = operation;
  1457. code_tmp->code.operation.type = BITNOT_func;
  1458. code_tmp->code.operation.left_exp = NULL;
  1459. code_tmp->code.operation.right_exp = right.data.statement_value;
  1460. new_token.data.statement_value = code_tmp;
  1461. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1462. return; // 回调自己
  1463. }
  1464. else{ // 模式1
  1465. fprintf(status_log, "[info][grammar] (bit_not)back one token to (power)\n");
  1466. back_one_token(list, left);
  1467. get_base_token(status, list, power, new_token);
  1468. if(new_token.type != NON_power){
  1469. back_one_token(list, new_token); // 往回[不匹配类型]
  1470. return;
  1471. }
  1472. new_token.type = NON_bit_not;
  1473. add_node(list, new_token);
  1474. return;
  1475. }
  1476. }
  1477. /*
  1478. power : element
  1479. | power POW element
  1480. | power LOG element
  1481. | power SQRT element
  1482. */
  1483. void power(p_status *status, token_node *list){
  1484. fprintf(status_log, "[info][grammar] mode status: power\n");
  1485. token left, right, symbol, new_token;
  1486. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1487. if(left.type == NON_power){ // 模式2/3
  1488. fprintf(status_log, "[info][grammar] (power)reduce right\n");
  1489. get_pop_token(status, list, symbol);
  1490. if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
  1491. get_right_token(status, list, call_down, right); // 回调右边
  1492. if(right.type != NON_call_down){
  1493. paser_error("Don't get a call_down");
  1494. }
  1495. // 逻辑操作
  1496. new_token.type = NON_power;
  1497. new_token.data_type = statement_value;
  1498. statement *code_tmp = make_statement();
  1499. code_tmp->type = operation;
  1500. if(symbol.type == POW_PASER){
  1501. code_tmp->code.operation.type = POW_func;
  1502. }
  1503. else if(symbol.type == LOG_PASER){
  1504. code_tmp->code.operation.type = LOG_func;
  1505. }
  1506. else{
  1507. code_tmp->code.operation.type = SQRT_func;
  1508. }
  1509. code_tmp->code.operation.left_exp = left.data.statement_value;
  1510. code_tmp->code.operation.right_exp = right.data.statement_value;
  1511. new_token.data.statement_value = code_tmp;
  1512. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1513. return power(status, list); // 回调自己
  1514. }
  1515. else{ // 递归跳出
  1516. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1517. fprintf(status_log, "[info][grammar] (power)out\n");
  1518. back_one_token(list, left);
  1519. back_again(list, symbol);
  1520. return;
  1521. }
  1522. }
  1523. else{ // 模式1
  1524. fprintf(status_log, "[info][grammar] (power)back one token to (element)\n");
  1525. back_one_token(list, left);
  1526. get_base_token(status, list, call_down, new_token);
  1527. if(new_token.type != NON_call_down){
  1528. back_one_token(list, new_token); // 往回[不匹配类型]
  1529. return;
  1530. }
  1531. new_token.type = NON_power;
  1532. add_node(list, new_token);
  1533. return power(status, list); // 回调自己
  1534. }
  1535. }
  1536. /*
  1537. call_down : element
  1538. | call_down LI top_exp RI
  1539. */
  1540. void call_down(p_status *status, token_node *list){ // 因试分解
  1541. fprintf(status_log, "[info][grammar] mode status: call_down\n");
  1542. token left, lb_t, rb_t, new_token, parameter_t;
  1543. parameter *p_list;
  1544. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1545. if(left.type == NON_call_down){
  1546. fprintf(status_log, "[info][grammar] (call_down)reduce right\n");
  1547. get_pop_token(status, list, lb_t);
  1548. if(lb_t.type == LI_PASER){
  1549. get_right_token(status, list, top_exp, parameter_t); // 回调右边
  1550. if(parameter_t.type != NON_top_exp){
  1551. paser_error("Don't get a top_exp");
  1552. }
  1553. get_pop_token(status, list, rb_t);
  1554. if(rb_t.type != RI_PASER){ // 带参数
  1555. paser_error("Don't get ']'");
  1556. }
  1557. // 逻辑操作
  1558. new_token.type = NON_call_down;
  1559. new_token.data_type = statement_value;
  1560. statement *code_tmp = make_statement();
  1561. code_tmp->type = down;
  1562. code_tmp->code.down.base_var = left.data.statement_value;
  1563. code_tmp->code.down.child_var = parameter_t.data.statement_value;
  1564. new_token.data.statement_value = code_tmp;
  1565. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1566. return call_down(status, list); // 回调自己
  1567. }
  1568. else{ // 递归跳出
  1569. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1570. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  1571. back_one_token(list, left);
  1572. back_again(list, lb_t);
  1573. return;
  1574. }
  1575. }
  1576. else{ // 模式1
  1577. fprintf(status_log, "[info][grammar] (call_down)back one token to (element)\n");
  1578. back_one_token(list, left);
  1579. get_base_token(status, list, element, new_token);
  1580. if(new_token.type != NON_element){
  1581. back_one_token(list, new_token); // 往回[不匹配类型]
  1582. return;
  1583. }
  1584. new_token.type = NON_call_down;
  1585. add_node(list, new_token);
  1586. return call_down(status, list); // 回调自己
  1587. }
  1588. }
  1589. /*
  1590. element : number
  1591. | LB top_exp RB
  1592. */
  1593. void element(p_status *status, token_node *list){ // 数字归约
  1594. fprintf(status_log, "[info][grammar] mode status: element\n");
  1595. token gett, new_token;
  1596. gett = pop_node(list); // 取得一个token
  1597. if(gett.type == LB_PASER){ // 模式3
  1598. fprintf(status_log, "[info][grammar] (element)get LB\n");
  1599. get_right_token(status, list, top_exp, new_token);
  1600. if(new_token.type != NON_top_exp){
  1601. paser_error("Don't get 'top_exp'");
  1602. }
  1603. token rb;
  1604. get_pop_token(status, list ,rb);
  1605. if(rb.type != RB_PASER){ // 匹配失败
  1606. paser_error("Don't get ')'");
  1607. }
  1608. new_token.type = NON_element;
  1609. add_node(list, new_token); // 压入节点
  1610. return;
  1611. }
  1612. else if(gett.type == VAR_PASER){ // a
  1613. back_one_token(list, gett);
  1614. get_base_token(status, list, var_token, new_token);
  1615. if(new_token.type != NON_base_var){
  1616. back_one_token(list, new_token); // 往回[不匹配类型]
  1617. return;
  1618. }
  1619. new_token.type = NON_element;
  1620. add_node(list, new_token);
  1621. return;
  1622. }
  1623. else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
  1624. back_one_token(list, gett);
  1625. token exp_token, rb, tmp_var;
  1626. get_right_token(status, list, top_exp, exp_token);
  1627. if(exp_token.type != NON_top_exp){
  1628. paser_error("Don't get 'top_exp'");
  1629. }
  1630. get_pop_token(status, list ,rb);
  1631. if(rb.type != RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
  1632. paser_error("Don't get ']'");
  1633. }
  1634. get_pop_token(status, list ,tmp_var);
  1635. if(tmp_var.type == VAR_PASER){ // a
  1636. back_one_token(list, tmp_var);
  1637. get_base_token(status, list, var_token, new_token);
  1638. if(new_token.type != NON_base_var){
  1639. paser_error("Don't get var");
  1640. }
  1641. new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
  1642. }
  1643. new_token.type = NON_element;
  1644. add_node(list, new_token);
  1645. return;
  1646. }
  1647. else{
  1648. fprintf(status_log, "[info][grammar] (element)back one token to (number)\n");
  1649. back_one_token(list, gett);
  1650. get_base_token(status, list, number, new_token);
  1651. if(new_token.type != NON_base_value){
  1652. back_one_token(list, new_token); // 往回[不匹配类型]
  1653. return;
  1654. }
  1655. new_token.type = NON_element;
  1656. add_node(list, new_token);
  1657. return;
  1658. }
  1659. }
  1660. /*
  1661. var_token : VAR
  1662. */
  1663. void var_token(p_status *status, token_node *list){ // 数字归约
  1664. fprintf(status_log, "[info][grammar] mode status: var_token\n");
  1665. token gett, new_token;
  1666. gett = pop_node(list); // 取得一个token
  1667. if(gett.type == VAR_PASER){ // var类型
  1668. new_token.type = NON_base_var;
  1669. GWARF_value tmp_value;
  1670. tmp_value.type = INT_value;
  1671. tmp_value.value.int_value = atoi(gett.data.text);
  1672. statement *code_tmp = make_statement();
  1673. code_tmp->type = base_var;
  1674. code_tmp->code.base_var.var_name = gett.data.text;
  1675. code_tmp->code.base_var.from = NULL;
  1676. new_token.data.statement_value = code_tmp;
  1677. new_token.data_type = statement_value;
  1678. fprintf(status_log, "[info][grammar] (var_token)out\n");
  1679. add_node(list, new_token); // 压入节点
  1680. return;
  1681. }
  1682. else{ // 不是期望值
  1683. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  1684. back_one_token(list, gett);
  1685. return;
  1686. }
  1687. }
  1688. /*
  1689. number : INT_PASER
  1690. | DOUBLE_PASER
  1691. | LB top_exp RB
  1692. */
  1693. void number(p_status *status, token_node *list){ // 数字归约
  1694. fprintf(status_log, "[info][grammar] mode status: number\n");
  1695. token gett, new_token;
  1696. gett = pop_node(list); // 取得一个token
  1697. if(gett.type == INT_PASER){ // int类型
  1698. new_token.type = NON_base_value;
  1699. GWARF_value tmp_value;
  1700. tmp_value.type = INT_value;
  1701. tmp_value.value.int_value = atoi(gett.data.text);
  1702. statement *code_tmp = make_statement();
  1703. code_tmp->type = call;
  1704. code_tmp->code.call.func = pack_call_name("int", NULL);
  1705. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1706. new_token.data.statement_value = code_tmp;
  1707. new_token.data_type = statement_value;
  1708. fprintf(status_log, "[info][grammar] (number)get int number: %d\n", tmp_value.value.int_value);
  1709. }
  1710. else if(gett.type == DOUBLE_PASER){
  1711. new_token.type = NON_base_value;
  1712. GWARF_value tmp_value;
  1713. tmp_value.type = NUMBER_value;
  1714. tmp_value.value.double_value = atof(gett.data.text);
  1715. statement *code_tmp = make_statement();
  1716. code_tmp->type = call;
  1717. code_tmp->code.call.func = pack_call_name("double", NULL);
  1718. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1719. new_token.data.statement_value = code_tmp;
  1720. new_token.data_type = statement_value;
  1721. fprintf(status_log, "[info][grammar] (number)get double number: %f\n", new_token.data.d_number);
  1722. }
  1723. else{ // 不是期望值
  1724. fprintf(status_log, "[info][grammar] (number)back one token\n");
  1725. back_one_token(list, gett);
  1726. return;
  1727. }
  1728. free(gett.data.text); // 释放字符串
  1729. fprintf(status_log, "[info][grammar] (number)add one token\n");
  1730. add_node(list, new_token); // 压入节点
  1731. }
  1732. void paser_error(char *text){
  1733. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  1734. printf("[error][grammar] paser error : %s\n\n", text);
  1735. exit(1);
  1736. }