syntax.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024
  1. #include"token.h"
  2. #include"lex.h"
  3. #include"../inter/interpreter.h"
  4. void factor(p_status *status, token_node *list);
  5. void power(p_status *status, token_node *list);
  6. void call_down(p_status *status, token_node *list);
  7. void number(p_status *status, token_node *list);
  8. void element(p_status *status, token_node *list);
  9. void var_token(p_status *status, token_node *list);
  10. void polynomial(p_status *status, token_node *list);
  11. void bit_move(p_status *status, token_node *list);
  12. void command(p_status *status, token_node *list);
  13. void while_(p_status *status, token_node *list);
  14. void if_(p_status *status, token_node *list);
  15. void for_(p_status *status, token_node *list);
  16. void elif_(p_status *status, token_node *list);
  17. void block_(p_status *status, token_node *list);
  18. void top_exp(p_status *status, token_node *list);
  19. void negative(p_status *status, token_node *list);
  20. void bit_not(p_status *status, token_node *list);
  21. void bit_notor(p_status *status, token_node *list);
  22. void bit_or(p_status *status, token_node *list);
  23. void bit_and(p_status *status, token_node *list);
  24. void compare(p_status *status, token_node *list);
  25. void bool_and(p_status *status, token_node *list);
  26. void bool_or(p_status *status, token_node *list);
  27. void bool_not(p_status *status, token_node *list);
  28. void eq_number(p_status *status, token_node *list);
  29. void call_back_(p_status *status, token_node *list);
  30. void def_(p_status *status, token_node *list);
  31. void ctrl_(p_status *status, token_node *list);
  32. void var_ctrl_(p_status *status, token_node *list);
  33. void return_(p_status *status, token_node *list);
  34. void formal_parameter(p_status *status, token_node *list);
  35. void list_(p_status *status, token_node *list);
  36. void paser_error(char *text);
  37. /*
  38. command_list : command
  39. | command_list command
  40. */
  41. void command_list(p_status *status, token_node *list){ // 多项式
  42. fprintf(status_log, "[info][grammar] mode status: command_list\n", text);
  43. token left, right, new_token;
  44. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  45. if(left.type == NON_command_list){ // 模式2
  46. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  47. get_right_token(status, list, command, right); // 回调右边
  48. if(right.type == NON_command){
  49. new_token.type = NON_command_list;
  50. new_token.data_type = empty;
  51. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  52. return command_list(status, list); // 回调自己
  53. }
  54. else{ // 递归跳出[EOF_token]
  55. printf("right.type = %d\n", right.type);
  56. fprintf(status_log, "[info][grammar] (command_list)out\n");
  57. back_one_token(list, left);
  58. back_again(list, right);
  59. return;
  60. }
  61. }
  62. else if(left.type == EOF_token){ // 递归跳出的条件
  63. fprintf(status_log, "[info][grammar] (command_list)out again\n");
  64. back_one_token(list, left);
  65. return;
  66. }
  67. else{ // 模式1
  68. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  69. back_one_token(list, left);
  70. get_base_token(status, list, command, new_token);
  71. if(new_token.type != NON_command){
  72. back_one_token(list, new_token); // 往回[不匹配类型]
  73. return;
  74. }
  75. new_token.type = NON_command_list;
  76. add_node(list, new_token);
  77. return command_list(status, list); // 回调自己
  78. }
  79. }
  80. /*
  81. command : top_exp <ENTER>
  82. */
  83. void command(p_status *status, token_node *list){ // 多项式
  84. fprintf(status_log, "[info][grammar] mode status: command\n", text);
  85. token left, new_token;
  86. left = pop_node(list); // 先弹出一个token 检查token
  87. if(left.type == WHILE_PASER){ // 是while类型的数据
  88. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  89. back_one_token(list, left);
  90. get_base_token(status, list, while_, new_token);
  91. get_stop_token();
  92. push_statement(statement_base, new_token);
  93. }
  94. else if(left.type == IF_PASER){
  95. fprintf(status_log, "[info][grammar] (command)back one token to (if)\n");
  96. back_one_token(list, left);
  97. get_base_token(status, list, if_, new_token);
  98. get_stop_token();
  99. push_statement(statement_base, new_token);
  100. }
  101. else if(left.type == FOR_PASER){
  102. fprintf(status_log, "[info][grammar] (command)back one token to (for)\n");
  103. back_one_token(list, left);
  104. get_base_token(status, list, for_, new_token);
  105. get_stop_token();
  106. push_statement(statement_base, new_token);
  107. }
  108. else if(left.type == DEF_PASER){
  109. fprintf(status_log, "[info][grammar] (command)back one token to (def_)\n");
  110. back_one_token(list, left);
  111. get_base_token(status, list, def_, new_token);
  112. get_stop_token();
  113. push_statement(statement_base, new_token);
  114. }
  115. else if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  116. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  117. fprintf(status_log, "[info][grammar] (command)back one token to (ctrl_)\n");
  118. back_one_token(list, left);
  119. get_base_token(status, list, ctrl_, new_token);
  120. get_stop_token();
  121. push_statement(statement_base, new_token);
  122. }
  123. else if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  124. fprintf(status_log, "[info][grammar] (command)back one token to (var_ctrl_)\n");
  125. back_one_token(list, left);
  126. get_base_token(status, list, var_ctrl_, new_token);
  127. get_stop_token();
  128. push_statement(statement_base, new_token);
  129. }
  130. else if(left.type == RETURN_PASER){
  131. fprintf(status_log, "[info][grammar] (command)back one token to (return_)\n");
  132. back_one_token(list, left);
  133. get_base_token(status, list, return_, new_token);
  134. get_stop_token();
  135. push_statement(statement_base, new_token);
  136. }
  137. else if(left.type == ENTER_PASER){
  138. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  139. }
  140. else if(left.type == EOF_token){
  141. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  142. back_one_token(list, left);
  143. goto return_back;
  144. }
  145. else{ // 表达式
  146. fprintf(status_log, "[info][grammar] (command)back one token to (top_exp)\n");
  147. back_one_token(list, left);
  148. get_base_token(status, list, top_exp, new_token);
  149. if(new_token.type != NON_top_exp){
  150. back_one_token(list, new_token); // 往回[不匹配类型]
  151. return;
  152. }
  153. fprintf(token_info, "[tag 7]\n");
  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 ':'");
  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. status->is_parameter = true;
  511. get_base_token(status, list, top_exp, next);
  512. status->is_parameter = false;
  513. if(next.type != NON_top_exp){
  514. back_one_token(list, next); // 往回[不匹配类型]
  515. return;
  516. }
  517. new_token.type = NON_parameter;
  518. new_token.data_type = parameter_list;
  519. parameter *tmp = NULL;
  520. get_pop_token(status, list, eq);
  521. if(eq.type == (status->is_dict ? COLON_PASER : EQ_PASER)){ // name_value模式
  522. if(status->is_list){
  523. paser_error("list shouldn't get '='");
  524. }
  525. get_right_token(status, list, top_exp, value_token);
  526. if(value_token.type != NON_top_exp){
  527. paser_error("Don't get a top_exp");
  528. return;
  529. }
  530. new_token.data.parameter_list = make_parameter_name(next.data.statement_value);
  531. new_token.data.parameter_list->u.value = value_token.data.statement_value;
  532. new_token.data.parameter_list->type = name_value;
  533. }
  534. else{
  535. if(status->is_dict){
  536. paser_error("dict should get ':'");
  537. }
  538. back_again(list,eq); // 回退[如果使用back_one_token则会导致add_node在EQ的后面]
  539. new_token.data.parameter_list = make_parameter_value(next.data.statement_value);
  540. new_token.data.parameter_list->u.var = new_token.data.parameter_list->u.value;
  541. new_token.data.parameter_list->type = only_value;
  542. }
  543. add_node(list, new_token);
  544. return formal_parameter(status, list); // 回调自己
  545. }
  546. }
  547. /*
  548. while_ : WHILE LB top_exp RB block
  549. */
  550. void while_(p_status *status, token_node *list){
  551. fprintf(status_log, "[info][grammar] mode status: while_\n");
  552. token while_t, lb_t, exp_t, rb_t, block_t, new_token;
  553. while_t = pop_node(list);
  554. if(while_t.type == WHILE_PASER){
  555. get_pop_token(status, list, lb_t);
  556. if(lb_t.type != LB_PASER){
  557. paser_error("Don't get '('");
  558. }
  559. get_right_token(status,list,top_exp,exp_t);
  560. if(exp_t.type != NON_top_exp){ // 不是表达式
  561. paser_error("Don't get 'top_exp'");
  562. }
  563. get_pop_token(status, list, rb_t);
  564. if(rb_t.type != RB_PASER){
  565. paser_error("Don't get ')'");
  566. }
  567. get_right_token(status,list,block_,block_t);
  568. if(block_t.type != NON_block){ // 不是表达式
  569. paser_error("Don't get '{'");
  570. }
  571. statement *while_tmp = make_statement();
  572. while_tmp->type = while_cycle;
  573. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  574. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  575. new_token.type = NON_while;
  576. new_token.data_type = statement_value;
  577. new_token.data.statement_value = while_tmp;
  578. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  579. return;
  580. }
  581. else{
  582. back_one_token(list, while_t);
  583. return;
  584. }
  585. }
  586. /*
  587. block_ : LP command_list RB
  588. */
  589. void block_(p_status *status, token_node *list){
  590. fprintf(status_log, "[info][grammar] mode status: block_\n");
  591. token lp_t, rp_t, new_token, command_list_t;
  592. lp_t = pop_node(list);
  593. again:
  594. if(lp_t.type == LP_PASER){
  595. statement *block_tmp = make_statement();
  596. statement_base = append_statement_list(block_tmp, statement_base);
  597. get_right_token(status,list,command_list,command_list_t); // 要把command_list也弹出来
  598. statement_base = free_statement_list(statement_base); // 重新释放
  599. get_pop_token(status, list, rp_t);
  600. if(rp_t.type != RP_PASER){
  601. paser_error("Don't get '}'");
  602. }
  603. new_token.type = NON_block;
  604. new_token.data_type = statement_value;
  605. new_token.data.statement_value = block_tmp;
  606. add_node(list, new_token); // 压入节点
  607. fprintf(status_log, "[tag 1]\n");
  608. return;
  609. }
  610. if(lp_t.type == ENTER_PASER){
  611. get_pop_token(status,list,lp_t);
  612. goto again;
  613. }
  614. else{
  615. printf("lp_t.type = %d != %d\n", lp_t.type, LP_PASER);
  616. back_one_token(list, lp_t);
  617. return;
  618. }
  619. }
  620. // var_ctrl_ 包含诸如:global, nolocal,之类的
  621. void var_ctrl_(p_status *status, token_node *list){
  622. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  623. token left, var, right, new_token;
  624. statement *times = NULL;
  625. char *var_name = NULL;
  626. left = pop_node(list);
  627. if(left.type == GLOBAL_PASER || left.type == DEFAULT_PASER || left.type == NOLOCAL_PASER){
  628. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  629. get_right_token(status, list, top_exp, var); // 取得base_var
  630. if(var.type != NON_top_exp && var.data.statement_value->type != base_var){
  631. paser_error("Don't get var");
  632. }
  633. else{
  634. var_name = malloc(sizeof(var.data.statement_value->code.base_var.var_name));
  635. strcpy(var_name, var.data.statement_value->code.base_var.var_name);
  636. times = var.data.statement_value->code.base_var.from;
  637. // TODO:: 本质上没有完全释放
  638. free(var.data.statement_value->code.base_var.var_name);
  639. free(var.data.statement_value);
  640. }
  641. if(left.type == DEFAULT_PASER){ // 设置times
  642. get_right_token(status, list, top_exp, right); // 回调右边
  643. if(right.type != NON_top_exp){
  644. back_again(list, right); // 不是期望的数字
  645. }
  646. else{
  647. times = right.data.statement_value;
  648. }
  649. }
  650. // 逻辑操作
  651. new_token.type = NON_ctrl;
  652. new_token.data_type = statement_value;
  653. statement *code_tmp = make_statement();
  654. switch (left.type)
  655. {
  656. case GLOBAL_PASER:
  657. code_tmp->type = set_global;
  658. code_tmp->code.set_global.name = var_name;
  659. break;
  660. case DEFAULT_PASER:
  661. code_tmp->type = set_default;
  662. code_tmp->code.set_default.name = var_name;
  663. code_tmp->code.set_default.times = times;
  664. break;
  665. case NOLOCAL_PASER:
  666. code_tmp->type = set_nonlocal;
  667. code_tmp->code.set_nonlocal.name = var_name;
  668. break;
  669. default:
  670. break;
  671. }
  672. new_token.data.statement_value = code_tmp;
  673. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  674. return; // 回调自己
  675. }
  676. else{ // 模式1
  677. back_one_token(list, left);
  678. return;
  679. }
  680. }
  681. void return_(p_status *status, token_node *list){
  682. fprintf(status_log, "[info][grammar] mode status: return_\n");
  683. token left, right, value, new_token;
  684. statement *times = NULL, *back_value = NULL;
  685. left = pop_node(list);
  686. if(left.type == RETURN_PASER){
  687. fprintf(status_log, "[info][grammar] (return_)reduce right\n");
  688. get_right_token(status, list, top_exp, value); // 回调右边
  689. if(value.type != NON_top_exp){
  690. back_again(list, value); // 不是期望的数字,就默认使用NULL,并且回退
  691. times = NULL;
  692. back_value = NULL;
  693. goto not_times;
  694. }
  695. else{
  696. back_value = value.data.statement_value;
  697. }
  698. get_right_token(status, list, top_exp, right); // 回调右边
  699. if(right.type != NON_top_exp){
  700. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  701. times = NULL;
  702. }
  703. else{
  704. times = right.data.statement_value;
  705. }
  706. not_times:
  707. new_token.type = NON_return;
  708. new_token.data_type = statement_value;
  709. statement *code_tmp = make_statement();
  710. code_tmp->type = return_code;
  711. code_tmp->code.return_code.times = times;
  712. code_tmp->code.return_code.value = back_value;
  713. new_token.data.statement_value = code_tmp;
  714. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  715. return; // 回调自己
  716. }
  717. else{ // 模式1
  718. back_one_token(list, left);
  719. return;
  720. }
  721. }
  722. // ctrl_包含诸如:break,broken,之类的
  723. void ctrl_(p_status *status, token_node *list){
  724. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  725. token left, right, new_token;
  726. statement *times = NULL;
  727. left = pop_node(list);
  728. if(left.type == BREAK_PASER || left.type == BROKEN_PASER || left.type == CONTINUE_PASER || left.type == CONTINUED_PASER ||
  729. left.type == RESTART_PASER || left.type == RESTARTED_PASER || left.type == REGO_PASER || left.type == REWENT_PASER){
  730. fprintf(status_log, "[info][grammar] (ctrl_)reduce right\n");
  731. if(left.type != REGO_PASER && left.type != REWENT_PASER){
  732. get_right_token(status, list, top_exp, right); // 回调右边
  733. if(right.type != NON_top_exp){
  734. back_again(list, right); // 不是期望的数字,就默认使用NULL,并且回退
  735. times = NULL;
  736. }
  737. else{
  738. times = right.data.statement_value;
  739. }
  740. }
  741. // 逻辑操作
  742. new_token.type = NON_ctrl;
  743. new_token.data_type = statement_value;
  744. statement *code_tmp = make_statement();
  745. switch (left.type)
  746. {
  747. case BREAK_PASER:
  748. code_tmp->type = break_cycle;
  749. code_tmp->code.break_cycle.times = times;
  750. break;
  751. case BROKEN_PASER:
  752. code_tmp->type = broken;
  753. code_tmp->code.broken.times = times;
  754. break;
  755. case CONTINUE_PASER:
  756. code_tmp->type = continue_cycle;
  757. code_tmp->code.continue_cycle.times = times;
  758. break;
  759. case CONTINUED_PASER:
  760. code_tmp->type = continued;
  761. code_tmp->code.continued.times = times;
  762. break;
  763. case RESTART_PASER:
  764. code_tmp->type = restart;
  765. code_tmp->code.restart.times = times;
  766. break;
  767. case RESTARTED_PASER:
  768. code_tmp->type = restarted;
  769. code_tmp->code.restarted.times = times;
  770. break;
  771. case REGO_PASER:
  772. code_tmp->type = rego;
  773. break;
  774. case REWENT_PASER:
  775. code_tmp->type = rewent;
  776. break;
  777. default:
  778. break;
  779. }
  780. new_token.data.statement_value = code_tmp;
  781. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  782. return; // 回调自己
  783. }
  784. else{ // 模式1
  785. back_one_token(list, left);
  786. return;
  787. }
  788. }
  789. /*
  790. top_exp : polynomial
  791. */
  792. void top_exp(p_status *status, token_node *list){
  793. fprintf(status_log, "[info][grammar] mode status: top_exp\n");
  794. token exp;
  795. get_base_token(status,list,eq_number,exp);
  796. if(exp.type != NON_eq){
  797. back_one_token(list, exp);
  798. return;
  799. }
  800. exp.type = NON_top_exp;
  801. add_node(list, exp); // 压入节点
  802. return;
  803. }
  804. /*
  805. eq_number : bool_or
  806. | eq_number AND bool_or
  807. */
  808. void eq_number(p_status *status, token_node *list){ // 因试分解
  809. fprintf(status_log, "[info][grammar] mode status: eq_number\n");
  810. token left, right, symbol, new_token;
  811. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  812. if(left.type == NON_eq){ // 模式2/3
  813. fprintf(status_log, "[info][grammar] (eq_number)reduce right\n");
  814. get_pop_token(status, list, symbol);
  815. if(symbol.type == EQ_PASER && !status->is_parameter){ // 模式2/3
  816. get_right_token(status, list, call_back_, right); // 回调右边
  817. if(right.type != NON_call){
  818. paser_error("Don't get a call_back_");
  819. }
  820. // 逻辑操作
  821. new_token.type = NON_eq;
  822. new_token.data_type = statement_value;
  823. statement *code_tmp = make_statement();
  824. code_tmp->type = operation;
  825. code_tmp->code.operation.type = ASSIGnMENT_func;
  826. code_tmp->code.operation.left_exp = left.data.statement_value;
  827. code_tmp->code.operation.right_exp = right.data.statement_value;
  828. new_token.data.statement_value = code_tmp;
  829. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  830. return eq_number(status, list); // 回调自己
  831. }
  832. else{ // 递归跳出
  833. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  834. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  835. back_one_token(list, left);
  836. back_again(list, symbol);
  837. return;
  838. }
  839. }
  840. else{ // 模式1
  841. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  842. back_one_token(list, left);
  843. get_base_token(status, list, call_back_, new_token);
  844. if(new_token.type != NON_call){
  845. back_one_token(list, new_token); // 往回[不匹配类型]
  846. return;
  847. }
  848. new_token.type = NON_eq;
  849. add_node(list, new_token);
  850. return eq_number(status, list); // 回调自己
  851. }
  852. }
  853. /*
  854. call_back_ : bool_or
  855. | call_back_ LB RB
  856. */
  857. void call_back_(p_status *status, token_node *list){ // 因试分解
  858. fprintf(status_log, "[info][grammar] mode status: call_back_\n");
  859. token left, symbol, rb_t, new_token, parameter_t;
  860. parameter *p_list;
  861. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  862. if(left.type == NON_call){
  863. fprintf(status_log, "[info][grammar] (call_back_)reduce right\n");
  864. get_pop_token(status, list, symbol);
  865. if(symbol.type == LB_PASER && !status->is_func){
  866. get_pop_token(status, list, rb_t);
  867. if(rb_t.type != RB_PASER){ // 带参数
  868. back_again(list, rb_t);
  869. get_right_token(status,list,formal_parameter,parameter_t);
  870. if(parameter_t.type != NON_parameter){
  871. paser_error("Don't get formal_parameter");
  872. }
  873. printf("rb_t.type = %d\n", rb_t.type);
  874. get_pop_token(status, list, rb_t);
  875. if(rb_t.type != RB_PASER){
  876. paser_error("Don't get ')'[2]");
  877. }
  878. p_list = parameter_t.data.parameter_list;
  879. }
  880. else{
  881. p_list = NULL; // 没参数
  882. }
  883. // 逻辑操作
  884. new_token.type = NON_call;
  885. new_token.data_type = statement_value;
  886. statement *code_tmp = make_statement();
  887. code_tmp->type = call;
  888. code_tmp->code.call.func = left.data.statement_value;
  889. code_tmp->code.call.parameter_list = p_list;
  890. new_token.data.statement_value = code_tmp;
  891. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  892. return call_back_(status, list); // 回调自己
  893. }
  894. else{ // 递归跳出
  895. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  896. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  897. back_one_token(list, left);
  898. back_again(list, symbol);
  899. return;
  900. }
  901. }
  902. else{ // 模式1
  903. fprintf(status_log, "[info][grammar] (call_back_)back one token to (bool_or)\n");
  904. back_one_token(list, left);
  905. get_base_token(status, list, bool_or, new_token);
  906. if(new_token.type != NON_bool_or){
  907. back_one_token(list, new_token); // 往回[不匹配类型]
  908. return;
  909. }
  910. new_token.type = NON_call;
  911. add_node(list, new_token);
  912. return call_back_(status, list); // 回调自己
  913. }
  914. }
  915. /*
  916. bool_or : bool_and
  917. | bool_or AND bool_and
  918. */
  919. void bool_or(p_status *status, token_node *list){ // 因试分解
  920. fprintf(status_log, "[info][grammar] mode status: bool_or\n");
  921. token left, right, symbol, new_token;
  922. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  923. if(left.type == NON_bool_or){ // 模式2/3
  924. fprintf(status_log, "[info][grammar] (bool_or)reduce right\n");
  925. get_pop_token(status, list, symbol);
  926. if(symbol.type == OR_PASER){ // 模式2/3
  927. get_right_token(status, list, bool_and, right); // 回调右边
  928. if(right.type != NON_bool_and){
  929. paser_error("Don't get a compare");
  930. }
  931. // 逻辑操作
  932. new_token.type = NON_bool_or;
  933. new_token.data_type = statement_value;
  934. statement *code_tmp = make_statement();
  935. code_tmp->type = operation;
  936. code_tmp->code.operation.type = OR_func;
  937. code_tmp->code.operation.left_exp = left.data.statement_value;
  938. code_tmp->code.operation.right_exp = right.data.statement_value;
  939. new_token.data.statement_value = code_tmp;
  940. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  941. return bool_or(status, list); // 回调自己
  942. }
  943. else{ // 递归跳出
  944. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  945. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  946. back_one_token(list, left);
  947. back_again(list, symbol);
  948. return;
  949. }
  950. }
  951. else{ // 模式1
  952. fprintf(status_log, "[info][grammar] (bool_or)back one token to (bool_and)\n");
  953. back_one_token(list, left);
  954. get_base_token(status, list, bool_and, new_token);
  955. if(new_token.type != NON_bool_and){
  956. back_one_token(list, new_token); // 往回[不匹配类型]
  957. return;
  958. }
  959. new_token.type = NON_bool_or;
  960. add_node(list, new_token);
  961. return bool_or(status, list); // 回调自己
  962. }
  963. }
  964. /*
  965. bool_and : bool_not
  966. | bool_and AND bool_not
  967. */
  968. void bool_and(p_status *status, token_node *list){ // 因试分解
  969. fprintf(status_log, "[info][grammar] mode status: bool_and\n");
  970. token left, right, symbol, new_token;
  971. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  972. if(left.type == NON_bool_and){ // 模式2/3
  973. fprintf(status_log, "[info][grammar] (bool_and)reduce right\n");
  974. get_pop_token(status, list, symbol);
  975. if(symbol.type == AND_PASER){ // 模式2/3
  976. get_right_token(status, list, bool_not, right); // 回调右边
  977. if(right.type != NON_bool_not){
  978. paser_error("Don't get a bool_not");
  979. }
  980. // 逻辑操作
  981. new_token.type = NON_bool_and;
  982. new_token.data_type = statement_value;
  983. statement *code_tmp = make_statement();
  984. code_tmp->type = operation;
  985. code_tmp->code.operation.type = AND_func;
  986. code_tmp->code.operation.left_exp = left.data.statement_value;
  987. code_tmp->code.operation.right_exp = right.data.statement_value;
  988. new_token.data.statement_value = code_tmp;
  989. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  990. return bool_and(status, list); // 回调自己
  991. }
  992. else{ // 递归跳出
  993. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  994. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  995. back_one_token(list, left);
  996. back_again(list, symbol);
  997. return;
  998. }
  999. }
  1000. else{ // 模式1
  1001. fprintf(status_log, "[info][grammar] (bool_and)back one token to (compare)\n");
  1002. back_one_token(list, left);
  1003. get_base_token(status, list, bool_not, new_token);
  1004. if(new_token.type != NON_bool_not){
  1005. back_one_token(list, new_token); // 往回[不匹配类型]
  1006. return;
  1007. }
  1008. new_token.type = NON_bool_and;
  1009. add_node(list, new_token);
  1010. return bool_and(status, list); // 回调自己
  1011. }
  1012. }
  1013. /*
  1014. bool_not : compare
  1015. | BITNOT bool_not
  1016. */
  1017. void bool_not(p_status *status, token_node *list){
  1018. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1019. token left, right, new_token;
  1020. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1021. if(left.type == NOT_PASER){ // 模式2
  1022. fprintf(status_log, "[info][grammar] (bool_not)reduce right\n");
  1023. get_right_token(status, list, bool_not, right); // 回调右边
  1024. if(right.type != NON_bool_not){
  1025. paser_error("Don't get a bool_not");
  1026. }
  1027. // 逻辑操作
  1028. new_token.type = NON_bool_not;
  1029. new_token.data_type = statement_value;
  1030. statement *code_tmp = make_statement();
  1031. code_tmp->type = operation;
  1032. code_tmp->code.operation.type = NOT_func;
  1033. code_tmp->code.operation.left_exp = NULL;
  1034. code_tmp->code.operation.right_exp = right.data.statement_value;
  1035. new_token.data.statement_value = code_tmp;
  1036. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1037. return; // 回调自己
  1038. }
  1039. else{ // 模式1
  1040. fprintf(status_log, "[info][grammar] (negative)back one token to (compare)\n");
  1041. back_one_token(list, left);
  1042. get_base_token(status, list, compare, new_token);
  1043. if(new_token.type != NON_compare){
  1044. back_one_token(list, new_token); // 往回[不匹配类型]
  1045. return;
  1046. }
  1047. new_token.type = NON_bool_not;
  1048. add_node(list, new_token);
  1049. return;
  1050. }
  1051. }
  1052. void compare(p_status *status, token_node *list){ // 多项式
  1053. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1054. token left, right, symbol, new_token;
  1055. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1056. if(left.type == NON_compare){ // 模式2/3
  1057. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1058. get_pop_token(status, list, symbol);
  1059. if(symbol.type == EQEQ_PASER || symbol.type == MOREEQ_PASER || symbol.type == LESSEQ_PASER ||
  1060. symbol.type == MORE_PASER || symbol.type == LESS_PASER || symbol.type == NOTEQ_PASER){ // 模式2/3
  1061. get_right_token(status, list, bit_notor, right); // 回调右边
  1062. if(right.type != NON_bit_notor){
  1063. paser_error("Don't get a bit_notor");
  1064. }
  1065. new_token.type = NON_compare;
  1066. new_token.data_type = statement_value;
  1067. statement *code_tmp = make_statement();
  1068. code_tmp->type = operation;
  1069. if(symbol.type == EQEQ_PASER){
  1070. code_tmp->code.operation.type = EQUAL_func;
  1071. }
  1072. else if(symbol.type == MOREEQ_PASER){
  1073. code_tmp->code.operation.type = MOREEQ_func;
  1074. }
  1075. else if(symbol.type == LESSEQ_PASER){
  1076. code_tmp->code.operation.type = LESSEQ_func;
  1077. }
  1078. else if(symbol.type == MORE_PASER){
  1079. code_tmp->code.operation.type = MORE_func;
  1080. }
  1081. else if(symbol.type == LESS_PASER){
  1082. code_tmp->code.operation.type = LESS_func;
  1083. }
  1084. else{
  1085. code_tmp->code.operation.type = NOTEQ_func;
  1086. }
  1087. code_tmp->code.operation.left_exp = left.data.statement_value;
  1088. code_tmp->code.operation.right_exp = right.data.statement_value;
  1089. new_token.data.statement_value = code_tmp;
  1090. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1091. return compare(status, list); // 回调自己
  1092. }
  1093. else{ // 递归跳出
  1094. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1095. back_one_token(list, left);
  1096. back_again(list, symbol);
  1097. return;
  1098. }
  1099. }
  1100. else{ // 模式1
  1101. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1102. back_one_token(list, left);
  1103. get_base_token(status, list, bit_notor, new_token);
  1104. if(new_token.type != NON_bit_notor){
  1105. back_one_token(list, new_token); // 往回[不匹配类型]
  1106. return;
  1107. }
  1108. new_token.type = NON_compare;
  1109. add_node(list, new_token);
  1110. return compare(status, list); // 回调自己
  1111. }
  1112. }
  1113. /*
  1114. bit_notor : bit_or
  1115. | bit_notor BITOR bit_or
  1116. */
  1117. void bit_notor(p_status *status, token_node *list){ // 因试分解
  1118. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1119. token left, right, symbol, new_token;
  1120. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1121. if(left.type == NON_bit_notor){ // 模式2/3
  1122. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1123. get_pop_token(status, list, symbol);
  1124. if(symbol.type == BITNOTOR_PASER){ // 模式2/3
  1125. get_right_token(status, list, bit_or, right); // 回调右边
  1126. if(right.type != NON_bit_or){
  1127. paser_error("Don't get a bit_or");
  1128. }
  1129. // 逻辑操作
  1130. new_token.type = NON_bit_notor;
  1131. new_token.data_type = statement_value;
  1132. statement *code_tmp = make_statement();
  1133. code_tmp->type = operation;
  1134. code_tmp->code.operation.type = BITNOTOR_func;
  1135. code_tmp->code.operation.left_exp = left.data.statement_value;
  1136. code_tmp->code.operation.right_exp = right.data.statement_value;
  1137. new_token.data.statement_value = code_tmp;
  1138. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1139. return bit_notor(status, list); // 回调自己
  1140. }
  1141. else{ // 递归跳出
  1142. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1143. fprintf(status_log, "[info][grammar] (bit_notor)out\n");
  1144. back_one_token(list, left);
  1145. back_again(list, symbol);
  1146. return;
  1147. }
  1148. }
  1149. else{ // 模式1
  1150. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1151. back_one_token(list, left);
  1152. get_base_token(status, list, bit_or, new_token);
  1153. if(new_token.type != NON_bit_or){
  1154. back_one_token(list, new_token); // 往回[不匹配类型]
  1155. return;
  1156. }
  1157. new_token.type = NON_bit_notor;
  1158. add_node(list, new_token);
  1159. return bit_notor(status, list); // 回调自己
  1160. }
  1161. }
  1162. /*
  1163. bit_or : bit_and
  1164. | bit_or BITOR bit_and
  1165. */
  1166. void bit_or(p_status *status, token_node *list){ // 因试分解
  1167. fprintf(status_log, "[info][grammar] mode status: bit_or\n");
  1168. token left, right, symbol, new_token;
  1169. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1170. if(left.type == NON_bit_or){ // 模式2/3
  1171. fprintf(status_log, "[info][grammar] (bit_or)reduce right\n");
  1172. get_pop_token(status, list, symbol);
  1173. if(symbol.type == BITOR_PASER){ // 模式2/3
  1174. get_right_token(status, list, bit_and, right); // 回调右边
  1175. if(right.type != NON_bit_and){
  1176. paser_error("Don't get a bit_and");
  1177. }
  1178. // 逻辑操作
  1179. new_token.type = NON_bit_or;
  1180. new_token.data_type = statement_value;
  1181. statement *code_tmp = make_statement();
  1182. code_tmp->type = operation;
  1183. code_tmp->code.operation.type = BITOR_func;
  1184. code_tmp->code.operation.left_exp = left.data.statement_value;
  1185. code_tmp->code.operation.right_exp = right.data.statement_value;
  1186. new_token.data.statement_value = code_tmp;
  1187. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1188. return bit_or(status, list); // 回调自己
  1189. }
  1190. else{ // 递归跳出
  1191. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1192. fprintf(status_log, "[info][grammar] (bit_or)out\n");
  1193. back_one_token(list, left);
  1194. back_again(list, symbol);
  1195. return;
  1196. }
  1197. }
  1198. else{ // 模式1
  1199. fprintf(status_log, "[info][grammar] (bit_or)back one token to (bit_and)\n");
  1200. back_one_token(list, left);
  1201. get_base_token(status, list, bit_and, new_token);
  1202. if(new_token.type != NON_bit_and){
  1203. back_one_token(list, new_token); // 往回[不匹配类型]
  1204. return;
  1205. }
  1206. new_token.type = NON_bit_or;
  1207. add_node(list, new_token);
  1208. return bit_or(status, list); // 回调自己
  1209. }
  1210. }
  1211. /*
  1212. bit_and : bit_move
  1213. | bit_and BITAND bit_move
  1214. */
  1215. void bit_and(p_status *status, token_node *list){ // 因试分解
  1216. fprintf(status_log, "[info][grammar] mode status: bit_and\n");
  1217. token left, right, symbol, new_token;
  1218. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1219. if(left.type == NON_bit_and){ // 模式2/3
  1220. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1221. get_pop_token(status, list, symbol);
  1222. if(symbol.type == BITAND_PASER){ // 模式2/3
  1223. get_right_token(status, list, bit_move, right); // 回调右边
  1224. if(right.type != NON_bit_move){
  1225. paser_error("Don't get a bit_move");
  1226. }
  1227. // 逻辑操作
  1228. new_token.type = NON_bit_and;
  1229. new_token.data_type = statement_value;
  1230. statement *code_tmp = make_statement();
  1231. code_tmp->type = operation;
  1232. code_tmp->code.operation.type = BITAND_func;
  1233. code_tmp->code.operation.left_exp = left.data.statement_value;
  1234. code_tmp->code.operation.right_exp = right.data.statement_value;
  1235. new_token.data.statement_value = code_tmp;
  1236. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1237. return bit_and(status, list); // 回调自己
  1238. }
  1239. else{ // 递归跳出
  1240. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1241. fprintf(status_log, "[info][grammar] (bit_and)out\n");
  1242. back_one_token(list, left);
  1243. back_again(list, symbol);
  1244. return;
  1245. }
  1246. }
  1247. else{ // 模式1
  1248. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1249. back_one_token(list, left);
  1250. get_base_token(status, list, bit_move, new_token);
  1251. if(new_token.type != NON_bit_move){
  1252. back_one_token(list, new_token); // 往回[不匹配类型]
  1253. return;
  1254. }
  1255. new_token.type = NON_bit_and;
  1256. add_node(list, new_token);
  1257. return bit_and(status, list); // 回调自己
  1258. }
  1259. }
  1260. /*
  1261. bit_move : power
  1262. | bit_move BITRIGHT factor
  1263. | bit_move BITLEFT factor
  1264. */
  1265. void bit_move(p_status *status, token_node *list){ // 因试分解
  1266. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1267. token left, right, symbol, new_token;
  1268. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1269. if(left.type == NON_bit_move){ // 模式2/3
  1270. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1271. get_pop_token(status, list, symbol);
  1272. if(symbol.type == BITRIGHT_PASER || symbol.type == BITLEFT_PASER){ // 模式2/3
  1273. get_right_token(status, list, polynomial, right); // 回调右边
  1274. if(right.type != NON_polynomial){
  1275. paser_error("Don't get a polynomial");
  1276. }
  1277. // 逻辑操作
  1278. new_token.type = NON_bit_move;
  1279. new_token.data_type = statement_value;
  1280. statement *code_tmp = make_statement();
  1281. code_tmp->type = operation;
  1282. if(symbol.type == BITRIGHT_PASER){
  1283. code_tmp->code.operation.type = BITRIGHT_func;
  1284. }
  1285. else{
  1286. code_tmp->code.operation.type = BITLEFT_func;
  1287. }
  1288. code_tmp->code.operation.left_exp = left.data.statement_value;
  1289. code_tmp->code.operation.right_exp = right.data.statement_value;
  1290. new_token.data.statement_value = code_tmp;
  1291. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1292. return bit_move(status, list); // 回调自己
  1293. }
  1294. else{ // 递归跳出
  1295. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1296. fprintf(status_log, "[info][grammar] (bit_move)out\n");
  1297. back_one_token(list, left);
  1298. back_again(list, symbol);
  1299. return;
  1300. }
  1301. }
  1302. else{ // 模式1
  1303. fprintf(status_log, "[info][grammar] (bit_move)back one token to (factor)\n");
  1304. back_one_token(list, left);
  1305. get_base_token(status, list, polynomial, new_token);
  1306. if(new_token.type != NON_polynomial){
  1307. back_one_token(list, new_token); // 往回[不匹配类型]
  1308. return;
  1309. }
  1310. new_token.type = NON_bit_move;
  1311. add_node(list, new_token);
  1312. return bit_move(status, list); // 回调自己
  1313. }
  1314. }
  1315. /*
  1316. polynomial : factor
  1317. | polynomial ADD factor
  1318. | polynomial SUB factor
  1319. */
  1320. void polynomial(p_status *status, token_node *list){ // 多项式
  1321. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  1322. token left, right, symbol, new_token;
  1323. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1324. if(left.type == NON_polynomial){ // 模式2/3
  1325. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  1326. get_pop_token(status, list, symbol);
  1327. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  1328. get_right_token(status, list, factor, right); // 回调右边
  1329. if(right.type != NON_factor){
  1330. paser_error("Don't get a factor");
  1331. }
  1332. new_token.type = NON_polynomial;
  1333. new_token.data_type = statement_value;
  1334. statement *code_tmp = make_statement();
  1335. code_tmp->type = operation;
  1336. if(symbol.type == ADD_PASER){
  1337. code_tmp->code.operation.type = ADD_func;
  1338. }
  1339. else{
  1340. code_tmp->code.operation.type = SUB_func;
  1341. }
  1342. code_tmp->code.operation.left_exp = left.data.statement_value;
  1343. code_tmp->code.operation.right_exp = right.data.statement_value;
  1344. new_token.data.statement_value = code_tmp;
  1345. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1346. return polynomial(status, list); // 回调自己
  1347. }
  1348. else{ // 递归跳出
  1349. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  1350. back_one_token(list, left);
  1351. back_again(list, symbol);
  1352. return;
  1353. }
  1354. }
  1355. else{ // 模式1
  1356. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  1357. back_one_token(list, left);
  1358. get_base_token(status, list, factor, new_token);
  1359. if(new_token.type != NON_factor){
  1360. back_one_token(list, new_token); // 往回[不匹配类型]
  1361. return;
  1362. }
  1363. new_token.type = NON_polynomial;
  1364. add_node(list, new_token);
  1365. return polynomial(status, list); // 回调自己
  1366. }
  1367. }
  1368. /*
  1369. factor : power
  1370. | factor MUL power
  1371. | factor DIV power
  1372. */
  1373. void factor(p_status *status, token_node *list){ // 因试分解
  1374. fprintf(status_log, "[info][grammar] mode status: factor\n");
  1375. token left, right, symbol, new_token;
  1376. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1377. if(left.type == NON_factor){ // 模式2/3
  1378. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  1379. get_pop_token(status, list, symbol);
  1380. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
  1381. get_right_token(status, list, negative, right); // 回调右边
  1382. if(right.type != NON_negative){
  1383. paser_error("Don't get a value");
  1384. }
  1385. // 逻辑操作
  1386. new_token.type = NON_factor;
  1387. new_token.data_type = statement_value;
  1388. statement *code_tmp = make_statement();
  1389. code_tmp->type = operation;
  1390. if(symbol.type == MUL_PASER){
  1391. code_tmp->code.operation.type = MUL_func;
  1392. }
  1393. else{
  1394. code_tmp->code.operation.type = DIV_func;
  1395. }
  1396. code_tmp->code.operation.left_exp = left.data.statement_value;
  1397. code_tmp->code.operation.right_exp = right.data.statement_value;
  1398. new_token.data.statement_value = code_tmp;
  1399. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1400. return factor(status, list); // 回调自己
  1401. }
  1402. else{ // 递归跳出
  1403. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1404. fprintf(status_log, "[info][grammar] (factor)out\n");
  1405. back_one_token(list, left);
  1406. back_again(list, symbol);
  1407. return;
  1408. }
  1409. }
  1410. else{ // 模式1
  1411. fprintf(status_log, "[info][grammar] (factor)back one token to (element)\n");
  1412. back_one_token(list, left);
  1413. get_base_token(status, list, negative, new_token);
  1414. if(new_token.type != NON_negative){
  1415. back_one_token(list, new_token); // 往回[不匹配类型]
  1416. return;
  1417. }
  1418. new_token.type = NON_factor;
  1419. add_node(list, new_token);
  1420. return factor(status, list); // 回调自己
  1421. }
  1422. }
  1423. /*
  1424. negative : bit_not
  1425. | BITNOT bit_not
  1426. */
  1427. void negative(p_status *status, token_node *list){
  1428. fprintf(status_log, "[info][grammar] mode status: negative\n");
  1429. token left, right, new_token;
  1430. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1431. if(left.type == SUB_PASER){ // 模式2
  1432. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1433. get_right_token(status, list, negative, right); // 回调右边
  1434. if(right.type != NON_negative){
  1435. paser_error("Don't get a negative");
  1436. }
  1437. // 逻辑操作
  1438. new_token.type = NON_negative;
  1439. new_token.data_type = statement_value;
  1440. statement *code_tmp = make_statement();
  1441. code_tmp->type = operation;
  1442. code_tmp->code.operation.type = NEGATIVE_func;
  1443. code_tmp->code.operation.left_exp = NULL;
  1444. code_tmp->code.operation.right_exp = right.data.statement_value;
  1445. new_token.data.statement_value = code_tmp;
  1446. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1447. return; // 回调自己
  1448. }
  1449. else{ // 模式1
  1450. fprintf(status_log, "[info][grammar] (negative)back one token to (bit_not)\n");
  1451. back_one_token(list, left);
  1452. get_base_token(status, list, bit_not, new_token);
  1453. if(new_token.type != NON_bit_not){
  1454. back_one_token(list, new_token); // 往回[不匹配类型]
  1455. return;
  1456. }
  1457. new_token.type = NON_negative;
  1458. add_node(list, new_token);
  1459. return;
  1460. }
  1461. }
  1462. /*
  1463. bit_not : power
  1464. | BITNOT power
  1465. */
  1466. void bit_not(p_status *status, token_node *list){
  1467. fprintf(status_log, "[info][grammar] mode status: bit_not\n");
  1468. token left, right, new_token;
  1469. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1470. if(left.type == BITNOT_PASER){ // 模式2
  1471. fprintf(status_log, "[info][grammar] (bit_not)reduce right\n");
  1472. get_right_token(status, list, bit_not, right); // 回调右边
  1473. if(right.type != NON_bit_not){
  1474. paser_error("Don't get a bit_not");
  1475. }
  1476. // 逻辑操作
  1477. new_token.type = NON_bit_not;
  1478. new_token.data_type = statement_value;
  1479. statement *code_tmp = make_statement();
  1480. code_tmp->type = operation;
  1481. code_tmp->code.operation.type = BITNOT_func;
  1482. code_tmp->code.operation.left_exp = NULL;
  1483. code_tmp->code.operation.right_exp = right.data.statement_value;
  1484. new_token.data.statement_value = code_tmp;
  1485. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1486. return; // 回调自己
  1487. }
  1488. else{ // 模式1
  1489. fprintf(status_log, "[info][grammar] (bit_not)back one token to (power)\n");
  1490. back_one_token(list, left);
  1491. get_base_token(status, list, power, new_token);
  1492. if(new_token.type != NON_power){
  1493. back_one_token(list, new_token); // 往回[不匹配类型]
  1494. return;
  1495. }
  1496. new_token.type = NON_bit_not;
  1497. add_node(list, new_token);
  1498. return;
  1499. }
  1500. }
  1501. /*
  1502. power : element
  1503. | power POW element
  1504. | power LOG element
  1505. | power SQRT element
  1506. */
  1507. void power(p_status *status, token_node *list){
  1508. fprintf(status_log, "[info][grammar] mode status: power\n");
  1509. token left, right, symbol, new_token;
  1510. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1511. if(left.type == NON_power){ // 模式2/3
  1512. fprintf(status_log, "[info][grammar] (power)reduce right\n");
  1513. get_pop_token(status, list, symbol);
  1514. if(symbol.type == POW_PASER || symbol.type == LOG_PASER || symbol.type == SQRT_PASER){ // 模式2/3/4
  1515. get_right_token(status, list, call_down, right); // 回调右边
  1516. if(right.type != NON_call_down){
  1517. paser_error("Don't get a call_down");
  1518. }
  1519. // 逻辑操作
  1520. new_token.type = NON_power;
  1521. new_token.data_type = statement_value;
  1522. statement *code_tmp = make_statement();
  1523. code_tmp->type = operation;
  1524. if(symbol.type == POW_PASER){
  1525. code_tmp->code.operation.type = POW_func;
  1526. }
  1527. else if(symbol.type == LOG_PASER){
  1528. code_tmp->code.operation.type = LOG_func;
  1529. }
  1530. else{
  1531. code_tmp->code.operation.type = SQRT_func;
  1532. }
  1533. code_tmp->code.operation.left_exp = left.data.statement_value;
  1534. code_tmp->code.operation.right_exp = right.data.statement_value;
  1535. new_token.data.statement_value = code_tmp;
  1536. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  1537. return power(status, list); // 回调自己
  1538. }
  1539. else{ // 递归跳出
  1540. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  1541. fprintf(status_log, "[info][grammar] (power)out\n");
  1542. back_one_token(list, left);
  1543. back_again(list, symbol);
  1544. return;
  1545. }
  1546. }
  1547. else{ // 模式1
  1548. fprintf(status_log, "[info][grammar] (power)back one token to (element)\n");
  1549. back_one_token(list, left);
  1550. get_base_token(status, list, call_down, new_token);
  1551. if(new_token.type != NON_call_down){
  1552. back_one_token(list, new_token); // 往回[不匹配类型]
  1553. return;
  1554. }
  1555. new_token.type = NON_power;
  1556. add_node(list, new_token);
  1557. return power(status, list); // 回调自己
  1558. }
  1559. }
  1560. /*
  1561. call_down : element
  1562. | call_down LI top_exp RI
  1563. */
  1564. void call_down(p_status *status, token_node *list){ // 因试分解
  1565. fprintf(status_log, "[info][grammar] mode status: call_down\n");
  1566. token left, lb_t, rb_t, new_token, parameter_t;
  1567. parameter *p_list;
  1568. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  1569. fprintf(token_info, "[debug][tag 4]\n");
  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(token_info, "[debug][tag 5]\n");
  1596. fprintf(status_log, "[info][grammar] (call_back_)out\n");
  1597. back_one_token(list, left);
  1598. back_again(list, lb_t);
  1599. fprintf(token_info, "[debug][tag 6]\n");
  1600. return;
  1601. }
  1602. }
  1603. else{ // 模式1
  1604. fprintf(status_log, "[info][grammar] (call_down)back one token to (element)\n");
  1605. back_one_token(list, left);
  1606. get_base_token(status, list, element, new_token);
  1607. fprintf(token_info, "[debug][tag 2]\n");
  1608. if(new_token.type != NON_element){
  1609. back_one_token(list, new_token); // 往回[不匹配类型]
  1610. return;
  1611. }
  1612. new_token.type = NON_call_down;
  1613. add_node(list, new_token);
  1614. fprintf(token_info, "[debug][tag 3]\n");
  1615. return call_down(status, list); // 回调自己
  1616. }
  1617. }
  1618. /*
  1619. element : number
  1620. | LB top_exp RB
  1621. */
  1622. void element(p_status *status, token_node *list){ // 数字归约
  1623. fprintf(status_log, "[info][grammar] mode status: element\n");
  1624. token gett, new_token;
  1625. gett = pop_node(list); // 取得一个token
  1626. if(gett.type == LB_PASER){ // 模式3
  1627. fprintf(status_log, "[info][grammar] (element)get LB\n");
  1628. get_right_token(status, list, top_exp, new_token);
  1629. if(new_token.type != NON_top_exp){
  1630. paser_error("Don't get 'top_exp'");
  1631. }
  1632. token rb;
  1633. get_pop_token(status, list ,rb);
  1634. if(rb.type != RB_PASER){ // 匹配失败
  1635. paser_error("Don't get ')'");
  1636. }
  1637. new_token.type = NON_element;
  1638. add_node(list, new_token); // 压入节点
  1639. return;
  1640. }
  1641. else if(gett.type == VAR_PASER){ // a
  1642. back_one_token(list, gett);
  1643. get_base_token(status, list, var_token, new_token);
  1644. if(new_token.type != NON_base_var){
  1645. back_one_token(list, new_token); // 往回[不匹配类型]
  1646. return;
  1647. }
  1648. new_token.type = NON_element;
  1649. add_node(list, new_token);
  1650. return;
  1651. }
  1652. else if(gett.type == LI_PASER){ // [1]a或[1]列表或[1,2,3,4]列表
  1653. token exp_token, rb, tmp_var;
  1654. get_right_token(status, list, top_exp, exp_token);
  1655. if(exp_token.type == RI_PASER){ //可以认定为空list
  1656. back_one_token(list, gett);
  1657. back_again(list, exp_token);
  1658. get_base_token(status,list,list_,new_token); // 不需要safe_get_token
  1659. if(new_token.type != NON_list){
  1660. paser_error("Don't get a list_");
  1661. }
  1662. goto back;
  1663. }
  1664. else if(exp_token.type != NON_top_exp){
  1665. paser_error("Don't get 'top_exp'");
  1666. }
  1667. fprintf(token_info, "[tag 1.2]\n");
  1668. get_pop_token(status, list ,rb);
  1669. if(rb.type == RI_PASER){ // 匹配失败 TODO:: 检查是不是[1,2,3,4]的列表类型
  1670. get_pop_token(status, list ,tmp_var);
  1671. if(tmp_var.type == VAR_PASER){ // a
  1672. back_one_token(list, tmp_var);
  1673. get_base_token(status, list, var_token, new_token);
  1674. if(new_token.type != NON_base_var){
  1675. paser_error("Don't get var");
  1676. }
  1677. new_token.data.statement_value->code.base_var.from = exp_token.data.statement_value;
  1678. goto back;
  1679. }
  1680. else{
  1681. // back again连用的时候要倒过来使用
  1682. /* 原因:
  1683. 先back_one_token的时候,token流变成
  1684. <之前的> - <A> - <NULL[seek/index]>
  1685. 先back_again的时候,token流变成
  1686. <之前的> - <A> - <B[seek]> - <NULL[index]>
  1687. 再back_again的时候,token流变成
  1688. <之前的> - <A> - <C[seek]> - <B> - <NULL[index]>
  1689. 其实还有一种解决方法:
  1690. 连用三次back_one_token
  1691. 然后再用两次back_token手动回退
  1692. */
  1693. back_one_token(list, gett);
  1694. back_again(list, tmp_var);
  1695. back_again(list, rb);
  1696. back_again(list, exp_token);
  1697. get_base_token(status,list,list_,new_token); // 不需要safe_get_token
  1698. if(new_token.type != NON_list){
  1699. paser_error("Don't get a list_");
  1700. }
  1701. goto back;
  1702. }
  1703. }
  1704. else{
  1705. back_one_token(list, gett);
  1706. back_again(list, rb);
  1707. back_again(list, exp_token);
  1708. get_base_token(status,list,list_,new_token); // 不需要safe_get_token
  1709. if(new_token.type != NON_list){
  1710. paser_error("Don't get a list_");
  1711. }
  1712. goto back;
  1713. }
  1714. back:
  1715. new_token.type = NON_element;
  1716. add_node(list, new_token);
  1717. fprintf(token_info, "[debug][tag 1]\n");
  1718. return;
  1719. }
  1720. else{
  1721. fprintf(status_log, "[info][grammar] (element)back one token to (number)\n");
  1722. back_one_token(list, gett);
  1723. get_base_token(status, list, number, new_token);
  1724. if(new_token.type != NON_base_value){
  1725. back_one_token(list, new_token); // 往回[不匹配类型]
  1726. return;
  1727. }
  1728. new_token.type = NON_element;
  1729. add_node(list, new_token);
  1730. return;
  1731. }
  1732. }
  1733. /*
  1734. list_ : LI formal_parameter RI
  1735. */
  1736. void list_(p_status *status, token_node *list){ // 数字归约
  1737. fprintf(status_log, "[info][grammar] mode status: list_\n");
  1738. token gett, list_core, ri_t, new_token;
  1739. parameter *base = NULL;
  1740. gett = pop_node(list); // 取得一个token
  1741. if(gett.type == LI_PASER){ // var类型
  1742. status->is_list = true;
  1743. get_right_token(status,list,formal_parameter,list_core);
  1744. status->is_list = false;
  1745. if(list_core.type == RI_PASER){ // 空列表
  1746. base = NULL;
  1747. goto make_list;
  1748. }
  1749. else if(list_core.type == NON_parameter){
  1750. base = list_core.data.parameter_list;
  1751. }
  1752. else{
  1753. paser_error("Don't get formal_parameter");
  1754. }
  1755. get_pop_token(status,list,ri_t);
  1756. if(ri_t.type != RI_PASER){
  1757. paser_error("Don't get ']'");
  1758. }
  1759. make_list:
  1760. new_token.type = NON_list;
  1761. statement *code_tmp = make_statement();
  1762. code_tmp->type = base_list;
  1763. code_tmp->code.base_list.value = base;
  1764. new_token.data.statement_value = code_tmp;
  1765. new_token.data_type = statement_value;
  1766. fprintf(status_log, "[info][grammar] (var_token)out\n");
  1767. add_node(list, new_token); // 压入节点
  1768. return;
  1769. }
  1770. else{ // 不是期望值
  1771. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  1772. back_one_token(list, gett);
  1773. return;
  1774. }
  1775. }
  1776. /*
  1777. var_token : VAR
  1778. */
  1779. void var_token(p_status *status, token_node *list){ // 数字归约
  1780. fprintf(status_log, "[info][grammar] mode status: var_token\n");
  1781. token gett, new_token;
  1782. gett = pop_node(list); // 取得一个token
  1783. if(gett.type == VAR_PASER){ // var类型
  1784. new_token.type = NON_base_var;
  1785. GWARF_value tmp_value;
  1786. tmp_value.type = INT_value;
  1787. tmp_value.value.int_value = atoi(gett.data.text);
  1788. statement *code_tmp = make_statement();
  1789. code_tmp->type = base_var;
  1790. code_tmp->code.base_var.var_name = gett.data.text;
  1791. code_tmp->code.base_var.from = NULL;
  1792. new_token.data.statement_value = code_tmp;
  1793. new_token.data_type = statement_value;
  1794. fprintf(status_log, "[info][grammar] (var_token)out\n");
  1795. add_node(list, new_token); // 压入节点
  1796. return;
  1797. }
  1798. else{ // 不是期望值
  1799. fprintf(status_log, "[info][grammar] (var_token)back one token\n");
  1800. back_one_token(list, gett);
  1801. return;
  1802. }
  1803. }
  1804. /*
  1805. number : INT_PASER
  1806. | DOUBLE_PASER
  1807. | LB top_exp RB
  1808. */
  1809. void number(p_status *status, token_node *list){ // 数字归约
  1810. fprintf(status_log, "[info][grammar] mode status: number\n");
  1811. token gett, new_token;
  1812. gett = pop_node(list); // 取得一个token
  1813. if(gett.type == INT_PASER){ // int类型
  1814. new_token.type = NON_base_value;
  1815. GWARF_value tmp_value;
  1816. tmp_value.type = INT_value;
  1817. tmp_value.value.int_value = atoi(gett.data.text);
  1818. statement *code_tmp = make_statement();
  1819. code_tmp->type = call;
  1820. code_tmp->code.call.func = pack_call_name("int", NULL);
  1821. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1822. new_token.data.statement_value = code_tmp;
  1823. new_token.data_type = statement_value;
  1824. fprintf(status_log, "[info][grammar] (number)get int number: %d\n", tmp_value.value.int_value);
  1825. }
  1826. else if(gett.type == DOUBLE_PASER){
  1827. new_token.type = NON_base_value;
  1828. GWARF_value tmp_value;
  1829. tmp_value.type = NUMBER_value;
  1830. tmp_value.value.double_value = atof(gett.data.text);
  1831. statement *code_tmp = make_statement();
  1832. code_tmp->type = call;
  1833. code_tmp->code.call.func = pack_call_name("double", NULL);
  1834. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  1835. new_token.data.statement_value = code_tmp;
  1836. new_token.data_type = statement_value;
  1837. fprintf(status_log, "[info][grammar] (number)get double number: %f\n", new_token.data.d_number);
  1838. }
  1839. else{ // 不是期望值
  1840. fprintf(status_log, "[info][grammar] (number)back one token\n");
  1841. back_one_token(list, gett);
  1842. return;
  1843. }
  1844. free(gett.data.text); // 释放字符串
  1845. fprintf(status_log, "[info][grammar] (number)add one token\n");
  1846. add_node(list, new_token); // 压入节点
  1847. }
  1848. void paser_error(char *text){
  1849. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  1850. printf("[error][grammar] paser error : %s\n\n", text);
  1851. exit(1);
  1852. }