syntax.c 68 KB

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