123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- #include"token.h"
- #include"lex.h"
- #include"../inter/interpreter.h"
- void factor(int *status, token_node *list);
- void number(int *status, token_node *list);
- void polynomial(int *status, token_node *list);
- void command(int *status, token_node *list);
- void while_(int *status, token_node *list);
- void block_(int *status, token_node *list);
- void paser_error(char *text);
- /*
- command_list : command
- | command_list command
- */
- void command_list(int *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: polynomial\n", text);
- token left, right, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_command_list){ // 模式2
- fprintf(status_log, "[info][grammar] (command_list)reduce right\n");
- get_right_token(status, list, command, right); // 回调右边
- if(right.type == NON_command){
- new_token.type = NON_command_list;
- new_token.data_type = empty;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return command_list(status, list); // 回调自己
- }
- else{ // 递归跳出[EOF_token]
- printf("right.type = %d\n", right.type);
- fprintf(status_log, "[info][grammar] (command_list)out\n");
- back_one_token(list, left);
- back_again(list, right);
- return;
- }
- }
- else if(left.type == EOF_token){ // 递归跳出的条件
- fprintf(status_log, "[info][grammar] (command_list)out\n");
- return;
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (command_list)back one token to (command)\n");
- back_one_token(list, left);
- get_base_token(status, list, command, new_token);
- if(new_token.type != NON_command){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_command_list;
- add_node(list, new_token);
- return command_list(status, list); // 回调自己
- }
- }
- /*
- command : polynomial <ENTER>
- */
- void command(int *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: polynomial\n", text);
- token left, new_token;
- left = pop_node(list); // 先弹出一个token 检查token
- if(left.type == WHILE_PASER){ // 是while类型的数据
- fprintf(status_log, "[info][grammar] (command)back one token to (while)\n");
- back_one_token(list, left);
- get_base_token(status, list, while_, new_token);
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- else if(left.type == ENTER_PASER){
- fprintf(status_log, "[info][grammar] (command)back <ENTER>\n");
- }
- else if(left.type == EOF_token){
- fprintf(status_log, "[info][grammar] (command)back <EOF>\n");
- back_one_token(list, left);
- goto return_back;
- }
- else{ // 表达式
- fprintf(status_log, "[info][grammar] (command)back one token to (polynomial)\n");
- back_one_token(list, left);
- get_base_token(status, list, polynomial, new_token);
- if(new_token.type != NON_polynomial){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- get_stop_token();
- push_statement(statement_base, new_token);
- }
- new_token.type = NON_command;
- add_node(list, new_token);
- return_back:
- return; // 回调自己
- }
- /*
- while_ : WHILE LB polynomial RB block // TODO:把polynomial改为top_exp
- */
- void while_(int *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: while_\n");
- token while_t, lb_t, exp_t, rb_t, block_t, new_token;
- while_t = pop_node(list);
- if(while_t.type == WHILE_PASER){
- get_pop_token(status, list, lb_t);
- if(lb_t.type != LB_PASER){
- paser_error("Don't get '('");
- }
- get_right_token(status,list,polynomial,exp_t);
- if(exp_t.type != NON_polynomial){ // 不是表达式
- paser_error("Don't get 'polynomial'");
- }
- get_pop_token(status, list, rb_t);
- if(rb_t.type != RB_PASER){
- paser_error("Don't get ')'");
- }
- get_right_token(status,list,block_,block_t);
- if(block_t.type != NON_block){ // 不是表达式
- paser_error("Don't get '{'");
- }
- statement *while_tmp = make_statement();
- while_tmp->type = while_cycle;
- while_tmp->code.while_cycle.condition = exp_t.data.statement_value;
- while_tmp->code.while_cycle.done = block_t.data.statement_value;
- new_token.type = NON_while;
- new_token.data_type = statement_value;
- new_token.data.statement_value = while_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, while_t);
- return;
- }
- }
- /*
- block_ : LP command_list RB
- */
- void block_(int *status, token_node *list){
- fprintf(status_log, "[info][grammar] mode status: block_\n");
- token lp_t, rp_t, new_token, command_list_t;
- lp_t = pop_node(list);
- if(lp_t.type == LP_PASER){
- statement *block_tmp = make_statement();
- statement_base = append_statement_list(block_tmp, statement_base);
-
- get_right_token(status,list,command_list,command_list_t);
- statement_base = free_statement_list(statement_base); // 重新释放
- get_pop_token(status, list, rp_t);
- if(rp_t.type != RP_PASER){
- printf("rp_t.type = %d\n", rp_t.type);
- paser_error("Don't get '}'");
- }
- new_token.type = NON_block;
- new_token.data_type = statement_value;
- new_token.data.statement_value = block_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return;
- }
- else{
- back_one_token(list, lp_t);
- return;
- }
- }
- /*
- polynomial : factor
- | polynomial ADD factor
- | polynomial SUB factor
- */
- void polynomial(int *status, token_node *list){ // 多项式
- fprintf(status_log, "[info][grammar] mode status: polynomial\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_polynomial){ // 模式2/3
- fprintf(status_log, "[info][grammar] (polynomial)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == ADD_PASER || symbol.type == SUB_PASER){ // 模式2/3
- get_right_token(status, list, factor, right); // 回调右边
- if(right.type != NON_factor){
- paser_error("Don't get a factor");
- }
- new_token.type = NON_polynomial;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == ADD_PASER){
- code_tmp->code.operation.type = ADD_func;
- }
- else{
- code_tmp->code.operation.type = SUB_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return polynomial(status, list); // 回调自己
- }
- else{ // 递归跳出
- fprintf(status_log, "[info][grammar] (polynomial)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (polynomial)back one token to (factor)\n");
- back_one_token(list, left);
- get_base_token(status, list, factor, new_token);
- if(new_token.type != NON_factor){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_polynomial;
- add_node(list, new_token);
- return polynomial(status, list); // 回调自己
- }
- }
- /*
- factor : number
- | factor MUL number
- | factor DIV number
- */
- void factor(int *status, token_node *list){ // 因试分解
- fprintf(status_log, "[info][grammar] mode status: factor\n");
- token left, right, symbol, new_token;
- left = pop_node(list); // 先弹出一个token 检查token的类型:区分是模式1,还是模式2/3
- if(left.type == NON_factor){ // 模式2/3
- fprintf(status_log, "[info][grammar] (factor)reduce right\n");
- get_pop_token(status, list, symbol);
- if(symbol.type == MUL_PASER || symbol.type == DIV_PASER){ // 模式2/3
- get_right_token(status, list, number, right); // 回调右边
- if(right.type != NON_base_value){
- paser_error("Don't get a value");
- }
- // 逻辑操作
- new_token.type = NON_factor;
- new_token.data_type = statement_value;
- statement *code_tmp = make_statement();
- code_tmp->type = operation;
- if(symbol.type == MUL_PASER){
- code_tmp->code.operation.type = MUL_func;
- }
- else{
- code_tmp->code.operation.type = DIV_func;
- }
- code_tmp->code.operation.left_exp = left.data.statement_value;
- code_tmp->code.operation.right_exp = right.data.statement_value;
- new_token.data.statement_value = code_tmp;
- add_node(list, new_token); // 压入节点[弹出3个压入1个]
- return factor(status, list); // 回调自己
- }
- else{ // 递归跳出
- // 回退,也就是让下一次pop的时候读取到的是left而不是symbol
- fprintf(status_log, "[info][grammar] (factor)out\n");
- back_one_token(list, left);
- back_again(list, symbol);
- return;
- }
- }
- else{ // 模式1
- fprintf(status_log, "[info][grammar] (factor)back one token to (number)\n");
- back_one_token(list, left);
- get_base_token(status, list, number, new_token);
- if(new_token.type != NON_base_value){
- back_one_token(list, new_token); // 往回[不匹配类型]
- return;
- }
- new_token.type = NON_factor;
- add_node(list, new_token);
- return factor(status, list); // 回调自己
- }
- }
- /*
- number : INT_PASER
- | DOUBLE_PASER
- | LB polynomial RB
- */
- void number(int *status, token_node *list){ // 数字归约
- fprintf(status_log, "[info][grammar] mode status: number\n");
- token gett, new_token;
- gett = pop_node(list); // 取得一个token
- if(gett.type == INT_PASER){ // int类型
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = INT_value;
- tmp_value.value.int_value = atoi(gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("int", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- fprintf(status_log, "[info][grammar] (number)get int number: %d\n", tmp_value.value.int_value);
- }
- else if(gett.type == DOUBLE_PASER){
- new_token.type = NON_base_value;
- GWARF_value tmp_value;
- tmp_value.type = NUMBER_value;
- tmp_value.value.double_value = atof(gett.data.text);
- statement *code_tmp = make_statement();
- code_tmp->type = call;
- code_tmp->code.call.func = pack_call_name("double", NULL);
- code_tmp->code.call.parameter_list = pack_value_parameter(tmp_value);
- new_token.data.statement_value = code_tmp;
- new_token.data_type = statement_value;
- fprintf(status_log, "[info][grammar] (number)get double number: %f\n", new_token.data.d_number);
- }
- else if(gett.type == LB_PASER){ // 模式3
- fprintf(status_log, "[info][grammar] (number)get LB\n");
- get_right_token(status, list, polynomial, new_token);
- new_token.type = NON_base_value;
- token rb;
- get_pop_token(status, list ,rb);
- if(rb.type != RB_PASER){ // 匹配失败
- paser_error("Don't get ')'");
- }
- }
- else{ // 不是期望值
- fprintf(status_log, "[info][grammar] (number)back one token\n");
- back_one_token(list, gett);
- return;
- }
- free(gett.data.text); // 释放字符串
- fprintf(status_log, "[info][grammar] (number)add one token\n");
- add_node(list, new_token); // 压入节点
- }
- void paser_error(char *text){
- fprintf(status_log, "[error][grammar] paser error : %s\n\n", text);
- printf("[error][grammar] paser error : %s\n\n", text);
- exit(1);
- }
|