syntax.c 70 KB

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