syntax.c 78 KB

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