syntax.c 106 KB

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