syntax.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include"token.h"
  2. #include"lex.h"
  3. #include"../inter/interpreter.h"
  4. void factor(int *status, token_node *list);
  5. void number(int *status, token_node *list);
  6. void polynomial(int *status, token_node *list);
  7. void command(int *status, token_node *list);
  8. void while_(int *status, token_node *list);
  9. void block_(int *status, token_node *list);
  10. void paser_error(char *text);
  11. /*
  12. command_list : command
  13. | command_list command
  14. */
  15. void command_list(int *status, token_node *list){ // 多项式
  16. fprintf(status_log, "[info][grammar] mode status: polynomial\n", text);
  17. token left, right, new_token;
  18. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  19. if(left.type == NON_command_list){ // 模式2
  20. fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
  21. get_right_token(status, list, command, right); // 回调右边
  22. if(right.type == NON_command){
  23. new_token.type = NON_command_list;
  24. new_token.data_type = empty;
  25. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  26. return command_list(status, list); // 回调自己
  27. }
  28. else{ // 递归跳出[EOF_token]
  29. printf("right.type = %d\n", right.type);
  30. fprintf(status_log, "[info][grammar] (command_list)out\n");
  31. back_one_token(list, left);
  32. back_again(list, right);
  33. return;
  34. }
  35. }
  36. else if(left.type == EOF_token){ // 递归跳出的条件
  37. fprintf(status_log, "[info][grammar] (command_list)out\n");
  38. return;
  39. }
  40. else{ // 模式1
  41. fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
  42. back_one_token(list, left);
  43. get_base_token(status, list, command, new_token);
  44. if(new_token.type != NON_command){
  45. back_one_token(list, new_token); // 往回[不匹配类型]
  46. return;
  47. }
  48. new_token.type = NON_command_list;
  49. add_node(list, new_token);
  50. return command_list(status, list); // 回调自己
  51. }
  52. }
  53. /*
  54. command : polynomial <ENTER>
  55. */
  56. void command(int *status, token_node *list){ // 多项式
  57. fprintf(status_log, "[info][grammar] mode status: polynomial\n", text);
  58. token left, new_token;
  59. left = pop_node(list); // 先弹出一个token 检查token
  60. if(left.type == WHILE_PASER){ // 是while类型的数据
  61. fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
  62. back_one_token(list, left);
  63. get_base_token(status, list, while_, new_token);
  64. get_stop_token();
  65. push_statement(statement_base, new_token);
  66. }
  67. else if(left.type == ENTER_PASER){
  68. fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
  69. }
  70. else if(left.type == EOF_token){
  71. fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
  72. back_one_token(list, left);
  73. goto return_back;
  74. }
  75. else{ // 表达式
  76. fprintf(status_log, "[info][grammar] (command)back one token to (polynomial)\n");
  77. back_one_token(list, left);
  78. get_base_token(status, list, polynomial, new_token);
  79. if(new_token.type != NON_polynomial){
  80. back_one_token(list, new_token); // 往回[不匹配类型]
  81. return;
  82. }
  83. get_stop_token();
  84. push_statement(statement_base, new_token);
  85. }
  86. new_token.type = NON_command;
  87. add_node(list, new_token);
  88. return_back:
  89. return; // 回调自己
  90. }
  91. /*
  92. while_ : WHILE LB polynomial RB block // TODO:把polynomial改为top_exp
  93. */
  94. void while_(int *status, token_node *list){
  95. fprintf(status_log, "[info][grammar] mode status: while_\n");
  96. token while_t, lb_t, exp_t, rb_t, block_t, new_token;
  97. while_t = pop_node(list);
  98. if(while_t.type == WHILE_PASER){
  99. get_pop_token(status, list, lb_t);
  100. if(lb_t.type != LB_PASER){
  101. paser_error("Don't get '('");
  102. }
  103. get_right_token(status,list,polynomial,exp_t);
  104. if(exp_t.type != NON_polynomial){ // 不是表达式
  105. paser_error("Don't get 'polynomial'");
  106. }
  107. get_pop_token(status, list, rb_t);
  108. if(rb_t.type != RB_PASER){
  109. paser_error("Don't get ')'");
  110. }
  111. get_right_token(status,list,block_,block_t);
  112. if(block_t.type != NON_block){ // 不是表达式
  113. paser_error("Don't get '{'");
  114. }
  115. statement *while_tmp = make_statement();
  116. while_tmp->type = while_cycle;
  117. while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
  118. while_tmp->code.while_cycle.done = block_t.data.statement_value;
  119. new_token.type = NON_while;
  120. new_token.data_type = statement_value;
  121. new_token.data.statement_value = while_tmp;
  122. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  123. return;
  124. }
  125. else{
  126. back_one_token(list, while_t);
  127. return;
  128. }
  129. }
  130. /*
  131. block_ : LP command_list RB
  132. */
  133. void block_(int *status, token_node *list){
  134. fprintf(status_log, "[info][grammar] mode status: block_\n");
  135. token lp_t, rp_t, new_token, command_list_t;
  136. lp_t = pop_node(list);
  137. if(lp_t.type == LP_PASER){
  138. statement *block_tmp = make_statement();
  139. statement_base = append_statement_list(block_tmp, statement_base);
  140. get_right_token(status,list,command_list,command_list_t);
  141. statement_base = free_statement_list(statement_base); // 重新释放
  142. get_pop_token(status, list, rp_t);
  143. if(rp_t.type != RP_PASER){
  144. printf("rp_t.type = %d\n", rp_t.type);
  145. paser_error("Don't get '}'");
  146. }
  147. new_token.type = NON_block;
  148. new_token.data_type = statement_value;
  149. new_token.data.statement_value = block_tmp;
  150. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  151. return;
  152. }
  153. else{
  154. back_one_token(list, lp_t);
  155. return;
  156. }
  157. }
  158. /*
  159. polynomial : factor
  160. | polynomial ADD factor
  161. | polynomial SUB factor
  162. */
  163. void polynomial(int *status, token_node *list){ // 多项式
  164. fprintf(status_log, "[info][grammar] mode status: polynomial\n");
  165. token left, right, symbol, new_token;
  166. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  167. if(left.type == NON_polynomial){ // 模式2/3
  168. fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
  169. get_pop_token(status, list, symbol);
  170. if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
  171. get_right_token(status, list, factor, right); // 回调右边
  172. if(right.type != NON_factor){
  173. paser_error("Don't get a factor");
  174. }
  175. new_token.type = NON_polynomial;
  176. new_token.data_type = statement_value;
  177. statement *code_tmp = make_statement();
  178. code_tmp->type = operation;
  179. if(symbol.type == ADD_PASER){
  180. code_tmp->code.operation.type = ADD_func;
  181. }
  182. else{
  183. code_tmp->code.operation.type = SUB_func;
  184. }
  185. code_tmp->code.operation.left_exp = left.data.statement_value;
  186. code_tmp->code.operation.right_exp = right.data.statement_value;
  187. new_token.data.statement_value = code_tmp;
  188. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  189. return polynomial(status, list); // 回调自己
  190. }
  191. else{ // 递归跳出
  192. fprintf(status_log, "[info][grammar] (polynomial)out\n");
  193. back_one_token(list, left);
  194. back_again(list, symbol);
  195. return;
  196. }
  197. }
  198. else{ // 模式1
  199. fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
  200. back_one_token(list, left);
  201. get_base_token(status, list, factor, new_token);
  202. if(new_token.type != NON_factor){
  203. back_one_token(list, new_token); // 往回[不匹配类型]
  204. return;
  205. }
  206. new_token.type = NON_polynomial;
  207. add_node(list, new_token);
  208. return polynomial(status, list); // 回调自己
  209. }
  210. }
  211. /*
  212. factor : number
  213. | factor MUL number
  214. | factor DIV number
  215. */
  216. void factor(int *status, token_node *list){ // 因试分解
  217. fprintf(status_log, "[info][grammar] mode status: factor\n");
  218. token left, right, symbol, new_token;
  219. left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
  220. if(left.type == NON_factor){ // 模式2/3
  221. fprintf(status_log, "[info][grammar] (factor)reduce right\n");
  222. get_pop_token(status, list, symbol);
  223. if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
  224. get_right_token(status, list, number, right); // 回调右边
  225. if(right.type != NON_base_value){
  226. paser_error("Don't get a value");
  227. }
  228. // 逻辑操作
  229. new_token.type = NON_factor;
  230. new_token.data_type = statement_value;
  231. statement *code_tmp = make_statement();
  232. code_tmp->type = operation;
  233. if(symbol.type == MUL_PASER){
  234. code_tmp->code.operation.type = MUL_func;
  235. }
  236. else{
  237. code_tmp->code.operation.type = DIV_func;
  238. }
  239. code_tmp->code.operation.left_exp = left.data.statement_value;
  240. code_tmp->code.operation.right_exp = right.data.statement_value;
  241. new_token.data.statement_value = code_tmp;
  242. add_node(list, new_token); // 压入节点[弹出3个压入1个]
  243. return factor(status, list); // 回调自己
  244. }
  245. else{ // 递归跳出
  246. // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
  247. fprintf(status_log, "[info][grammar] (factor)out\n");
  248. back_one_token(list, left);
  249. back_again(list, symbol);
  250. return;
  251. }
  252. }
  253. else{ // 模式1
  254. fprintf(status_log, "[info][grammar] (factor)back one token to (number)\n");
  255. back_one_token(list, left);
  256. get_base_token(status, list, number, new_token);
  257. if(new_token.type != NON_base_value){
  258. back_one_token(list, new_token); // 往回[不匹配类型]
  259. return;
  260. }
  261. new_token.type = NON_factor;
  262. add_node(list, new_token);
  263. return factor(status, list); // 回调自己
  264. }
  265. }
  266. /*
  267. number : INT_PASER
  268. | DOUBLE_PASER
  269. | LB polynomial RB
  270. */
  271. void number(int *status, token_node *list){ // 数字归约
  272. fprintf(status_log, "[info][grammar] mode status: number\n");
  273. token gett, new_token;
  274. gett = pop_node(list); // 取得一个token
  275. if(gett.type == INT_PASER){ // int类型
  276. new_token.type = NON_base_value;
  277. GWARF_value tmp_value;
  278. tmp_value.type = INT_value;
  279. tmp_value.value.int_value = atoi(gett.data.text);
  280. statement *code_tmp = make_statement();
  281. code_tmp->type = call;
  282. code_tmp->code.call.func = pack_call_name("int", NULL);
  283. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  284. new_token.data.statement_value = code_tmp;
  285. new_token.data_type = statement_value;
  286. fprintf(status_log, "[info][grammar] (number)get int number: %d\n", tmp_value.value.int_value);
  287. }
  288. else if(gett.type == DOUBLE_PASER){
  289. new_token.type = NON_base_value;
  290. GWARF_value tmp_value;
  291. tmp_value.type = NUMBER_value;
  292. tmp_value.value.double_value = atof(gett.data.text);
  293. statement *code_tmp = make_statement();
  294. code_tmp->type = call;
  295. code_tmp->code.call.func = pack_call_name("double", NULL);
  296. code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
  297. new_token.data.statement_value = code_tmp;
  298. new_token.data_type = statement_value;
  299. fprintf(status_log, "[info][grammar] (number)get double number: %f\n", new_token.data.d_number);
  300. }
  301. else if(gett.type == LB_PASER){ // 模式3
  302. fprintf(status_log, "[info][grammar] (number)get LB\n");
  303. get_right_token(status, list, polynomial, new_token);
  304. new_token.type = NON_base_value;
  305. token rb;
  306. get_pop_token(status, list ,rb);
  307. if(rb.type != RB_PASER){ // 匹配失败
  308. paser_error("Don't get ')'");
  309. }
  310. }
  311. else{ // 不是期望值
  312. fprintf(status_log, "[info][grammar] (number)back one token\n");
  313. back_one_token(list, gett);
  314. return;
  315. }
  316. free(gett.data.text); // 释放字符串
  317. fprintf(status_log, "[info][grammar] (number)add one token\n");
  318. add_node(list, new_token); // 压入节点
  319. }
  320. void paser_error(char *text){
  321. fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
  322. printf("[error][grammar] paser error : %s\n\n", text);
  323. exit(1);
  324. }