syntax.c 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  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 paser_value(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_class(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 list_(p_status *status, token_node *list);
  36. void dict_(p_status *status, token_node *list);
  37. void hide_list(p_status *status, token_node *list);
  38. void do_while_(p_status *status, token_node *list);
  39. void try_(p_status *status, token_node *list);
  40. void out_exception(p_status *status, token_node *list);
  41. void self_exp(p_status *status, token_node *list);
  42. void lambda_(p_status *status, token_node *list);
  43. void paser_error(char *text);
  44. /*
  45. command_list : command
  46. | command_list command
  47. */
  48. void command_list(p_status *status, token_node *list){ // 多项式
  49. fprintf(status_log, "[info][grammar] mode status: command_list\n", text);
  50. token left, right, new_token;
  51. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  52. if(left.type == NON_command_list){ // 模式2
  53. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  54. get_right_token(status, list, command, right); // 回调右边
  55. if(right.type == NON_command){
  56. new_token.type = NON_command_list;
  57. new_token.data_type = empty;
  58. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  59. return command_list(status, list); // 回调自己
  60. }
  61. else{ // 递归跳出[EOF_token]
  62. printf("right.type = %d\n", right.type);
  63. fprintf(status_log, "[info][grammar] (command_list)out\n");
  64. back_one_token(list, left);
  65. back_again(list, right);
  66. return;
  67. }
  68. }
  69. else if(left.type == EOF_token){ // 递归跳出的条件
  70. fprintf(status_log, "[info][grammar] (command_list)out again\n");
  71. back_one_token(list, left);
  72. return;
  73. }
  74. else{ // 模式1
  75. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  76. back_one_token(list, left);
  77. get_base_token(status, list, command, new_token);
  78. if(new_token.type != NON_command){
  79. back_one_token(list, new_token); // 往回[不匹配类型]
  80. return;
  81. }
  82. new_token.type = NON_command_list;
  83. add_node(list, new_token);
  84. return command_list(status, list); // 回调自己
  85. }
  86. }
  87. /*
  88. command : top_exp <ENTER>
  89. */
  90. void command(p_status *old_status, token_node *list){ // 多项式
  91. p_status status;
  92. status = *old_status;
  93. status.is_left = false; // is_left复位[只有top_exp会开启以及element会开启]
  94. fprintf(status_log, "[info][grammar] mode status: command\n", text);
  95. token left, new_token;
  96. left = pop_node(list); // 先弹出一个token 检查token
  97. if(left.type == WHILE_PASER){ // 是while类型的数据
  98. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  99. back_one_token(list, left);
  100. get_base_token(&status, list, while_, new_token);
  101. get_stop_token(status, list);
  102. push_statement(statement_base, new_token);
  103. }
  104. else if(left.type == IF_PASER){
  105. fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
  106. back_one_token(list, left);
  107. get_base_token(&status, list, if_, new_token);
  108. get_stop_token(status, list);
  109. push_statement(statement_base, new_token);
  110. }
  111. else if(left.type == FOR_PASER){
  112. fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
  113. back_one_token(list, left);
  114. get_base_token(&status, list, for_, new_token);
  115. get_stop_token(status, list);
  116. push_statement(statement_base, new_token);
  117. }
  118. else if(left.type == DEF_PASER || left.type == CLASS_PASER){
  119. fprintf(status_log, "[info][grammar] (command)back one token to (def_class)\n");
  120. back_one_token(list, left);
  121. get_base_token(&status, list, def_class, new_token);
  122. get_stop_token(status, list);
  123. push_statement(statement_base, new_token);
  124. }
  125. else if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  126. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  127. fprintf(status_log, "[info][grammar] (command)back one token to (ctrl_)\n");
  128. back_one_token(list, left);
  129. get_base_token(&status, list, ctrl_, new_token);
  130. get_stop_token(status, list);
  131. push_statement(statement_base, new_token);
  132. }
  133. else if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  134. fprintf(status_log, "[info][grammar] (command)back one token to (var_ctrl_)\n");
  135. back_one_token(list, left);
  136. get_base_token(&status, list, var_ctrl_, new_token);
  137. get_stop_token(status, list);
  138. push_statement(statement_base, new_token);
  139. }
  140. else if(left.type == RETURN_PASER){
  141. fprintf(status_log, "[info][grammar] (command)back one token to (return_)\n");
  142. back_one_token(list, left);
  143. get_base_token(&status, list, return_, new_token);
  144. get_stop_token(status, list);
  145. push_statement(statement_base, new_token);
  146. }
  147. else if(left.type == TRY_PASER){
  148. fprintf(status_log, "[info][grammar] (command)back one token to (try_)\n");
  149. back_one_token(list, left);
  150. get_base_token(&status, list, try_, new_token);
  151. get_stop_token(status, list);
  152. push_statement(statement_base, new_token);
  153. }
  154. else if(left.type == DO_PASER){
  155. fprintf(status_log, "[info][grammar] (command)back one token to (do/do...while)\n");
  156. back_one_token(list, left);
  157. get_base_token(&status, list, do_while_, new_token);
  158. get_stop_token(status, list);
  159. push_statement(statement_base, new_token);
  160. }
  161. else if(left.type == RAISE_PASER || left.type == THROW_PASER || left.type == ASSERT_PASER){
  162. fprintf(status_log, "[info][grammar] (command)back one token to (out_exception)\n");
  163. back_one_token(list, left);
  164. get_base_token(&status, list, out_exception, new_token);
  165. get_stop_token(status, list);
  166. push_statement(statement_base, new_token);
  167. }
  168. else if(left.type == ENTER_PASER){
  169. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  170. }
  171. else if(left.type == EOF_token){
  172. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  173. back_one_token(list, left);
  174. goto return_back;
  175. }
  176. else{ // 表达式
  177. fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
  178. back_one_token(list, left);
  179. status.is_left = true;
  180. get_base_token(&status, list, top_exp, new_token);
  181. status.is_left = false;
  182. if(new_token.type != NON_top_exp){
  183. back_one_token(list, new_token); // 往回[不匹配类型]
  184. return;
  185. }
  186. get_stop_token(status, list);
  187. push_statement(statement_base, new_token);
  188. }
  189. new_token.type = NON_command;
  190. add_node(list, new_token);
  191. return_back:
  192. return; // 回调自己
  193. }
  194. /*
  195. if_ : IF LB top_exp RB block
  196. */
  197. void if_(p_status *status, token_node *list){
  198. fprintf(status_log, "[info][grammar] mode status: if_\n");
  199. token if_t, exp_t, block_t, next_t, child_t, new_token;
  200. if_t = pop_node(list);
  201. if(if_t.type == IF_PASER){
  202. get_right_token(status,list,top_exp,exp_t);
  203. if(exp_t.type != NON_top_exp){ // 不是表达式
  204. paser_error("Don't get 'top_exp'");
  205. }
  206. get_right_token(status,list,block_,block_t);
  207. if(block_t.type != NON_block){ // 不是表达式
  208. paser_error("Don't get '{'");
  209. }
  210. statement *if_tmp = make_statement();
  211. if_tmp->type = if_branch;
  212. if_tmp->code.if_branch.done = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  213. // 检查else和elseif
  214. el_again:
  215. get_pop_token(status,list,next_t);
  216. if(next_t.type == ENTER_PASER){ // 忽略Enter
  217. goto el_again;
  218. }
  219. printf("next_t.type = %d\n", next_t.type);
  220. if(next_t.type == ELIF_PASER || next_t.type == ELSE_PASER){ // elif
  221. back_one_token(list, next_t);
  222. get_base_token(status,list,elif_,child_t);
  223. if(child_t.type == NON_elif){
  224. append_elif(child_t.data.if_list_base, if_tmp->code.if_branch.done);
  225. goto el_again;
  226. }
  227. }
  228. else{
  229. back_again(list, next_t); // 下一次读取需要用safe_get_token
  230. }
  231. new_token.type = NON_if;
  232. new_token.data_type = statement_value;
  233. new_token.data.statement_value = if_tmp;
  234. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  235. token tmp_enter;
  236. tmp_enter.type = ENTER_PASER;
  237. tmp_enter.data_type = empty;
  238. back_again(list, tmp_enter); // push入一个ENTER
  239. return;
  240. }
  241. else{
  242. back_one_token(list, if_t);
  243. return;
  244. }
  245. }
  246. /*
  247. elif_ : ELIF LB top_exp RB block
  248. */
  249. void elif_(p_status *status, token_node *list){
  250. fprintf(status_log, "[info][grammar] mode status: elif_\n");
  251. token elif_t, exp_t, block_t, next_t, new_token;
  252. elif_t = pop_node(list);
  253. if(elif_t.type == ELIF_PASER){
  254. get_right_token(status,list,top_exp,exp_t);
  255. if(exp_t.type != NON_top_exp){ // 不是表达式
  256. paser_error("Don't get 'top_exp'");
  257. }
  258. get_right_token(status,list,block_,block_t);
  259. if(block_t.type != NON_block){ // 不是表达式
  260. paser_error("Don't get '{'");
  261. }
  262. if_list *if_tmp = make_if(exp_t.data.statement_value, block_t.data.statement_value);
  263. new_token.type = NON_elif;
  264. new_token.data_type = if_list_base;
  265. new_token.data.if_list_base = if_tmp;
  266. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  267. return;
  268. }
  269. if(elif_t.type == ELSE_PASER){ // else
  270. get_right_token(status,list,block_,block_t);
  271. if(block_t.type != NON_block){ // 不是表达式
  272. paser_error("Don't get '{'");
  273. }
  274. if_list *if_tmp = make_if(NULL, block_t.data.statement_value);
  275. new_token.type = NON_elif;
  276. new_token.data_type = if_list_base;
  277. new_token.data.if_list_base = if_tmp;
  278. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  279. return;
  280. }
  281. else{
  282. back_one_token(list, elif_t);
  283. return;
  284. }
  285. }
  286. /*
  287. for_ : FOR LB top_exp COMMA top_exp COMMA top_exp RB block
  288. */
  289. void for_(p_status *status, token_node *list){
  290. fprintf(status_log, "[info][grammar] mode status: for_\n");
  291. token for_t, exp_1, exp_2, exp_3, block_t, comma_t,new_token;
  292. statement *exp_a, *exp_b, *exp_c;
  293. for_t = pop_node(list);
  294. if(for_t.type == FOR_PASER){
  295. bool is_for_in = false; // 是否为for in模式
  296. get_pop_token(status, list, exp_1);
  297. if(exp_1.type == COMMA_PASER){
  298. exp_a = NULL; // exp_1 = NULL;
  299. }
  300. else{
  301. back_one_token(list, exp_1);
  302. p_status new_status;
  303. new_status = *status;
  304. new_status.is_for = true;
  305. get_base_token(&new_status,list,top_exp,exp_1); // 不是使用right token,不需要执行safe_get_token
  306. new_status.is_for = false;
  307. if(exp_1.type != NON_top_exp){ // 不是表达式
  308. paser_error("Don't get 'top_exp'");
  309. }
  310. get_pop_token(status, list, comma_t);
  311. if(comma_t.type == IN_PASER){ // for in 模式 -> exp1不可以为NULL
  312. is_for_in = true;
  313. exp_a = exp_1.data.statement_value;
  314. goto exp3;
  315. }
  316. else if(comma_t.type != COMMA_PASER){
  317. paser_error("Don't get ',' in for cycle");
  318. }
  319. exp_a = exp_1.data.statement_value;
  320. }
  321. get_pop_token(status, list, exp_2);
  322. if(exp_2.type == COMMA_PASER){
  323. exp_b = NULL; // exp_1 = NULL;
  324. }
  325. else{
  326. back_one_token(list, exp_2);
  327. p_status new_status;
  328. new_status = *status;
  329. new_status.is_for = true;
  330. get_base_token(&new_status,list,top_exp,exp_2); // 不是使用right token,不需要执行safe_get_token
  331. new_status.is_for = false;
  332. if(exp_2.type != NON_top_exp){ // 不是表达式
  333. paser_error("Don't get 'top_exp'");
  334. }
  335. get_pop_token(status, list, comma_t);
  336. if(comma_t.type != COMMA_PASER){
  337. paser_error("Don't get ',' in for cycle");
  338. }
  339. exp_b = exp_2.data.statement_value;
  340. }
  341. exp3:
  342. get_pop_token(status, list, exp_3);
  343. if(exp_3.type == COMMA_PASER){
  344. if(is_for_in){
  345. paser_error("Don't get iter object");
  346. }
  347. exp_c = NULL; // exp_1 = NULL;
  348. }
  349. else{
  350. back_one_token(list, exp_3);
  351. p_status new_status;
  352. new_status = *status;
  353. if(!is_for_in) new_status.is_for = true;
  354. get_base_token(&new_status,list,top_exp,exp_3); // 不是使用right token,不需要执行safe_get_token
  355. if(!is_for_in) new_status.is_for = false;
  356. if(exp_3.type != NON_top_exp){ // 不是表达式
  357. paser_error("Don't get 'top_exp'");
  358. }
  359. if(!is_for_in){ // for in 不需要匹配“,”
  360. get_pop_token(status, list, comma_t);
  361. if(comma_t.type != COMMA_PASER){
  362. paser_error("Don't get ',' in for cycle");
  363. }
  364. }
  365. exp_c = exp_3.data.statement_value;
  366. }
  367. get_right_token(status,list,block_,block_t);
  368. if(block_t.type != NON_block){ // 不是表达式
  369. paser_error("Don't get '{'");
  370. }
  371. statement *for_tmp = make_statement();
  372. if(is_for_in){
  373. for_tmp->type = for_in_cycle;
  374. for_tmp->code.for_in_cycle.var = exp_a;
  375. for_tmp->code.for_in_cycle.iter = exp_c;
  376. for_tmp->code.for_in_cycle.done = block_t.data.statement_value;
  377. }
  378. else{
  379. for_tmp->type = for_cycle;
  380. for_tmp->code.for_cycle.first = exp_a;
  381. for_tmp->code.for_cycle.condition = exp_b;
  382. for_tmp->code.for_cycle.after = exp_c;
  383. for_tmp->code.for_cycle.done = block_t.data.statement_value;
  384. }
  385. new_token.type = NON_for;
  386. new_token.data_type = statement_value;
  387. new_token.data.statement_value = for_tmp;
  388. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  389. return;
  390. }
  391. else{
  392. back_one_token(list, for_t);
  393. return;
  394. }
  395. }
  396. /*
  397. def_ : DEF LB RB block
  398. */
  399. void def_class(p_status *status, token_node *list){
  400. fprintf(status_log, "[info][grammar] mode status: def_\n");
  401. token def_t, lb_t, rb_t, block_t, name_t, parameter_t, new_token;
  402. parameter *p_list;
  403. def_t = pop_node(list);
  404. if(def_t.type == DEF_PASER || def_t.type == CLASS_PASER){
  405. p_status new_status;
  406. new_status = *status;
  407. new_status.is_func = true;
  408. get_right_token(&new_status,list,top_exp,name_t); // 避免了top_exp把括号捕捉为call_back,不过,可以使用list设置status参数从而让call_back不捕捉[未实现]
  409. new_status.is_func = false;
  410. if(name_t.type != NON_top_exp){ // 不是表达式
  411. paser_error("Don't get 'top_exp'");
  412. }
  413. get_pop_token(status, list, lb_t);
  414. if(lb_t.type != LB_PASER){
  415. printf("lb_t.type = %d\n", lb_t.type);
  416. paser_error("Don't get '('");
  417. }
  418. get_pop_token(status, list, rb_t);
  419. if(rb_t.type != RB_PASER){ // 带参数
  420. back_one_token(list, rb_t);
  421. get_base_token(status,list,formal_parameter,parameter_t); // 不需要safe_get_token
  422. if(parameter_t.type != NON_parameter){
  423. paser_error("Don't get formal_parameter");
  424. }
  425. get_pop_token(status, list, rb_t);
  426. printf("rb_t.type = %d\n", rb_t.type);
  427. if(rb_t.type != RB_PASER){
  428. paser_error("Don't get ')'[1]");
  429. }
  430. p_list = parameter_t.data.parameter_list;
  431. }
  432. else{
  433. p_list = NULL; // 没参数
  434. }
  435. get_right_token(status,list,block_,block_t);
  436. if(block_t.type != NON_block){ // 不是表达式
  437. paser_error("Don't get '{'");
  438. }
  439. statement *def_tmp = make_statement();
  440. if(def_t.type == DEF_PASER){
  441. def_tmp->type = def;
  442. def_tmp->code.def.var = name_t.data.statement_value;
  443. def_tmp->code.def.parameter_list = p_list;
  444. def_tmp->code.def.done = block_t.data.statement_value;
  445. }
  446. else{
  447. def_tmp->type = set_class;
  448. def_tmp->code.set_class.var = name_t.data.statement_value;
  449. def_tmp->code.set_class.father_list = p_list;
  450. def_tmp->code.set_class.done = block_t.data.statement_value;
  451. }
  452. new_token.type = NON_def;
  453. new_token.data_type = statement_value;
  454. new_token.data.statement_value = def_tmp;
  455. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  456. return;
  457. }
  458. else{
  459. back_one_token(list, def_t);
  460. return;
  461. }
  462. }
  463. void formal_parameter(p_status *status, token_node *list){ // 因试分解
  464. fprintf(status_log, "[info][grammar] mode status: formal_parameter\n");
  465. token left, next, comma, before, eq, value_token, new_token;
  466. int mode = only_value;
  467. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  468. if(left.type == NON_parameter){ // 模式2/3
  469. fprintf(status_log, "[info][grammar] (formal_parameter)reduce right\n");
  470. get_pop_token(status, list, comma);
  471. if(comma.type == (status->is_slice ? COLON_PASER : COMMA_PASER)){
  472. get_pop_token(status, list, before);
  473. if(before.type == MUL_PASER && !status->is_peq){
  474. if(status->is_dict){
  475. paser_error("dict shouldn't get '*'");
  476. }
  477. mode = put_args;
  478. }
  479. else if(before.type == POW_PASER && !status->is_peq){
  480. if(status->is_list){
  481. paser_error("list shouldn't get '*'");
  482. }
  483. mode = put_kwargs;
  484. }
  485. else{
  486. mode = only_value;
  487. back_again(list,before); // 回退
  488. }
  489. p_status new_status;
  490. new_status = *status;
  491. new_status.is_parameter = true;
  492. get_right_token(&new_status, list, top_exp, next);
  493. new_status.is_parameter = false;
  494. if(next.type != NON_top_exp){ // 结尾分号
  495. back_one_token(list, left); // 分号忽略
  496. back_again(list, next);
  497. return;
  498. }
  499. new_token = left;
  500. parameter *tmp = NULL;
  501. get_pop_token(status, list, eq);
  502. if(eq.type == ((status->is_dict || status->is_peq) ? COLON_PASER : EQ_PASER)){ // name_value模式
  503. if(status->is_list){
  504. paser_error("list shouldn't get '='");
  505. }
  506. p_status new_status;
  507. new_status = *status;
  508. if(status->is_peq) new_status.is_parameter = true;
  509. get_right_token(&new_status, list, top_exp, value_token);
  510. if(status->is_peq) new_status.is_parameter = false;
  511. if(value_token.type != NON_top_exp){
  512. paser_error("Don't get a top_exp");
  513. return;
  514. }
  515. tmp = append_parameter_name(next.data.statement_value, new_token.data.parameter_list);
  516. tmp->type = name_value;
  517. tmp->u.value = value_token.data.statement_value;
  518. }
  519. else{
  520. if(status->is_dict){
  521. paser_error("dict should get ':'[1]");
  522. }
  523. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  524. tmp = append_parameter_value(next.data.statement_value, new_token.data.parameter_list);
  525. tmp->type = mode;
  526. tmp->u.var = tmp->u.value;
  527. }
  528. add_node(list, new_token);
  529. return formal_parameter(status, list); // 回调自己
  530. }
  531. else{ // 递归跳出
  532. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  533. fprintf(status_log, "[info][grammar] (formal_parameter)out\n");
  534. back_one_token(list, left);
  535. back_again(list, comma);
  536. return;
  537. }
  538. }
  539. else if(left.type == POW_PASER || left.type == MUL_PASER){ // 模式1
  540. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)[**/*]\n");
  541. p_status new_status;
  542. new_status = *status;
  543. new_status.is_parameter = true;
  544. get_right_token(&new_status, list, top_exp, next); // 不需要back_one_token
  545. new_status.is_parameter = false;
  546. if(next.type != NON_top_exp){
  547. back_one_token(list, next); // 往回[不匹配类型]
  548. return;
  549. }
  550. new_token.type = NON_parameter;
  551. new_token.data_type = parameter_list;
  552. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  553. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  554. if(left.type == POW_PASER){
  555. if(status->is_list){
  556. paser_error("list shouldn't get '**'");
  557. }
  558. new_token.data.parameter_list->type = put_kwargs;
  559. }
  560. else{
  561. if(status->is_dict){
  562. paser_error("dict shouldn't get '*'");
  563. }
  564. new_token.data.parameter_list->type = put_args;
  565. }
  566. add_node(list, new_token);
  567. return formal_parameter(status, list); // 回调自己
  568. }
  569. else{ // 模式1
  570. fprintf(status_log, "[info][grammar] (formal_parameter)back one token to (top_exp)\n");
  571. back_one_token(list, left);
  572. printf("left.type = %d\n", left.type);
  573. p_status new_status;
  574. new_status = *status;
  575. new_status.is_parameter = true;
  576. get_base_token(&new_status, list, top_exp, next);
  577. new_status.is_parameter = false;
  578. if(next.type != NON_top_exp){
  579. back_one_token(list, next); // 往回[不匹配类型]
  580. return;
  581. }
  582. new_token.type = NON_parameter;
  583. new_token.data_type = parameter_list;
  584. parameter *tmp = NULL;
  585. get_pop_token(status, list, eq);
  586. printf("eq.type = %d\n", eq.type);
  587. if(eq.type == ((status->is_dict || status->is_peq) ? COLON_PASER : EQ_PASER)){ // name_value模式
  588. if(status->is_list){
  589. paser_error("list shouldn't get '='");
  590. }
  591. p_status new_status;
  592. new_status = *status;
  593. if(status->is_peq) new_status.is_parameter = false;
  594. get_right_token(&new_status, list, top_exp, value_token);
  595. if(status->is_peq) new_status.is_parameter = true;
  596. if(value_token.type != NON_top_exp){
  597. paser_error("Don't get a top_exp");
  598. return;
  599. }
  600. new_token.data.parameter_list = make_parameter_name(next.data.statement_value);
  601. new_token.data.parameter_list->u.value = value_token.data.statement_value;
  602. new_token.data.parameter_list->type = name_value;
  603. }
  604. else{
  605. if(status->is_dict){
  606. paser_error("dict should get ':'[2]");
  607. }
  608. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  609. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  610. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  611. new_token.data.parameter_list->type = only_value;
  612. }
  613. add_node(list, new_token);
  614. return formal_parameter(status, list); // 回调自己
  615. }
  616. }
  617. /*
  618. while_ : WHILE LB top_exp RB block
  619. */
  620. void do_while_(p_status *status, token_node *list){
  621. fprintf(status_log, "[info][grammar] mode status: while_\n");
  622. token do_t, while_t, exp_t, block_t, new_token;
  623. do_t = pop_node(list);
  624. if(do_t.type == DO_PASER){
  625. get_right_token(status,list,block_,block_t);
  626. if(block_t.type != NON_block){ // 不是表达式
  627. paser_error("Don't get '{'");
  628. }
  629. statement *tmp = make_statement();
  630. get_pop_token(status, list, while_t);
  631. if(while_t.type != WHILE_PASER){ // block模式
  632. back_again(list,while_t);
  633. tmp->type = code_block;
  634. tmp->code.code_block.done = block_t.data.statement_value;
  635. }
  636. else{
  637. get_right_token(status,list,top_exp,exp_t);
  638. if(exp_t.type != NON_top_exp){ // 不是表达式
  639. paser_error("Don't get 'top_exp'");
  640. }
  641. tmp->type = while_cycle;
  642. tmp->code.while_cycle.condition = exp_t.data.statement_value;
  643. tmp->code.while_cycle.done = block_t.data.statement_value;
  644. tmp->code.while_cycle.first_do = true;
  645. }
  646. new_token.type = NON_do_while;
  647. new_token.data_type = statement_value;
  648. new_token.data.statement_value = tmp;
  649. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  650. return;
  651. }
  652. else{
  653. back_one_token(list, do_t);
  654. return;
  655. }
  656. }
  657. /*
  658. while_ : WHILE LB top_exp RB block
  659. */
  660. void while_(p_status *status, token_node *list){
  661. fprintf(status_log, "[info][grammar] mode status: while_\n");
  662. token while_t, exp_t, block_t, new_token;
  663. while_t = pop_node(list);
  664. if(while_t.type == WHILE_PASER){
  665. get_right_token(status,list,top_exp,exp_t);
  666. if(exp_t.type != NON_top_exp){ // 不是表达式
  667. paser_error("Don't get 'top_exp'");
  668. }
  669. get_right_token(status,list,block_,block_t);
  670. if(block_t.type != NON_block){ // 不是表达式
  671. paser_error("Don't get '{'");
  672. }
  673. statement *while_tmp = make_statement();
  674. while_tmp->type = while_cycle;
  675. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  676. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  677. while_tmp->code.while_cycle.first_do = false;
  678. new_token.type = NON_while;
  679. new_token.data_type = statement_value;
  680. new_token.data.statement_value = while_tmp;
  681. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  682. return;
  683. }
  684. else{
  685. back_one_token(list, while_t);
  686. return;
  687. }
  688. }
  689. /*
  690. try_ : TRY block EXCEPT AS top_exp block
  691. */
  692. void try_(p_status *status, token_node *list){
  693. fprintf(status_log, "[info][grammar] mode status: while_\n");
  694. token try_t, try_block, except_t, as_t, var_t, except_block, new_token;
  695. try_t = pop_node(list);
  696. if(try_t.type == TRY_PASER){
  697. get_right_token(status, list, block_, try_block);
  698. if(try_block.type != NON_block){
  699. paser_error("Don't get '{'");
  700. }
  701. except_again:
  702. get_pop_token(status,list,except_t);
  703. if(except_t.type == ENTER_PASER){
  704. goto except_again;
  705. }
  706. else if(except_t.type != EXCEPT_PASER){ // 不是except
  707. paser_error("Don't get 'except'");
  708. }
  709. get_pop_token(status,list,as_t);
  710. if(as_t.type != AS_PASER){ // 不是except
  711. paser_error("Don't get 'as'");
  712. }
  713. get_right_token(status, list, top_exp, var_t);
  714. if(var_t.type != NON_top_exp){
  715. paser_error("Don't get top_exp");
  716. }
  717. get_right_token(status,list,block_,except_block);
  718. if(except_block.type != NON_block){ // 不是表达式
  719. paser_error("Don't get '{'");
  720. }
  721. statement *try_tmp = make_statement();
  722. try_tmp->type = try_code;
  723. try_tmp->code.try_code.try = try_block.data.statement_value;
  724. try_tmp->code.try_code.except = except_block.data.statement_value;
  725. try_tmp->code.try_code.var = var_t.data.statement_value;
  726. new_token.type = NON_try;
  727. new_token.data_type = statement_value;
  728. new_token.data.statement_value = try_tmp;
  729. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  730. return;
  731. }
  732. else{
  733. back_one_token(list, try_t);
  734. return;
  735. }
  736. }
  737. /*
  738. block_ : LP command_list RB
  739. */
  740. void block_(p_status *status, token_node *list){
  741. fprintf(status_log, "[info][grammar] mode status: block_\n");
  742. token lp_t, rp_t, new_token, command_list_t;
  743. lp_t = pop_node(list);
  744. again:
  745. if(lp_t.type == LP_PASER){
  746. statement *block_tmp = make_statement();
  747. statement_base = append_statement_list(block_tmp, statement_base);
  748. p_status new_status = *status; // 继承file_p等值
  749. reset_status(new_status); // 不会影响 *staus
  750. new_status.dict_to_enter = true;
  751. get_right_token(&new_status,list,command_list,command_list_t); // 要把command_list也弹出来
  752. statement_base = free_statement_list(statement_base); // 重新释放
  753. get_pop_token(status, list, rp_t);
  754. if(rp_t.type != RP_PASER){
  755. paser_error("Don't get '}'");
  756. }
  757. new_token.type = NON_block;
  758. new_token.data_type = statement_value;
  759. new_token.data.statement_value = block_tmp;
  760. add_node(list, new_token); // 压入节点
  761. return;
  762. }
  763. if(lp_t.type == ENTER_PASER){
  764. get_pop_token(status,list,lp_t);
  765. goto again;
  766. }
  767. else{
  768. printf("lp_t.type = %d != %d\n", lp_t.type, LP_PASER);
  769. back_one_token(list, lp_t);
  770. return;
  771. }
  772. }
  773. // var_ctrl_ 包含诸如:global, nolocal,之类的
  774. void var_ctrl_(p_status *status, token_node *list){
  775. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  776. token left, var, right, new_token;
  777. statement *times = NULL;
  778. char *var_name = NULL;
  779. left = pop_node(list);
  780. if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  781. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  782. get_right_token(status, list, top_exp, var); // 取得base_var
  783. if(var.type != NON_top_exp && var.data.statement_value->type != base_var){
  784. paser_error("Don't get var");
  785. }
  786. else{
  787. var_name = malloc(sizeof(var.data.statement_value->code.base_var.var_name));
  788. strcpy(var_name, var.data.statement_value->code.base_var.var_name);
  789. times = var.data.statement_value->code.base_var.from;
  790. // TODO:: 本质上没有完全释放
  791. free(var.data.statement_value->code.base_var.var_name);
  792. free(var.data.statement_value);
  793. }
  794. if(left.type == DEFAULT_PASER){ // 设置times
  795. get_right_token(status, list, top_exp, right); // 回调右边
  796. if(right.type != NON_top_exp){
  797. back_again(list, right); // 不是期望的数字
  798. }
  799. else{
  800. times = right.data.statement_value;
  801. }
  802. }
  803. // 逻辑操作
  804. new_token.type = NON_ctrl;
  805. new_token.data_type = statement_value;
  806. statement *code_tmp = make_statement();
  807. switch (left.type)
  808. {
  809. case GLOBAL_PASER:
  810. code_tmp->type = set_global;
  811. code_tmp->code.set_global.name = var_name;
  812. break;
  813. case DEFAULT_PASER:
  814. code_tmp->type = set_default;
  815. code_tmp->code.set_default.name = var_name;
  816. code_tmp->code.set_default.times = times;
  817. break;
  818. case NOLOCAL_PASER:
  819. code_tmp->type = set_nonlocal;
  820. code_tmp->code.set_nonlocal.name = var_name;
  821. break;
  822. default:
  823. break;
  824. }
  825. new_token.data.statement_value = code_tmp;
  826. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  827. return; // 回调自己
  828. }
  829. else{ // 模式1
  830. back_one_token(list, left);
  831. return;
  832. }
  833. }
  834. // raise throw 和 assert
  835. void out_exception(p_status *status, token_node *list){
  836. fprintf(status_log, "[info][grammar] mode status: out_exception\n");
  837. token left, exp1, exp2, new_token;
  838. left = pop_node(list);
  839. if(left.type == RAISE_PASER || left.type == THROW_PASER || left.type == ASSERT_PASER){
  840. fprintf(status_log, "[info][grammar] (out_exception)reduce right\n");
  841. get_right_token(status, list, top_exp, exp1); // 取得base_var
  842. if(exp1.type != NON_top_exp){
  843. paser_error("Don't get top_exp");
  844. }
  845. if(left.type != THROW_PASER){ // 设置times
  846. get_right_token(status, list, top_exp, exp2); // 回调右边
  847. if(exp2.type != NON_top_exp){
  848. paser_error("Don't get top_exp");
  849. }
  850. }
  851. // 逻辑操作
  852. new_token.type = NON_exception;
  853. new_token.data_type = statement_value;
  854. statement *code_tmp = make_statement();
  855. switch (left.type)
  856. {
  857. case RAISE_PASER:
  858. code_tmp->type = raise_e;
  859. code_tmp->code.raise_e.done = exp1.data.statement_value;
  860. code_tmp->code.raise_e.info = exp2.data.statement_value;
  861. break;
  862. case THROW_PASER:
  863. code_tmp->type = throw_e;
  864. code_tmp->code.throw_e.done = exp1.data.statement_value;
  865. break;
  866. case ASSERT_PASER:
  867. code_tmp->type = assert_e;
  868. code_tmp->code.assert_e.condition = exp1.data.statement_value;
  869. code_tmp->code.assert_e.info = exp2.data.statement_value;
  870. break;
  871. default:
  872. break;
  873. }
  874. new_token.data.statement_value = code_tmp;
  875. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  876. return; // 回调自己
  877. }
  878. else{ // 模式1
  879. back_one_token(list, left);
  880. return;
  881. }
  882. }
  883. void return_(p_status *status, token_node *list){
  884. fprintf(status_log, "[info][grammar] mode status: return_\n");
  885. token left, right, value, new_token;
  886. statement *times = NULL, *back_value = NULL;
  887. left = pop_node(list);
  888. if(left.type == RETURN_PASER){
  889. fprintf(status_log, "[info][grammar] (return_)reduce right\n");
  890. get_right_token(status, list, top_exp, value); // 回调右边
  891. if(value.type != NON_top_exp){
  892. back_again(list, value); // 不是期望的数字,就默认使用NULL,并且回退
  893. times = NULL;
  894. back_value = NULL;
  895. goto not_times;
  896. }
  897. else{
  898. back_value = value.data.statement_value;
  899. }
  900. get_right_token(status, list, top_exp, right); // 回调右边
  901. if(right.type != NON_top_exp){
  902. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  903. times = NULL;
  904. }
  905. else{
  906. times = right.data.statement_value;
  907. }
  908. not_times:
  909. new_token.type = NON_return;
  910. new_token.data_type = statement_value;
  911. statement *code_tmp = make_statement();
  912. code_tmp->type = return_code;
  913. code_tmp->code.return_code.times = times;
  914. code_tmp->code.return_code.value = back_value;
  915. new_token.data.statement_value = code_tmp;
  916. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  917. return; // 回调自己
  918. }
  919. else{ // 模式1
  920. back_one_token(list, left);
  921. return;
  922. }
  923. }
  924. // ctrl_包含诸如:break,broken,之类的
  925. void ctrl_(p_status *status, token_node *list){
  926. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  927. token left, right, new_token;
  928. statement *times = NULL;
  929. left = pop_node(list);
  930. if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  931. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  932. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  933. if(left.type != REGO_PASER && left.type != REWENT_PASER){
  934. get_right_token(status, list, top_exp, right); // 回调右边
  935. if(right.type != NON_top_exp){
  936. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  937. times = NULL;
  938. }
  939. else{
  940. times = right.data.statement_value;
  941. }
  942. }
  943. // 逻辑操作
  944. new_token.type = NON_ctrl;
  945. new_token.data_type = statement_value;
  946. statement *code_tmp = make_statement();
  947. switch (left.type)
  948. {
  949. case BREAK_PASER:
  950. code_tmp->type = break_cycle;
  951. code_tmp->code.break_cycle.times = times;
  952. break;
  953. case BROKEN_PASER:
  954. code_tmp->type = broken;
  955. code_tmp->code.broken.times = times;
  956. break;
  957. case CONTINUE_PASER:
  958. code_tmp->type = continue_cycle;
  959. code_tmp->code.continue_cycle.times = times;
  960. break;
  961. case CONTINUED_PASER:
  962. code_tmp->type = continued;
  963. code_tmp->code.continued.times = times;
  964. break;
  965. case RESTART_PASER:
  966. code_tmp->type = restart;
  967. code_tmp->code.restart.times = times;
  968. break;
  969. case RESTARTED_PASER:
  970. code_tmp->type = restarted;
  971. code_tmp->code.restarted.times = times;
  972. break;
  973. case REGO_PASER:
  974. code_tmp->type = rego;
  975. break;
  976. case REWENT_PASER:
  977. code_tmp->type = rewent;
  978. break;
  979. default:
  980. break;
  981. }
  982. new_token.data.statement_value = code_tmp;
  983. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  984. return; // 回调自己
  985. }
  986. else{ // 模式1
  987. back_one_token(list, left);
  988. return;
  989. }
  990. }
  991. /*
  992. top_exp : polynomial
  993. */
  994. void top_exp(p_status *status, token_node *list){
  995. fprintf(status_log, "[info][grammar] mode status: top_exp\n");
  996. token exp;
  997. get_base_token(status,list,eq_number,exp);
  998. if(exp.type != NON_eq){
  999. back_one_token(list, exp);
  1000. return;
  1001. }
  1002. exp.type = NON_top_exp;
  1003. add_node(list, exp); // 压入节点
  1004. return;
  1005. }
  1006. /*
  1007. eq_number : bool_or
  1008. | eq_number AND bool_or
  1009. */
  1010. void eq_number(p_status *status, token_node *list){ // 因试分解
  1011. fprintf(status_log, "[info][grammar] mode status: eq_number\n");
  1012. token left, right, symbol, new_token;
  1013. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1014. if(left.type == NON_eq){ // 模式2/3
  1015. fprintf(status_log, "[info][grammar] (eq_number)reduce right\n");
  1016. get_pop_token(status, list, symbol);
  1017. if((symbol.type == EQ_PASER && !status->is_parameter) || symbol.type == AADD_PASER || symbol.type == ASUB_PASER || symbol.type == AMUL_PASER || symbol.type == ADIV_PASER||
  1018. symbol.type == AMOD_PASER || symbol.type == AINTDIV_PASER || symbol.type == APOW_PASER){ // 模式2/3
  1019. get_right_token(status, list, hide_list, right); // 回调右边
  1020. if(right.type != NON_hide_list){
  1021. paser_error("Don't get a hide_list");
  1022. }
  1023. // 逻辑操作
  1024. new_token.type = NON_eq;
  1025. new_token.data_type = statement_value;
  1026. statement *code_tmp = make_statement();
  1027. code_tmp->type = operation;
  1028. switch (symbol.type)
  1029. {
  1030. case EQ_PASER:
  1031. code_tmp->code.operation.type = ASSIGnMENT_func;
  1032. break;
  1033. case AADD_PASER:
  1034. code_tmp->code.operation.type = AADD_func;
  1035. break;
  1036. case ASUB_PASER:
  1037. code_tmp->code.operation.type = ASUB_func;
  1038. break;
  1039. case AMUL_PASER:
  1040. code_tmp->code.operation.type = AMUL_func;
  1041. break;
  1042. case ADIV_PASER:
  1043. code_tmp->code.operation.type = ADIV_func;
  1044. break;
  1045. case AMOD_PASER:
  1046. code_tmp->code.operation.type = AMOD_func;
  1047. break;
  1048. case AINTDIV_PASER:
  1049. code_tmp->code.operation.type = AINTDIV_func;
  1050. break;
  1051. case APOW_PASER:
  1052. code_tmp->code.operation.type = APOW_func;
  1053. break;
  1054. }
  1055. code_tmp->code.operation.left_exp = left.data.statement_value;
  1056. code_tmp->code.operation.right_exp = right.data.statement_value;
  1057. new_token.data.statement_value = code_tmp;
  1058. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1059. return eq_number(status, list); // 回调自己
  1060. }
  1061. else{ // 递归跳出
  1062. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1063. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1064. back_one_token(list, left);
  1065. back_again(list, symbol);
  1066. return;
  1067. }
  1068. }
  1069. else{ // 模式1
  1070. // is_left将会在这里消亡为false
  1071. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  1072. back_one_token(list, left);
  1073. get_base_token(status, list, hide_list, new_token); // hide_list会处理is_left
  1074. if(new_token.type != NON_hide_list){
  1075. back_one_token(list, new_token); // 往回[不匹配类型]
  1076. return;
  1077. }
  1078. if(status->is_left){ // 必须是在最左边
  1079. p_status new_status;
  1080. new_status = *status;
  1081. new_status.is_left = false; // 设置为false,随后一切top_exp行为均不执行这一步骤
  1082. token comma, p_left, eq_t, p_right, tmp;
  1083. parameter *the_right;
  1084. get_pop_token(&new_status, list,comma);
  1085. if(comma.type == COMMA_PASER){ // a,b = [1,2]的赋值方式
  1086. back_one_token(list, new_token); // 先把new_token和comma一起回退
  1087. back_again(list, comma);
  1088. new_status.is_peq = true;
  1089. get_base_token(&new_status,list,formal_parameter,p_left);
  1090. if(p_left.type != NON_parameter){
  1091. paser_error("Dont get formal_parameter");
  1092. }
  1093. new_status.is_peq = false;
  1094. get_pop_token(status, list, eq_t);
  1095. if(eq_t.type != EQ_PASER){ // 变成hide_list
  1096. back_again(list, eq_t);
  1097. statement *code_tmp = make_statement();
  1098. code_tmp->type = base_tuple;
  1099. code_tmp->code.base_tuple.value = p_left.data.parameter_list;
  1100. new_token.data.statement_value = code_tmp;
  1101. new_token.data_type = statement_value;
  1102. goto return_back;
  1103. }
  1104. get_pop_token(status,list,tmp);
  1105. if(tmp.type == POW_PASER || tmp.type == MUL_PASER || tmp.type == COLON_PASER){
  1106. if(tmp.type != COLON_PASER){
  1107. back_again(list, tmp);
  1108. }
  1109. get_right_token(status,list,formal_parameter,p_right);
  1110. if(p_right.type != NON_parameter){
  1111. paser_error("Don't get formal_parameter");
  1112. }
  1113. the_right = p_right.data.parameter_list;
  1114. }
  1115. else{
  1116. back_again(list, tmp);
  1117. get_right_token(status,list,hide_list,p_right);
  1118. if(p_right.type != NON_hide_list){
  1119. paser_error("Don't get hide_list");
  1120. }
  1121. the_right = make_parameter_value(p_right.data.statement_value);
  1122. the_right->type = put_args;
  1123. the_right->u.var = the_right->u.value;
  1124. }
  1125. new_token.data_type = statement_value;
  1126. new_token.data.statement_value = make_statement();
  1127. new_token.data.statement_value->type = pack_eq;
  1128. new_token.data.statement_value->code.pack_eq.left = p_left.data.parameter_list;
  1129. new_token.data.statement_value->code.pack_eq.right = the_right;
  1130. }
  1131. else{
  1132. back_again(list, comma); // 回退comma 使用back_again目的是让new_token在comma之前
  1133. }
  1134. }
  1135. return_back:
  1136. new_token.type = NON_eq;
  1137. add_node(list, new_token);
  1138. return eq_number(status, list); // 回调自己
  1139. }
  1140. }
  1141. void hide_list(p_status *status, token_node *list){
  1142. fprintf(status_log, "[info][grammar] mode status: hide_list\n");
  1143. token exp;
  1144. bool old_is_left = status->is_left;
  1145. p_status new_status;
  1146. new_status = *status;
  1147. new_status.is_left = false;
  1148. get_base_token(&new_status,list,call_back_,exp);
  1149. new_status.is_left = old_is_left;
  1150. if(exp.type != NON_call){
  1151. back_one_token(list, exp);
  1152. return;
  1153. }
  1154. if(status->is_func + status->is_left + status->is_parameter + status->is_for + status->is_list + status->is_dict == 0){ // 几个会用到逗号的选项先排除
  1155. token comma;
  1156. get_pop_token(status,list,comma);
  1157. printf("comma.type = %d\n", comma.type);
  1158. if(comma.type == COMMA_PASER){
  1159. token new_token;
  1160. back_one_token(list, exp);
  1161. back_again(list, comma);
  1162. p_status new_status;
  1163. new_status = *status;
  1164. new_status.is_list = true;
  1165. get_base_token(&new_status, list, formal_parameter, new_token);
  1166. new_status.is_list = false;
  1167. if(new_token.type != NON_parameter){
  1168. paser_error("Don't get formal_parameter");
  1169. }
  1170. statement *code_tmp = make_statement();
  1171. code_tmp->type = base_tuple;
  1172. code_tmp->code.base_tuple.value = new_token.data.parameter_list;
  1173. exp.data.statement_value = code_tmp;
  1174. exp.data_type = statement_value;
  1175. }
  1176. else{
  1177. back_again(list, comma);
  1178. }
  1179. }
  1180. exp.type = NON_hide_list;
  1181. add_node(list, exp); // 压入节点
  1182. return;
  1183. }
  1184. /*
  1185. call_back_ : bool_or
  1186. | call_back_ LB RB
  1187. */
  1188. void call_back_(p_status *status, token_node *list){ // 因试分解
  1189. fprintf(status_log, "[info][grammar] mode status: call_back_\n");
  1190. token left, symbol, rb_t, new_token, parameter_t;
  1191. parameter *p_list;
  1192. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1193. if(left.type == NON_call){
  1194. fprintf(status_log, "[info][grammar] (call_back_)reduce right\n");
  1195. get_pop_token(status, list, symbol);
  1196. if(symbol.type == LB_PASER && !status->is_func){
  1197. get_pop_token(status, list, rb_t);
  1198. if(rb_t.type != RB_PASER){ // 带参数
  1199. back_again(list, rb_t);
  1200. get_right_token(status,list,formal_parameter,parameter_t);
  1201. if(parameter_t.type != NON_parameter){
  1202. paser_error("Don't get formal_parameter");
  1203. }
  1204. printf("rb_t.type = %d\n", rb_t.type);
  1205. get_pop_token(status, list, rb_t);
  1206. if(rb_t.type != RB_PASER){
  1207. paser_error("Don't get ')'[2]");
  1208. }
  1209. p_list = parameter_t.data.parameter_list;
  1210. }
  1211. else{
  1212. p_list = NULL; // 没参数
  1213. }
  1214. // 逻辑操作
  1215. new_token.type = NON_call;
  1216. new_token.data_type = statement_value;
  1217. statement *code_tmp = make_statement();
  1218. code_tmp->type = call;
  1219. code_tmp->code.call.func = left.data.statement_value;
  1220. code_tmp->code.call.parameter_list = p_list;
  1221. new_token.data.statement_value = code_tmp;
  1222. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1223. return call_back_(status, list); // 回调自己
  1224. }
  1225. else{ // 递归跳出
  1226. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1227. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  1228. back_one_token(list, left);
  1229. back_again(list, symbol);
  1230. return;
  1231. }
  1232. }
  1233. else{ // 模式1
  1234. fprintf(status_log, "[info][grammar] (call_back_)back one token to (lambda_)\n");
  1235. back_one_token(list, left);
  1236. get_base_token(status, list, lambda_, new_token);
  1237. if(new_token.type != NON_lambda){
  1238. back_one_token(list, new_token); // 往回[不匹配类型]
  1239. return;
  1240. }
  1241. new_token.type = NON_call;
  1242. add_node(list, new_token);
  1243. return call_back_(status, list); // 回调自己
  1244. }
  1245. }
  1246. void lambda_(p_status *status, token_node *list){
  1247. fprintf(status_log, "[info][grammar] mode status: lambda_\n");
  1248. token left, lambda_t, block_t, symbol, new_token;
  1249. left = pop_node(list);
  1250. if(left.type != NON_bool_or){
  1251. back_one_token(list, left);
  1252. get_base_token(status, list, bool_or, left);
  1253. if(left.type != NON_bool_or){
  1254. back_one_token(list, left);
  1255. return;
  1256. }
  1257. }
  1258. get_pop_token(status, list, lambda_t);
  1259. if(lambda_t.type != LAMBDA_PASER){ // 不是lambda表达式
  1260. left.type = NON_lambda;
  1261. back_one_token(list, left);
  1262. back_again(list, lambda_t);
  1263. return;
  1264. }
  1265. get_right_token(status, list, block_, block_t);
  1266. if(block_t.type != NON_block){
  1267. paser_error("Do't get '{");
  1268. }
  1269. statement *lambda_tmp = make_statement();
  1270. lambda_tmp->type = lambda_func;
  1271. if(left.data.statement_value->type == base_tuple){
  1272. lambda_tmp->code.lambda_func.parameter_list = left.data.statement_value->code.base_tuple.value;
  1273. }
  1274. else{
  1275. lambda_tmp->code.lambda_func.parameter_list = NULL;
  1276. }
  1277. free(left.data.statement_value);
  1278. lambda_tmp->code.lambda_func.done = block_t.data.statement_value;
  1279. new_token.type = NON_lambda;
  1280. new_token.data_type = statement_value;
  1281. new_token.data.statement_value = lambda_tmp;
  1282. add_node(list, new_token);
  1283. return;
  1284. }
  1285. /*
  1286. bool_or : bool_and
  1287. | bool_or AND bool_and
  1288. */
  1289. void bool_or(p_status *status, token_node *list){ // 因试分解
  1290. fprintf(status_log, "[info][grammar] mode status: bool_or\n");
  1291. token left, right, symbol, new_token;
  1292. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1293. if(left.type == NON_bool_or){ // 模式2/3
  1294. fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
  1295. get_pop_token(status, list, symbol);
  1296. if(symbol.type == OR_PASER){ // 模式2/3
  1297. get_right_token(status, list, bool_and, right); // 回调右边
  1298. if(right.type != NON_bool_and){
  1299. paser_error("Don't get a compare");
  1300. }
  1301. // 逻辑操作
  1302. new_token.type = NON_bool_or;
  1303. new_token.data_type = statement_value;
  1304. statement *code_tmp = make_statement();
  1305. code_tmp->type = operation;
  1306. code_tmp->code.operation.type = OR_func;
  1307. code_tmp->code.operation.left_exp = left.data.statement_value;
  1308. code_tmp->code.operation.right_exp = right.data.statement_value;
  1309. new_token.data.statement_value = code_tmp;
  1310. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1311. return bool_or(status, list); // 回调自己
  1312. }
  1313. else{ // 递归跳出
  1314. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1315. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1316. back_one_token(list, left);
  1317. back_again(list, symbol);
  1318. return;
  1319. }
  1320. }
  1321. else{ // 模式1
  1322. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  1323. back_one_token(list, left);
  1324. get_base_token(status, list, bool_and, new_token);
  1325. if(new_token.type != NON_bool_and){
  1326. back_one_token(list, new_token); // 往回[不匹配类型]
  1327. return;
  1328. }
  1329. new_token.type = NON_bool_or;
  1330. add_node(list, new_token);
  1331. return bool_or(status, list); // 回调自己
  1332. }
  1333. }
  1334. /*
  1335. bool_and : bool_not
  1336. | bool_and AND bool_not
  1337. */
  1338. void bool_and(p_status *status, token_node *list){ // 因试分解
  1339. fprintf(status_log, "[info][grammar] mode status: bool_and\n");
  1340. token left, right, symbol, new_token;
  1341. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1342. if(left.type == NON_bool_and){ // 模式2/3
  1343. fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
  1344. get_pop_token(status, list, symbol);
  1345. if(symbol.type == AND_PASER){ // 模式2/3
  1346. get_right_token(status, list, bool_not, right); // 回调右边
  1347. if(right.type != NON_bool_not){
  1348. paser_error("Don't get a bool_not");
  1349. }
  1350. // 逻辑操作
  1351. new_token.type = NON_bool_and;
  1352. new_token.data_type = statement_value;
  1353. statement *code_tmp = make_statement();
  1354. code_tmp->type = operation;
  1355. code_tmp->code.operation.type = AND_func;
  1356. code_tmp->code.operation.left_exp = left.data.statement_value;
  1357. code_tmp->code.operation.right_exp = right.data.statement_value;
  1358. new_token.data.statement_value = code_tmp;
  1359. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1360. return bool_and(status, list); // 回调自己
  1361. }
  1362. else{ // 递归跳出
  1363. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1364. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1365. back_one_token(list, left);
  1366. back_again(list, symbol);
  1367. return;
  1368. }
  1369. }
  1370. else{ // 模式1
  1371. fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
  1372. back_one_token(list, left);
  1373. get_base_token(status, list, bool_not, new_token);
  1374. if(new_token.type != NON_bool_not){
  1375. back_one_token(list, new_token); // 往回[不匹配类型]
  1376. return;
  1377. }
  1378. new_token.type = NON_bool_and;
  1379. add_node(list, new_token);
  1380. return bool_and(status, list); // 回调自己
  1381. }
  1382. }
  1383. /*
  1384. bool_not : compare
  1385. | BITNOT bool_not
  1386. */
  1387. void bool_not(p_status *status, token_node *list){
  1388. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1389. token left, right, new_token;
  1390. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1391. if(left.type == NOT_PASER){ // 模式2
  1392. fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
  1393. get_right_token(status, list, bool_not, right); // 回调右边
  1394. if(right.type != NON_bool_not){
  1395. paser_error("Don't get a bool_not");
  1396. }
  1397. // 逻辑操作
  1398. new_token.type = NON_bool_not;
  1399. new_token.data_type = statement_value;
  1400. statement *code_tmp = make_statement();
  1401. code_tmp->type = operation;
  1402. code_tmp->code.operation.type = NOT_func;
  1403. code_tmp->code.operation.left_exp = NULL;
  1404. code_tmp->code.operation.right_exp = right.data.statement_value;
  1405. new_token.data.statement_value = code_tmp;
  1406. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1407. return; // 回调自己
  1408. }
  1409. else{ // 模式1
  1410. fprintf(status_log, "[info][grammar] (negative)back one token to (compare)\n");
  1411. back_one_token(list, left);
  1412. get_base_token(status, list, compare, new_token);
  1413. if(new_token.type != NON_compare){
  1414. back_one_token(list, new_token); // 往回[不匹配类型]
  1415. return;
  1416. }
  1417. new_token.type = NON_bool_not;
  1418. add_node(list, new_token);
  1419. return;
  1420. }
  1421. }
  1422. void compare(p_status *status, token_node *list){ // 多项式
  1423. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1424. token left, right, symbol, new_token;
  1425. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1426. if(left.type == NON_compare){ // 模式2/3
  1427. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1428. get_pop_token(status, list, symbol);
  1429. if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
  1430. symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){ // 模式2/3
  1431. get_right_token(status, list, bit_notor, right); // 回调右边
  1432. if(right.type != NON_bit_notor){
  1433. paser_error("Don't get a bit_notor");
  1434. }
  1435. new_token.type = NON_compare;
  1436. new_token.data_type = statement_value;
  1437. statement *code_tmp = make_statement();
  1438. code_tmp->type = operation;
  1439. if(symbol.type == EQEQ_PASER){
  1440. code_tmp->code.operation.type = EQUAL_func;
  1441. }
  1442. else if(symbol.type == MOREEQ_PASER){
  1443. code_tmp->code.operation.type = MOREEQ_func;
  1444. }
  1445. else if(symbol.type == LESSEQ_PASER){
  1446. code_tmp->code.operation.type = LESSEQ_func;
  1447. }
  1448. else if(symbol.type == MORE_PASER){
  1449. code_tmp->code.operation.type = MORE_func;
  1450. }
  1451. else if(symbol.type == LESS_PASER){
  1452. code_tmp->code.operation.type = LESS_func;
  1453. }
  1454. else{
  1455. code_tmp->code.operation.type = NOTEQ_func;
  1456. }
  1457. code_tmp->code.operation.left_exp = left.data.statement_value;
  1458. code_tmp->code.operation.right_exp = right.data.statement_value;
  1459. new_token.data.statement_value = code_tmp;
  1460. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1461. return compare(status, list); // 回调自己
  1462. }
  1463. else{ // 递归跳出
  1464. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1465. back_one_token(list, left);
  1466. back_again(list, symbol);
  1467. return;
  1468. }
  1469. }
  1470. else{ // 模式1
  1471. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1472. back_one_token(list, left);
  1473. get_base_token(status, list, bit_notor, new_token);
  1474. if(new_token.type != NON_bit_notor){
  1475. back_one_token(list, new_token); // 往回[不匹配类型]
  1476. return;
  1477. }
  1478. new_token.type = NON_compare;
  1479. add_node(list, new_token);
  1480. return compare(status, list); // 回调自己
  1481. }
  1482. }
  1483. /*
  1484. bit_notor : bit_or
  1485. | bit_notor BITOR bit_or
  1486. */
  1487. void bit_notor(p_status *status, token_node *list){ // 因试分解
  1488. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1489. token left, right, symbol, new_token;
  1490. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1491. if(left.type == NON_bit_notor){ // 模式2/3
  1492. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1493. get_pop_token(status, list, symbol);
  1494. if(symbol.type == BITNOTOR_PASER){ // 模式2/3
  1495. get_right_token(status, list, bit_or, right); // 回调右边
  1496. if(right.type != NON_bit_or){
  1497. paser_error("Don't get a bit_or");
  1498. }
  1499. // 逻辑操作
  1500. new_token.type = NON_bit_notor;
  1501. new_token.data_type = statement_value;
  1502. statement *code_tmp = make_statement();
  1503. code_tmp->type = operation;
  1504. code_tmp->code.operation.type = BITNOTOR_func;
  1505. code_tmp->code.operation.left_exp = left.data.statement_value;
  1506. code_tmp->code.operation.right_exp = right.data.statement_value;
  1507. new_token.data.statement_value = code_tmp;
  1508. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1509. return bit_notor(status, list); // 回调自己
  1510. }
  1511. else{ // 递归跳出
  1512. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1513. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1514. back_one_token(list, left);
  1515. back_again(list, symbol);
  1516. return;
  1517. }
  1518. }
  1519. else{ // 模式1
  1520. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1521. back_one_token(list, left);
  1522. get_base_token(status, list, bit_or, new_token);
  1523. if(new_token.type != NON_bit_or){
  1524. back_one_token(list, new_token); // 往回[不匹配类型]
  1525. return;
  1526. }
  1527. new_token.type = NON_bit_notor;
  1528. add_node(list, new_token);
  1529. return bit_notor(status, list); // 回调自己
  1530. }
  1531. }
  1532. /*
  1533. bit_or : bit_and
  1534. | bit_or BITOR bit_and
  1535. */
  1536. void bit_or(p_status *status, token_node *list){ // 因试分解
  1537. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1538. token left, right, symbol, new_token;
  1539. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1540. if(left.type == NON_bit_or){ // 模式2/3
  1541. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1542. get_pop_token(status, list, symbol);
  1543. if(symbol.type == BITOR_PASER){ // 模式2/3
  1544. get_right_token(status, list, bit_and, right); // 回调右边
  1545. if(right.type != NON_bit_and){
  1546. paser_error("Don't get a bit_and");
  1547. }
  1548. // 逻辑操作
  1549. new_token.type = NON_bit_or;
  1550. new_token.data_type = statement_value;
  1551. statement *code_tmp = make_statement();
  1552. code_tmp->type = operation;
  1553. code_tmp->code.operation.type = BITOR_func;
  1554. code_tmp->code.operation.left_exp = left.data.statement_value;
  1555. code_tmp->code.operation.right_exp = right.data.statement_value;
  1556. new_token.data.statement_value = code_tmp;
  1557. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1558. return bit_or(status, list); // 回调自己
  1559. }
  1560. else{ // 递归跳出
  1561. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1562. fprintf(status_log, "[info][grammar] (bit_or)out\n");
  1563. back_one_token(list, left);
  1564. back_again(list, symbol);
  1565. return;
  1566. }
  1567. }
  1568. else{ // 模式1
  1569. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1570. back_one_token(list, left);
  1571. get_base_token(status, list, bit_and, new_token);
  1572. if(new_token.type != NON_bit_and){
  1573. back_one_token(list, new_token); // 往回[不匹配类型]
  1574. return;
  1575. }
  1576. new_token.type = NON_bit_or;
  1577. add_node(list, new_token);
  1578. return bit_or(status, list); // 回调自己
  1579. }
  1580. }
  1581. /*
  1582. bit_and : bit_move
  1583. | bit_and BITAND bit_move
  1584. */
  1585. void bit_and(p_status *status, token_node *list){ // 因试分解
  1586. fprintf(status_log, "[info][grammar] mode status: bit_and\n");
  1587. token left, right, symbol, new_token;
  1588. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1589. if(left.type == NON_bit_and){ // 模式2/3
  1590. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1591. get_pop_token(status, list, symbol);
  1592. if(symbol.type == BITAND_PASER){ // 模式2/3
  1593. get_right_token(status, list, bit_move, right); // 回调右边
  1594. if(right.type != NON_bit_move){
  1595. paser_error("Don't get a bit_move");
  1596. }
  1597. // 逻辑操作
  1598. new_token.type = NON_bit_and;
  1599. new_token.data_type = statement_value;
  1600. statement *code_tmp = make_statement();
  1601. code_tmp->type = operation;
  1602. code_tmp->code.operation.type = BITAND_func;
  1603. code_tmp->code.operation.left_exp = left.data.statement_value;
  1604. code_tmp->code.operation.right_exp = right.data.statement_value;
  1605. new_token.data.statement_value = code_tmp;
  1606. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1607. return bit_and(status, list); // 回调自己
  1608. }
  1609. else{ // 递归跳出
  1610. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1611. fprintf(status_log, "[info][grammar] (bit_and)out\n");
  1612. back_one_token(list, left);
  1613. back_again(list, symbol);
  1614. return;
  1615. }
  1616. }
  1617. else{ // 模式1
  1618. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1619. back_one_token(list, left);
  1620. get_base_token(status, list, bit_move, new_token);
  1621. if(new_token.type != NON_bit_move){
  1622. back_one_token(list, new_token); // 往回[不匹配类型]
  1623. return;
  1624. }
  1625. new_token.type = NON_bit_and;
  1626. add_node(list, new_token);
  1627. return bit_and(status, list); // 回调自己
  1628. }
  1629. }
  1630. /*
  1631. bit_move : power
  1632. | bit_move BITRIGHT factor
  1633. | bit_move BITLEFT factor
  1634. */
  1635. void bit_move(p_status *status, token_node *list){ // 因试分解
  1636. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1637. token left, right, symbol, new_token;
  1638. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1639. if(left.type == NON_bit_move){ // 模式2/3
  1640. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1641. get_pop_token(status, list, symbol);
  1642. if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
  1643. get_right_token(status, list, polynomial, right); // 回调右边
  1644. if(right.type != NON_polynomial){
  1645. paser_error("Don't get a polynomial");
  1646. }
  1647. // 逻辑操作
  1648. new_token.type = NON_bit_move;
  1649. new_token.data_type = statement_value;
  1650. statement *code_tmp = make_statement();
  1651. code_tmp->type = operation;
  1652. if(symbol.type == BITRIGHT_PASER){
  1653. code_tmp->code.operation.type = BITRIGHT_func;
  1654. }
  1655. else{
  1656. code_tmp->code.operation.type = BITLEFT_func;
  1657. }
  1658. code_tmp->code.operation.left_exp = left.data.statement_value;
  1659. code_tmp->code.operation.right_exp = right.data.statement_value;
  1660. new_token.data.statement_value = code_tmp;
  1661. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1662. return bit_move(status, list); // 回调自己
  1663. }
  1664. else{ // 递归跳出
  1665. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1666. fprintf(status_log, "[info][grammar] (bit_move)out\n");
  1667. back_one_token(list, left);
  1668. back_again(list, symbol);
  1669. return;
  1670. }
  1671. }
  1672. else{ // 模式1
  1673. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1674. back_one_token(list, left);
  1675. get_base_token(status, list, polynomial, new_token);
  1676. if(new_token.type != NON_polynomial){
  1677. back_one_token(list, new_token); // 往回[不匹配类型]
  1678. return;
  1679. }
  1680. new_token.type = NON_bit_move;
  1681. add_node(list, new_token);
  1682. return bit_move(status, list); // 回调自己
  1683. }
  1684. }
  1685. /*
  1686. polynomial : factor
  1687. | polynomial ADD factor
  1688. | polynomial SUB factor
  1689. */
  1690. void polynomial(p_status *status, token_node *list){ // 多项式
  1691. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1692. token left, right, symbol, new_token;
  1693. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1694. if(left.type == NON_polynomial){ // 模式2/3
  1695. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1696. get_pop_token(status, list, symbol);
  1697. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  1698. get_right_token(status, list, factor, right); // 回调右边
  1699. if(right.type != NON_factor){
  1700. paser_error("Don't get a factor");
  1701. }
  1702. new_token.type = NON_polynomial;
  1703. new_token.data_type = statement_value;
  1704. statement *code_tmp = make_statement();
  1705. code_tmp->type = operation;
  1706. if(symbol.type == ADD_PASER){
  1707. code_tmp->code.operation.type = ADD_func;
  1708. }
  1709. else{
  1710. code_tmp->code.operation.type = SUB_func;
  1711. }
  1712. code_tmp->code.operation.left_exp = left.data.statement_value;
  1713. code_tmp->code.operation.right_exp = right.data.statement_value;
  1714. new_token.data.statement_value = code_tmp;
  1715. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1716. return polynomial(status, list); // 回调自己
  1717. }
  1718. else{ // 递归跳出
  1719. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1720. back_one_token(list, left);
  1721. back_again(list, symbol);
  1722. return;
  1723. }
  1724. }
  1725. else{ // 模式1
  1726. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1727. back_one_token(list, left);
  1728. get_base_token(status, list, factor, new_token);
  1729. if(new_token.type != NON_factor){
  1730. back_one_token(list, new_token); // 往回[不匹配类型]
  1731. return;
  1732. }
  1733. new_token.type = NON_polynomial;
  1734. add_node(list, new_token);
  1735. return polynomial(status, list); // 回调自己
  1736. }
  1737. }
  1738. /*
  1739. factor : power
  1740. | factor MUL power
  1741. | factor DIV power
  1742. */
  1743. void factor(p_status *status, token_node *list){ // 因试分解
  1744. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1745. token left, right, symbol, new_token;
  1746. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1747. if(left.type == NON_factor){ // 模式2/3
  1748. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1749. get_pop_token(status, list, symbol);
  1750. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER || symbol.type == INTDIV_PASER || symbol.type == MOD_PASER){ // 模式2/3
  1751. get_right_token(status, list, negative, right); // 回调右边
  1752. if(right.type != NON_negative){
  1753. paser_error("Don't get a value");
  1754. }
  1755. // 逻辑操作
  1756. new_token.type = NON_factor;
  1757. new_token.data_type = statement_value;
  1758. statement *code_tmp = make_statement();
  1759. code_tmp->type = operation;
  1760. if(symbol.type == MUL_PASER){
  1761. code_tmp->code.operation.type = MUL_func;
  1762. }
  1763. else if(symbol.type == DIV_PASER){
  1764. code_tmp->code.operation.type = DIV_func;
  1765. }
  1766. else if(symbol.type == INTDIV_PASER){
  1767. code_tmp->code.operation.type = INTDIV_func;
  1768. }
  1769. else{
  1770. code_tmp->code.operation.type = MOD_func;
  1771. }
  1772. code_tmp->code.operation.left_exp = left.data.statement_value;
  1773. code_tmp->code.operation.right_exp = right.data.statement_value;
  1774. new_token.data.statement_value = code_tmp;
  1775. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1776. return factor(status, list); // 回调自己
  1777. }
  1778. else{ // 递归跳出
  1779. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1780. fprintf(status_log, "[info][grammar] (factor)out\n");
  1781. back_one_token(list, left);
  1782. back_again(list, symbol);
  1783. return;
  1784. }
  1785. }
  1786. else{ // 模式1
  1787. fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
  1788. back_one_token(list, left);
  1789. get_base_token(status, list, negative, new_token);
  1790. if(new_token.type != NON_negative){
  1791. back_one_token(list, new_token); // 往回[不匹配类型]
  1792. return;
  1793. }
  1794. new_token.type = NON_factor;
  1795. add_node(list, new_token);
  1796. return factor(status, list); // 回调自己
  1797. }
  1798. }
  1799. /*
  1800. negative : bit_not
  1801. | SUB_PASER bit_not
  1802. | ADD_PASER bit_not
  1803. */
  1804. void negative(p_status *status, token_node *list){
  1805. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1806. token left, right, new_token;
  1807. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1808. if(left.type == SUB_PASER){ // 模式2
  1809. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1810. get_right_token(status, list, negative, right); // 回调右边
  1811. if(right.type != NON_negative){
  1812. paser_error("Don't get a negative");
  1813. }
  1814. // 逻辑操作
  1815. new_token.type = NON_negative;
  1816. new_token.data_type = statement_value;
  1817. statement *code_tmp = make_statement();
  1818. code_tmp->type = operation;
  1819. code_tmp->code.operation.type = NEGATIVE_func;
  1820. code_tmp->code.operation.left_exp = NULL;
  1821. code_tmp->code.operation.right_exp = right.data.statement_value;
  1822. new_token.data.statement_value = code_tmp;
  1823. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1824. return;
  1825. }
  1826. else{ // 模式1
  1827. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  1828. if(left.type != ADD_PASER){ // 取正运算符
  1829. back_one_token(list, left);
  1830. get_base_token(status, list, bit_not, new_token);
  1831. }
  1832. else{ // 需要safe_get_token
  1833. get_right_token(status, list, negative, new_token);
  1834. }
  1835. if(new_token.type != NON_bit_not && new_token.type != NON_negative){
  1836. back_one_token(list, new_token); // 往回[不匹配类型]
  1837. return;
  1838. }
  1839. new_token.type = NON_negative;
  1840. add_node(list, new_token);
  1841. return;
  1842. }
  1843. }
  1844. /*
  1845. bit_not : power
  1846. | BITNOT power
  1847. */
  1848. void bit_not(p_status *status, token_node *list){
  1849. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  1850. token left, right, new_token;
  1851. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1852. if(left.type == BITNOT_PASER){ // 模式2
  1853. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1854. get_right_token(status, list, bit_not, right); // 回调右边
  1855. if(right.type != NON_bit_not){
  1856. paser_error("Don't get a bit_not");
  1857. }
  1858. // 逻辑操作
  1859. new_token.type = NON_bit_not;
  1860. new_token.data_type = statement_value;
  1861. statement *code_tmp = make_statement();
  1862. code_tmp->type = operation;
  1863. code_tmp->code.operation.type = BITNOT_func;
  1864. code_tmp->code.operation.left_exp = NULL;
  1865. code_tmp->code.operation.right_exp = right.data.statement_value;
  1866. new_token.data.statement_value = code_tmp;
  1867. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1868. return; // 回调自己
  1869. }
  1870. else{ // 模式1
  1871. fprintf(status_log, "[info][grammar] (bit_not)back one token to (power)\n");
  1872. back_one_token(list, left);
  1873. get_base_token(status, list, power, new_token);
  1874. if(new_token.type != NON_power){
  1875. back_one_token(list, new_token); // 往回[不匹配类型]
  1876. return;
  1877. }
  1878. new_token.type = NON_bit_not;
  1879. add_node(list, new_token);
  1880. return;
  1881. }
  1882. }
  1883. /*
  1884. power : element
  1885. | power POW element
  1886. | power LOG element
  1887. | power SQRT element
  1888. */
  1889. void power(p_status *status, token_node *list){
  1890. fprintf(status_log, "[info][grammar] mode status: power\n");
  1891. token left, right, symbol, new_token;
  1892. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1893. if(left.type == NON_power){ // 模式2/3
  1894. fprintf(status_log, "[info][grammar] (power)reduce right\n");
  1895. get_pop_token(status, list, symbol);
  1896. if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
  1897. get_right_token(status, list, self_exp, right); // 回调右边
  1898. if(right.type != NON_self_exp){
  1899. paser_error("Don't get a self_exp");
  1900. }
  1901. // 逻辑操作
  1902. new_token.type = NON_power;
  1903. new_token.data_type = statement_value;
  1904. statement *code_tmp = make_statement();
  1905. code_tmp->type = operation;
  1906. if(symbol.type == POW_PASER){
  1907. code_tmp->code.operation.type = POW_func;
  1908. }
  1909. else if(symbol.type == LOG_PASER){
  1910. code_tmp->code.operation.type = LOG_func;
  1911. }
  1912. else{
  1913. code_tmp->code.operation.type = SQRT_func;
  1914. }
  1915. code_tmp->code.operation.left_exp = left.data.statement_value;
  1916. code_tmp->code.operation.right_exp = right.data.statement_value;
  1917. new_token.data.statement_value = code_tmp;
  1918. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1919. return power(status, list); // 回调自己
  1920. }
  1921. else{ // 递归跳出
  1922. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1923. fprintf(status_log, "[info][grammar] (power)out\n");
  1924. back_one_token(list, left);
  1925. back_again(list, symbol);
  1926. return;
  1927. }
  1928. }
  1929. else{ // 模式1
  1930. fprintf(status_log, "[info][grammar] (power)back one token to (element)\n");
  1931. back_one_token(list, left);
  1932. get_base_token(status, list, self_exp, new_token);
  1933. if(new_token.type != NON_self_exp){
  1934. back_one_token(list, new_token); // 往回[不匹配类型]
  1935. return;
  1936. }
  1937. new_token.type = NON_power;
  1938. add_node(list, new_token);
  1939. return power(status, list); // 回调自己
  1940. }
  1941. }
  1942. /*
  1943. self_exp : hide_list
  1944. | FADD self_exp
  1945. | FSUB self_exp
  1946. | self_exp FADD
  1947. | self_exp FSUB
  1948. */
  1949. void self_exp(p_status *status, token_node *list){ // 因试分解
  1950. fprintf(status_log, "[info][grammar] mode status: self_exp\n");
  1951. token left, right, new_token;
  1952. parameter *p_list;
  1953. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1954. if(left.type == FADD_PASER || left.type == FSUB_PASER){ // ++a和--a
  1955. get_right_token(status, list, self_exp, right); // 递归自己,比如----a匹配为--new_token,new_token匹配为--a
  1956. if(right.type != NON_self_exp){
  1957. paser_error("Don't get self_exp");
  1958. }
  1959. statement *code_tmp = make_statement();
  1960. code_tmp->type = operation;
  1961. code_tmp->code.operation.type = (left.type == FADD_PASER) ? FADD_func : FSUB_func;
  1962. code_tmp->code.operation.left_exp = NULL;
  1963. code_tmp->code.operation.right_exp = right.data.statement_value;
  1964. new_token.data.statement_value = code_tmp;
  1965. new_token.data_type = statement_value;
  1966. new_token.type = NON_self_exp;
  1967. add_node(list, new_token); // 压入节点
  1968. return;
  1969. }
  1970. else if(left.type == NON_self_exp){
  1971. get_pop_token(status, list, right);
  1972. if(right.type == FADD_PASER || right.type == FSUB_PASER){ // a++和a--
  1973. statement *code_tmp = make_statement();
  1974. code_tmp->type = operation;
  1975. code_tmp->code.operation.type = (right.type == FADD_PASER) ? LADD_func : LSUB_func;
  1976. code_tmp->code.operation.left_exp = left.data.statement_value;
  1977. code_tmp->code.operation.right_exp = NULL;
  1978. new_token.data.statement_value = code_tmp;
  1979. new_token.data_type = statement_value;
  1980. new_token.type = NON_self_exp;
  1981. add_node(list, new_token); // 压入节点
  1982. return self_exp(status, list); // 回调自己,比如a----匹配为self_exp--,再匹配为self_exp
  1983. }
  1984. else{ // 跳出递归
  1985. back_one_token(list, left);
  1986. back_again(list, right);
  1987. return;
  1988. }
  1989. }
  1990. else{ // a--和a++
  1991. back_one_token(list,left);
  1992. get_base_token(status, list, call_down, new_token);
  1993. if(new_token.type != NON_call_down){
  1994. back_one_token(list, new_token); // 往回[不匹配类型]
  1995. return;
  1996. }
  1997. new_token.type = NON_self_exp;
  1998. add_node(list, new_token);
  1999. return self_exp(status, list); // 回调自己
  2000. }
  2001. }
  2002. /*
  2003. call_down : element
  2004. | call_down LI top_exp RI
  2005. */
  2006. void call_down(p_status *status, token_node *list){ // 因试分解
  2007. fprintf(status_log, "[info][grammar] mode status: call_down\n");
  2008. token left, lb_t, rb_t, new_token, parameter_t;
  2009. parameter *p_list = NULL;
  2010. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  2011. if(left.type == NON_call_down){
  2012. fprintf(status_log, "[info][grammar] (call_down)reduce right\n");
  2013. get_pop_token(status, list, lb_t);
  2014. if(lb_t.type == LI_PASER){
  2015. p_status new_status;
  2016. new_status = *status;
  2017. new_status.is_list = true;
  2018. get_right_token(&new_status, list, top_exp, parameter_t); // 回调右边
  2019. if(parameter_t.type != NON_top_exp){
  2020. paser_error("Don't get a top_exp");
  2021. }
  2022. statement *code_tmp = make_statement();
  2023. get_pop_token(status, list, rb_t);
  2024. if(rb_t.type == RI_PASER || rb_t.type == COMMA_PASER){ // a[1,2,3,4]模式
  2025. back_one_token(list, parameter_t);
  2026. back_again(list, rb_t);
  2027. get_base_token(status,list,formal_parameter,parameter_t);
  2028. if(parameter_t.type != NON_parameter){
  2029. paser_error("Don't get formal_parameter");
  2030. }
  2031. get_pop_token(status, list, rb_t); // 把rb重新弹出来
  2032. p_list = parameter_t.data.parameter_list;
  2033. code_tmp->type = down;
  2034. code_tmp->code.down.base_var = left.data.statement_value;
  2035. code_tmp->code.down.child_var = p_list;
  2036. }
  2037. else if(rb_t.type == COLON_PASER){ // a[1,2,3,4]模式
  2038. back_one_token(list, parameter_t);
  2039. back_again(list, rb_t);
  2040. p_status new_status;
  2041. new_status = *status;
  2042. new_status.is_slice = true;
  2043. get_base_token(&new_status,list,formal_parameter,parameter_t);
  2044. if(parameter_t.type != NON_parameter){
  2045. paser_error("Don't get formal_parameter");
  2046. }
  2047. get_pop_token(status, list, rb_t); // 把rb重新弹出来
  2048. if(rb_t.type != RI_PASER){
  2049. printf("rb_t.type = %d\n", rb_t.type);
  2050. goto error;
  2051. }
  2052. p_list = parameter_t.data.parameter_list;
  2053. code_tmp->type = slice;
  2054. code_tmp->code.slice.base_var = left.data.statement_value;
  2055. code_tmp->code.slice.value = p_list;
  2056. }
  2057. else{
  2058. free(code_tmp);
  2059. error: paser_error("Don't get ']', ':' or ','");
  2060. }
  2061. // 逻辑操作
  2062. new_token.type = NON_call_down;
  2063. new_token.data_type = statement_value;
  2064. new_token.data.statement_value = code_tmp;
  2065. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  2066. return call_down(status, list); // 回调自己
  2067. }
  2068. else{ // 递归跳出
  2069. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  2070. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  2071. back_one_token(list, left);
  2072. back_again(list, lb_t);
  2073. return;
  2074. }
  2075. }
  2076. else{ // 模式1
  2077. fprintf(status_log, "[info][grammar] (call_down)back one token to (element)\n");
  2078. back_one_token(list, left);
  2079. get_base_token(status, list, element, new_token);
  2080. if(new_token.type != NON_element){
  2081. back_one_token(list, new_token); // 往回[不匹配类型]
  2082. return;
  2083. }
  2084. new_token.type = NON_call_down;
  2085. add_node(list, new_token);
  2086. return call_down(status, list); // 回调自己
  2087. }
  2088. }
  2089. /*
  2090. element : number
  2091. | LB top_exp RB
  2092. */
  2093. void element(p_status *status, token_node *list){ // 数字归约
  2094. fprintf(status_log, "[info][grammar] mode status: element\n");
  2095. token gett, new_token;
  2096. gett = pop_node(list); // 取得一个token
  2097. if(gett.type == LB_PASER){ // 模式3
  2098. fprintf(status_log, "[info][grammar] (element)get LB\n");
  2099. p_status new_status = *status; // 继承file_p等值
  2100. reset_status(new_status); // 不会影响 *staus
  2101. new_status.ignore_enter = true; // 括号内忽略回车
  2102. get_right_token(&new_status, list, top_exp, new_token);
  2103. if(new_token.type == RB_PASER){
  2104. statement *code_tmp = make_statement(); // 默认
  2105. new_token.data_type = statement_value;
  2106. new_token.data.statement_value = code_tmp;
  2107. goto return_back;
  2108. }
  2109. else if(new_token.type != NON_top_exp){
  2110. paser_error("Don't get 'top_exp'");
  2111. }
  2112. token rb;
  2113. get_pop_token(status, list ,rb);
  2114. if(rb.type != RB_PASER){ // 匹配失败
  2115. paser_error("Don't get ')'");
  2116. }
  2117. return_back:
  2118. new_token.type = NON_element;
  2119. add_node(list, new_token); // 压入节点
  2120. return;
  2121. }
  2122. else if(gett.type == VAR_PASER){ // a
  2123. back_one_token(list, gett);
  2124. get_base_token(status, list, var_token, new_token);
  2125. if(new_token.type != NON_base_var){
  2126. back_one_token(list, new_token); // 往回[不匹配类型]
  2127. return;
  2128. }
  2129. new_token.type = NON_element;
  2130. add_node(list, new_token);
  2131. return;
  2132. }
  2133. else if(gett.type == LP_PASER){ // dict
  2134. back_one_token(list, gett);
  2135. p_status new_status = *status; // 继承file_p等值
  2136. reset_status(new_status); // 不会影响 *staus
  2137. new_status.ignore_enter = true; // 括号内忽略回车
  2138. get_base_token(&new_status,list,dict_,new_token); // 不需要safe_get_token
  2139. if(new_token.type != NON_dict){
  2140. paser_error("Don't get a dict_");
  2141. }
  2142. new_token.type = NON_element;
  2143. add_node(list, new_token);
  2144. return;
  2145. }
  2146. else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
  2147. token exp_token, rb, tmp_var;
  2148. p_status new_status;
  2149. new_status = *status;
  2150. new_status.is_list = true; // 防止top_exp收走逗号
  2151. new_status.ignore_enter = true; // 括号内忽略回车
  2152. get_right_token(&new_status, list, top_exp, exp_token);
  2153. if(exp_token.type == RI_PASER){ //可以认定为空list
  2154. back_one_token(list, gett);
  2155. back_again(list, exp_token);
  2156. get_base_token(status,list,list_,new_token); // 返回空列表
  2157. if(new_token.type != NON_list){
  2158. paser_error("Don't get a list_");
  2159. }
  2160. goto back;
  2161. }
  2162. else if(exp_token.type != NON_top_exp){
  2163. paser_error("Don't get 'top_exp'");
  2164. }
  2165. get_pop_token(status, list ,rb);
  2166. if(rb.type == RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
  2167. get_pop_token(status, list ,tmp_var);
  2168. if(tmp_var.type == VAR_PASER){ // a
  2169. back_one_token(list, tmp_var);
  2170. get_base_token(status, list, var_token, new_token);
  2171. if(new_token.type != NON_base_var){
  2172. paser_error("Don't get var");
  2173. }
  2174. new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
  2175. goto back;
  2176. }
  2177. else{
  2178. // back again连用的时候要倒过来使用
  2179. /* 原因:
  2180. 先back_one_token的时候,token流变成
  2181. <之前的> - <A> - <NULL[seek/index]>
  2182. 先back_again的时候,token流变成
  2183. <之前的> - <A> - <B[seek]> - <NULL[index]>
  2184. 再back_again的时候,token流变成
  2185. <之前的> - <A> - <C[seek]> - <B> - <NULL[index]>
  2186. 其实还有一种解决方法:
  2187. 连用三次back_one_token
  2188. 然后再用两次back_token手动回退
  2189. */
  2190. back_one_token(list, gett);
  2191. back_again(list, tmp_var);
  2192. back_again(list, rb);
  2193. back_again(list, exp_token);
  2194. get_base_token(&new_status,list,list_,new_token); // 不需要safe_get_token
  2195. if(new_token.type != NON_list){
  2196. paser_error("Don't get a list_");
  2197. }
  2198. goto back;
  2199. }
  2200. }
  2201. else{
  2202. back_one_token(list, gett);
  2203. back_again(list, rb);
  2204. back_again(list, exp_token);
  2205. get_base_token(&new_status,list,list_,new_token); // 不需要safe_get_token
  2206. if(new_token.type != NON_list){
  2207. paser_error("Don't get a list_");
  2208. }
  2209. goto back;
  2210. }
  2211. back:
  2212. new_token.type = NON_element;
  2213. add_node(list, new_token);
  2214. return;
  2215. }
  2216. else{
  2217. fprintf(status_log, "[info][grammar] (element)back one token to (paser_value)\n");
  2218. back_one_token(list, gett);
  2219. get_base_token(status, list, paser_value, new_token);
  2220. if(new_token.type != NON_base_value){
  2221. back_one_token(list, new_token); // 往回[不匹配类型]
  2222. return;
  2223. }
  2224. new_token.type = NON_element;
  2225. add_node(list, new_token);
  2226. return;
  2227. }
  2228. }
  2229. /*
  2230. list_ : LI formal_parameter RI
  2231. */
  2232. void list_(p_status *status, token_node *list){ // 数字归约
  2233. fprintf(status_log, "[info][grammar] mode status: list_\n");
  2234. token gett, list_core, ri_t, new_token;
  2235. parameter *base = NULL;
  2236. gett = pop_node(list); // 取得一个token
  2237. if(gett.type == LI_PASER){ // var类型
  2238. p_status new_status;
  2239. new_status = *status;
  2240. new_status.is_list = true;
  2241. get_right_token(&new_status,list,formal_parameter,list_core);
  2242. new_status.is_list = false;
  2243. if(list_core.type == RI_PASER){ // 空列表
  2244. base = NULL;
  2245. goto make_list;
  2246. }
  2247. else if(list_core.type == NON_parameter){
  2248. base = list_core.data.parameter_list;
  2249. }
  2250. else{
  2251. paser_error("Don't get formal_parameter");
  2252. }
  2253. get_pop_token(status,list,ri_t);
  2254. if(ri_t.type != RI_PASER){
  2255. paser_error("Don't get ']'");
  2256. }
  2257. make_list:
  2258. new_token.type = NON_list;
  2259. statement *code_tmp = make_statement();
  2260. code_tmp->type = base_list;
  2261. code_tmp->code.base_list.value = base;
  2262. new_token.data.statement_value = code_tmp;
  2263. new_token.data_type = statement_value;
  2264. fprintf(status_log, "[info][grammar] (list_)out\n");
  2265. add_node(list, new_token); // 压入节点
  2266. return;
  2267. }
  2268. else{ // 不是期望值
  2269. fprintf(status_log, "[info][grammar] (list_)back one token\n");
  2270. back_one_token(list, gett);
  2271. return;
  2272. }
  2273. }
  2274. /*
  2275. dict_ : LP formal_parameter RP
  2276. */
  2277. void dict_(p_status *status, token_node *list){ // 数字归约
  2278. fprintf(status_log, "[info][grammar] mode status: dict_\n");
  2279. token gett, dict_core, rp_t, new_token;
  2280. parameter *base = NULL;
  2281. gett = pop_node(list); // 取得一个token
  2282. if(gett.type == LP_PASER){ // var类型
  2283. p_status new_status;
  2284. new_status = *status;
  2285. new_status.is_dict = true;
  2286. get_right_token(&new_status,list,formal_parameter,dict_core);
  2287. new_status.is_dict = false;
  2288. if(dict_core.type == RP_PASER){ // 空列表
  2289. base = NULL;
  2290. goto make_dict;
  2291. }
  2292. else if(dict_core.type == NON_parameter){
  2293. base = dict_core.data.parameter_list;
  2294. }
  2295. else{
  2296. paser_error("Don't get formal_parameter");
  2297. }
  2298. get_pop_token(status,list,rp_t);
  2299. if(rp_t.type != RP_PASER){
  2300. paser_error("Don't get '}'");
  2301. }
  2302. make_dict:
  2303. new_token.type = NON_dict;
  2304. statement *code_tmp = make_statement();
  2305. code_tmp->type = base_dict;
  2306. code_tmp->code.base_dict.value = base;
  2307. new_token.data.statement_value = code_tmp;
  2308. new_token.data_type = statement_value;
  2309. fprintf(status_log, "[info][grammar] (dict_)out\n");
  2310. add_node(list, new_token); // 压入节点
  2311. return;
  2312. }
  2313. else{ // 不是期望值
  2314. fprintf(status_log, "[info][grammar] (dict_)back one token\n");
  2315. back_one_token(list, gett);
  2316. return;
  2317. }
  2318. }
  2319. /*
  2320. var_token : VAR
  2321. */
  2322. void var_token(p_status *status, token_node *list){ // 数字归约
  2323. fprintf(status_log, "[info][grammar] mode status: var_token\n");
  2324. token gett, new_token;
  2325. gett = pop_node(list); // 取得一个token
  2326. if(gett.type == VAR_PASER){ // var类型
  2327. new_token.type = NON_base_var;
  2328. GWARF_value tmp_value;
  2329. tmp_value.type = INT_value;
  2330. tmp_value.value.int_value = atoi(gett.data.text);
  2331. statement *code_tmp = make_statement();
  2332. code_tmp->type = base_var;
  2333. code_tmp->code.base_var.var_name = gett.data.text;
  2334. code_tmp->code.base_var.from = NULL;
  2335. new_token.data.statement_value = code_tmp;
  2336. new_token.data_type = statement_value;
  2337. fprintf(status_log, "[info][grammar] (var_token)out\n");
  2338. add_node(list, new_token); // 压入节点
  2339. return;
  2340. }
  2341. else{ // 不是期望值
  2342. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  2343. back_one_token(list, gett);
  2344. return;
  2345. }
  2346. }
  2347. /*
  2348. number : INT_PASER
  2349. | DOUBLE_PASER
  2350. */
  2351. void paser_value(p_status *status, token_node *list){ // 数字归约
  2352. fprintf(status_log, "[info][grammar] mode status: paser_value\n");
  2353. token gett, new_token;
  2354. gett = pop_node(list); // 取得一个token
  2355. if(gett.type == INT_PASER){ // int类型
  2356. new_token.type = NON_base_value;
  2357. GWARF_value tmp_value;
  2358. tmp_value.type = INT_value;
  2359. tmp_value.value.int_value = atoi(gett.data.text);
  2360. statement *code_tmp = make_statement();
  2361. code_tmp->type = call;
  2362. code_tmp->code.call.func = pack_call_name("int", NULL);
  2363. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2364. new_token.data.statement_value = code_tmp;
  2365. new_token.data_type = statement_value;
  2366. }
  2367. else if(gett.type == DOUBLE_PASER){
  2368. new_token.type = NON_base_value;
  2369. GWARF_value tmp_value;
  2370. tmp_value.type = NUMBER_value;
  2371. tmp_value.value.double_value = atof(gett.data.text);
  2372. statement *code_tmp = make_statement();
  2373. code_tmp->type = call;
  2374. code_tmp->code.call.func = pack_call_name("double", NULL);
  2375. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2376. new_token.data.statement_value = code_tmp;
  2377. new_token.data_type = statement_value;
  2378. }
  2379. else if(gett.type == STR_PASER){
  2380. new_token.type = NON_base_value;
  2381. GWARF_value tmp_value;
  2382. tmp_value.type = STRING_value;
  2383. if(gett.data.text == NULL){
  2384. tmp_value.value.string = "";
  2385. }
  2386. else{
  2387. tmp_value.value.string = gett.data.text;
  2388. }
  2389. printf("tmp_value.value.string = %s, %s\n", tmp_value.value.string, gett.data.text);
  2390. statement *code_tmp = make_statement();
  2391. code_tmp->type = call;
  2392. code_tmp->code.call.func = pack_call_name("str", NULL);
  2393. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2394. new_token.data.statement_value = code_tmp;
  2395. new_token.data_type = statement_value;
  2396. goto not_free;
  2397. // printf("[info][grammar] (paser_value)get str: %s\n", tmp_value.value.string);
  2398. }
  2399. else if(gett.type == TRUE_PASER || gett.type == FALSE_PASER){
  2400. new_token.type = NON_base_value;
  2401. GWARF_value tmp_value;
  2402. tmp_value.type = BOOL_value;
  2403. if(gett.type == TRUE_PASER){
  2404. tmp_value.value.bool_value = true;
  2405. }
  2406. else{
  2407. tmp_value.value.bool_value = false;
  2408. }
  2409. statement *code_tmp = make_statement();
  2410. code_tmp->type = call;
  2411. code_tmp->code.call.func = pack_call_name("bool", NULL);
  2412. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  2413. new_token.data.statement_value = code_tmp;
  2414. new_token.data_type = statement_value;
  2415. }
  2416. else if(gett.type == NONE_PASER){
  2417. new_token.type = NON_base_value;
  2418. statement *code_tmp = make_statement();
  2419. code_tmp->type = base_value;
  2420. code_tmp->code.base_value.value.type = NULL_value;
  2421. code_tmp->code.base_value.value.value.int_value = 0;
  2422. new_token.data.statement_value = code_tmp;
  2423. new_token.data_type = statement_value;
  2424. }
  2425. else{ // 不是期望值
  2426. fprintf(status_log, "[info][grammar] (paser_value)back one token\n");
  2427. back_one_token(list, gett);
  2428. return;
  2429. }
  2430. free(gett.data.text); // 释放字符串
  2431. not_free:
  2432. fprintf(status_log, "[info][grammar] (paser_value)add one token\n");
  2433. add_node(list, new_token); // 压入节点
  2434. }
  2435. void paser_error(char *text){
  2436. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  2437. printf("[error][grammar] paser error : %s\n\n", text);
  2438. exit(1);
  2439. }