嵌入式单片机设计吧 关注:23贴子:95
  • 6回复贴,共1

C语言编译器——语法分析器

只看楼主收藏回复

可以简单分析赋值语句 如:char a = 10;
生成AST树
const char* const AST_string[] = {
"CharDeclaration",
"AssignmentExp",
"IntLiteral",
};
enum syntactic_parser_types{
CharDeclaration = 0,
AssignmentExp,
Ast_IntLiteral,
}syntactic_parser_type;
typedef Save_Token_t tokens_fun_t(void);
typedef struct tokens_fun{
tokens_fun_t *peek;
tokens_fun_t *read;
}Token_fun;
static int save_token_read_index = 0;
Save_Token_t token_peek(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index];
}
Save_Token_t token_read(void){
if(save_token_read_index >= save_token_index){
//return NULL;
}
return Mytokens[save_token_read_index++];
}
typedef struct SimpleASTNode{
enum syntactic_parser_types type;
char text[Id_LENGTH];
int level;
}SimpleASTNode_t;
SimpleASTNode_t AST_node[20] = {};
static int AST_index = 0;
void add_AST(enum syntactic_parser_types type,char* text,int level){
AST_node[AST_index].type = type;
strcpy(AST_node[AST_index].text,text);
AST_node[AST_index].level = level;
AST_index++;
}
void printf_AST(){
int i,j;
printf("\r\n=====syntactic_parser======\r\n\r\n");
for( i = 0; i < AST_index; i++){
for(j = 0; j < (AST_node[i].level-1);j++){
//printf("\t");
printf(" ");
}
printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
}
}
int syntactic_parser(){
Save_Token_t token;
Token_fun tokens_fun;
tokens_fun.peek = token_peek;
tokens_fun.read = token_read;
token = tokens_fun.peek();//预读
//if((token != NULL) && (token.type == CHAR)){
if( (token.type == CHAR)){
token = tokens_fun.read();//消耗掉char
token = tokens_fun.peek();
if(token.type == Id){
token = tokens_fun.read();
//创建当前节点,并把变量名记到AST节点的文本值中,
add_AST(CharDeclaration,token.text,1);
token = tokens_fun.peek();//预读
if(token.type == Assignment){
add_AST(AssignmentExp,token.text,2);
token = tokens_fun.read();
token = tokens_fun.peek();//预读
if(token.type == IntLiteral){
token = tokens_fun.read();
add_AST(Ast_IntLiteral,token.text,3);
}
}
}
}
printf_AST();
}
int main(){
Lexical_analyzer();
syntactic_parser();
}


IP属地:北京1楼2022-08-13 15:46回复
    const char* const AST_string[] = {
    "CharDeclaration",
    "AssignmentExp",
    "IntLiteral",
    };
    enum syntactic_parser_types{
    CharDeclaration = 0,
    AssignmentExp,
    Ast_IntLiteral,
    }syntactic_parser_type;
    typedef Save_Token_t tokens_fun_t(void);
    typedef struct tokens_fun{
    tokens_fun_t *peek;
    tokens_fun_t *read;
    }Token_fun;
    static int save_token_read_index = 0;
    Save_Token_t token_peek(void){
    if(save_token_read_index >= save_token_index){
    //return NULL;
    }
    return Mytokens[save_token_read_index];
    }
    Save_Token_t token_read(void){
    if(save_token_read_index >= save_token_index){
    //return NULL;
    }
    return Mytokens[save_token_read_index++];
    }
    typedef struct SimpleASTNode{
    enum syntactic_parser_types type;
    char text[Id_LENGTH];
    //int level;
    struct SimpleASTNode* left;
    struct SimpleASTNode* right;
    }SimpleASTNode_t;
    SimpleASTNode_t AST_node[20] = {};
    static int AST_index = 0;
    SimpleASTNode_t * AST_node_roots[20];
    static int AST_note_index = 0;
    void add_AST(enum syntactic_parser_types type,char* text){
    AST_node[AST_index].type = type;
    strcpy(AST_node[AST_index].text,text);
    AST_index++;
    }
    void new_SimpleASTNode(enum syntactic_parser_types type,char* text){
    AST_node[AST_index].type = type;
    strcpy(AST_node[AST_index].text,text);
    AST_index++;
    AST_node[AST_index].left = NULL;
    AST_node[AST_index].right = NULL;
    AST_node_roots[AST_note_index] = &AST_node[AST_index-1];
    AST_note_index++;
    }
    SimpleASTNode_t additive(Save_Token_t token){
    }
    void printf_AST(){
    int i,j;
    printf("\r\n=====syntactic_parser======\r\n\r\n");
    for( i = 0; i < AST_index; i++){
    printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
    }
    }
    int syntactic_parser(){
    Save_Token_t token;
    Token_fun tokens_fun;
    tokens_fun.peek = token_peek;
    tokens_fun.read = token_read;
    token = tokens_fun.peek();//预读
    //if((token != NULL) && (token.type == CHAR)){
    if( (token.type == CHAR)){
    token = tokens_fun.read();//消耗掉char
    token = tokens_fun.peek();
    if(token.type == Id){
    token = tokens_fun.read();
    //创建当前节点,并把变量名记到AST节点的文本值中,
    new_SimpleASTNode(CharDeclaration,token.text);
    token = tokens_fun.peek();//预读
    if(token.type == Assignment){
    //add_AST(AssignmentExp,token.text);
    token = tokens_fun.read();
    SimpleASTNode_t child = additive(token); //匹配一个表达式
    }
    }
    }
    printf_AST();
    }
    int main(){
    Lexical_analyzer();
    syntactic_parser();
    }


    IP属地:北京2楼2022-08-13 19:48
    回复
      可以解析赋值语句
      char abc = 10+5*4;
      char def = 10+5+4;
      、、==========================
      const char* const AST_string[] = {
      "CharDeclaration",
      "AssignmentExp",
      "IntLiteral",
      "AdditiveExp",//+
      "MulticativeExp",//*
      };
      enum syntactic_parser_types{
      CharDeclaration = 0,
      AssignmentExp,
      Ast_IntLiteral,
      AdditiveExp,//+
      MulticativeExp,//*
      }syntactic_parser_type;
      typedef Save_Token_t* tokens_fun_t(void);
      typedef struct tokens_fun{
      tokens_fun_t *peek;
      tokens_fun_t *read;
      }Token_fun;
      static int save_token_read_index = 0;
      Save_Token_t* token_peek(void){
      //printf("token_peek %d==%d\r\n",save_token_read_index,save_token_index);
      if(save_token_read_index < save_token_index){
      return &Mytokens[save_token_read_index];
      }
      return NULL;
      }
      Save_Token_t* token_read(void){
      if(save_token_read_index < save_token_index){
      save_token_read_index++;
      return &Mytokens[save_token_read_index-1];
      }
      return NULL;
      }
      typedef struct SimpleASTNode{
      enum syntactic_parser_types type;
      char text[Id_LENGTH];
      //int level;
      struct SimpleASTNode* left;
      struct SimpleASTNode* right;
      }SimpleASTNode_t;
      SimpleASTNode_t AST_node[20] = {};
      static int AST_index = 0;
      SimpleASTNode_t * AST_node_roots[20];
      static int AST_note_index = 0;
      void add_AST(enum syntactic_parser_types type,char* text){
      AST_node[AST_index].type = type;
      strcpy(AST_node[AST_index].text,text);
      AST_index++;
      }
      void new_SimpleASTNode(enum syntactic_parser_types type,char* text){
      AST_node[AST_index].type = type;
      strcpy(AST_node[AST_index].text,text);
      AST_index++;
      AST_node[AST_index].left = NULL;
      AST_node[AST_index].right = NULL;
      AST_node_roots[AST_note_index] = &AST_node[AST_index-1];
      AST_note_index++;
      }
      Token_fun tokens_fun;
      SimpleASTNode_t *new_SimpleASTNode_2(enum syntactic_parser_types type,char* text){
      AST_node[AST_index].type = type;
      strcpy(AST_node[AST_index].text,text);
      AST_node[AST_index].left = NULL;
      AST_node[AST_index].right = NULL;
      AST_index++;
      return &AST_node[AST_index-1];
      }
      void Node_add_child(SimpleASTNode_t *node,SimpleASTNode_t *child){
      if(node == NULL){
      return;
      }
      if(node->left == NULL){
      node->left = child;
      }else if(node->right == NULL){
      node->right = child;
      }
      }
      #define DEBUG(x) printf(x)
      SimpleASTNode_t * multiplicative(){
      SimpleASTNode_t *node = NULL;
      SimpleASTNode_t *child1 = NULL;
      Save_Token_t* token;
      token = tokens_fun.peek();
      if((token != NULL) && (token->type == IntLiteral)){
      token = tokens_fun.read();//shu zhi
      child1 = new_SimpleASTNode_2(Ast_IntLiteral,token->text);
      node = child1;
      token = tokens_fun.peek();//*
      if((token != NULL) && (token->type == Star)){
      token = tokens_fun.read();//*
      SimpleASTNode_t *child2 = multiplicative();
      if(child2 != NULL){
      node = new_SimpleASTNode_2(MulticativeExp,token->text);
      Node_add_child(node,child1);
      Node_add_child(node,child2);
      }else{
      printf("invalid multiplicative expression, expecting the right part.");
      }
      }
      }
      return node;
      }
      SimpleASTNode_t* additive(Save_Token_t* tokens){
      Save_Token_t* token;
      SimpleASTNode_t *child1 = multiplicative();//ji suan di yi ge zhi jie dian
      SimpleASTNode_t *node = child1;
      token = tokens_fun.peek();
      if((child1 != NULL) && (token != NULL)){
      if(token->type == Plus){
      token = tokens_fun.read();
      SimpleASTNode_t *child2 = additive(token);
      if(child2 != NULL){
      node = new_SimpleASTNode_2(AdditiveExp,token->text);
      Node_add_child(node,child1);
      Node_add_child(node,child2);
      }else{
      DEBUG("invalid additive expression, expecting the right part.");
      }
      }
      }
      return node;
      }
      int get_Tree_Depth(SimpleASTNode_t* Tree){
      int L,R;
      if(Tree == NULL){
      return 0;
      }else{
      L = get_Tree_Depth(Tree->left);
      R = get_Tree_Depth(Tree->right);
      if(L > R){
      return L + 1;
      }
      return R+1;
      }
      }
      void printf_AST(SimpleASTNode_t* Tree,int Depth,int for_depth,int hight){
      int i,j;
      //for( i = 0; i < AST_index; i++){
      //printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
      //}
      if(hight == 0){
      for(i = 0;i < (Depth - for_depth - 2);i++){
      //printf(" ");
      }
      }
      //printf("%d%%=%s %s\r\n",hight,AST_string[Tree->type],Tree->text);
      printf("%s %s\r\n",AST_string[Tree->type],Tree->text);
      if(Tree->left != NULL){
      for(i = 0;i < hight+1;i++){
      printf(" ");
      }
      //printf("/");
      printf_AST(Tree->left,Depth,for_depth+1,hight+1);
      }
      if(Tree->right != NULL){
      for(i = 0;i < hight+1;i++){
      printf(" ");
      }
      //printf("\\");
      printf_AST(Tree->right,Depth,for_depth-1,hight+1);
      }
      }
      int syntactic_parser(){
      Save_Token_t* token;
      SimpleASTNode_t* child;
      SimpleASTNode_t* node;
      tokens_fun.peek = token_peek;
      tokens_fun.read = token_read;
      while(1){
      token = tokens_fun.peek();//预读
      if(token == NULL){
      return 0;
      }
      if((token != NULL) && (token->type == CHAR)){
      //if( (token.type == CHAR)){
      token = tokens_fun.read();//消耗掉char
      token = tokens_fun.peek();
      if(token->type == Id){
      token = tokens_fun.read();
      //创建当前节点,并把变量名记到AST节点的文本值中,
      node = new_SimpleASTNode_2(CharDeclaration,token->text);
      token = tokens_fun.peek();//预读
      if(token->type == Assignment){
      //add_AST(AssignmentExp,token.text);
      token = tokens_fun.read();
      child = additive(token); //匹配一个表达式
      //child = multiplicative();
      Node_add_child(node,child);
      }
      }
      }
      printf("\r\n=====syntactic_parser======\r\n\r\n");
      int Depth = get_Tree_Depth(node);
      printf_AST(node,Depth,0,0);
      }
      }
      int main(){
      Lexical_analyzer();
      syntactic_parser();
      }


      IP属地:北京3楼2022-08-16 12:03
      回复
        AST计算表达式的值 ,如 3+4*5
        int evaluate(SimpleASTNode_t* node,int tab_num){
        int result = 0;
        int value1;
        int value2;
        SimpleASTNode_t* child;
        SimpleASTNode_t* child1;
        SimpleASTNode_t* child2;
        switch(node->type){
        case CharDeclaration:
        child = node->left;
        result = evaluate(child,tab_num+1);
        break;
        case AdditiveExp:
        child1 = node->left;
        value1 = evaluate(child1,tab_num+1);
        child2 = node->right;
        value2 = evaluate(child2,tab_num+1);
        if(strcmp(node->text,"+") == 0){
        result = value1 + value2;
        }
        break;
        case MulticativeExp:
        child1 = node->left;
        value1 = evaluate(child1,tab_num+1);
        child2 = node->right;
        value2 = evaluate(child2,tab_num+1);
        if(strcmp(node->text,"*") == 0){
        result = value1 * value2;
        }
        break;
        case Ast_IntLiteral:
        result = atoi(node->text);
        break;
        default :
        break;
        }
        for(int i = 0; i < tab_num;i++){
        printf(" ");
        }
        printf("Result: %d\r\n",result);
        return result;
        }


        IP属地:北京4楼2022-08-16 17:44
        回复
          支持小括号 char abc = 2+3*(3+5)
          SimpleASTNode_t* additive(Save_Token_t* tokens);
          SimpleASTNode_t * primary(Save_Token_t* tokens){
          SimpleASTNode_t * node = NULL;
          Save_Token_t* token;
          token = tokens_fun.peek();
          if(token != NULL){
          if(token->type == IntLiteral){//123
          token = tokens_fun.read();
          node = new_SimpleASTNode_2(Ast_IntLiteral,token->text);
          }else if(token->type == Id){//abc
          token = tokens_fun.read();
          node = new_SimpleASTNode_2(Ast_Identifier,token->text);
          }else if(token->type == LeftParen){//(
          token = tokens_fun.read();
          node = additive(tokens);
          if(node != NULL){
          token = tokens_fun.peek();
          if((token != NULL) && (token->type == RightParen) ){
          token = tokens_fun.read();
          }else{
          printf("expecting right parenthesis");
          }
          }
          }else{
          printf("expecting an additive expression inside parenthesis");//括号内应为加法表达式
          }
          }
          return node;
          }
          SimpleASTNode_t * multiplicative(Save_Token_t* tokens){
          Save_Token_t* token;
          SimpleASTNode_t *node = NULL;
          SimpleASTNode_t *child1 = primary(tokens);
          node = child1;
          token = tokens_fun.peek();
          if((token != NULL) && (child1 != NULL)){
          if((token->type == Star) || (token->type == Slash)){
          token = tokens_fun.read();//*
          SimpleASTNode_t *child2 = primary(tokens);
          if(child2 != NULL){
          node = new_SimpleASTNode_2(MulticativeExp,token->text);
          Node_add_child(node,child1);
          Node_add_child(node,child2);
          }else{
          printf("invalid multiplicative expression, expecting the right part.");
          }
          }
          }
          return node;
          }
          SimpleASTNode_t* additive(Save_Token_t* tokens){
          Save_Token_t* token;
          SimpleASTNode_t *child1 = multiplicative(tokens);//ji suan di yi ge zhi jie dian
          SimpleASTNode_t *node = child1;
          token = tokens_fun.peek();
          if((child1 != NULL) && (token != NULL)){
          if((token->type == Plus) || (token->type == Minus) ){
          token = tokens_fun.read();
          SimpleASTNode_t *child2 = additive(token);
          if(child2 != NULL){
          node = new_SimpleASTNode_2(AdditiveExp,token->text);
          Node_add_child(node,child1);
          Node_add_child(node,child2);
          }else{
          DEBUG("invalid additive expression, expecting the right part.");
          }
          }
          }
          return node;
          }


          IP属地:北京5楼2022-08-16 20:33
          回复
            消除连加结合性的问题2+3+5
            SimpleASTNode_t* additive(Save_Token_t* tokens){
            Save_Token_t* token;
            SimpleASTNode_t *child1 = multiplicative(tokens);//ji suan di yi ge zhi jie dian
            SimpleASTNode_t *node = child1;
            if(child1 != NULL){
            while(1){//循环应用add'
            token = tokens_fun.peek();
            if((token != NULL) && ((token->type == Plus) || (token->type == Minus) )){
            token = tokens_fun.read();
            SimpleASTNode_t *child2 = multiplicative(tokens);
            node = new_SimpleASTNode_2(AdditiveExp,token->text);
            Node_add_child(node,child1);
            Node_add_child(node,child2);
            child1 = node;
            }else{
            break;
            }
            }
            }
            return node;
            }


            IP属地:北京6楼2022-08-17 10:08
            回复
              const char* const AST_string[] = {
              "CharDeclaration",
              "AssignmentExp",
              "IntLiteral",
              "AdditiveExp",//+
              "MulticativeExp",//*
              "Ast_Identifier",//abc_
              };
              enum syntactic_parser_types{
              CharDeclaration = 0,
              AssignmentExp,//=
              Ast_IntLiteral,//123
              AdditiveExp,//+
              MulticativeExp,//*
              Ast_Identifier,//abc_
              }syntactic_parser_type;
              typedef Save_Token_t* tokens_fun_t(void);
              typedef struct tokens_fun{
              tokens_fun_t *peek;
              tokens_fun_t *read;
              }Token_fun;
              static int save_token_read_index = 0;
              Save_Token_t* token_peek(void){
              //printf("token_peek %d==%d\r\n",save_token_read_index,save_token_index);
              if(save_token_read_index < save_token_index){
              return &Mytokens[save_token_read_index];
              }
              return NULL;
              }
              Save_Token_t* token_read(void){
              if(save_token_read_index < save_token_index){
              save_token_read_index++;
              return &Mytokens[save_token_read_index-1];
              }
              return NULL;
              }
              typedef struct SimpleASTNode{
              enum syntactic_parser_types type;
              char text[Id_LENGTH];
              //int level;
              struct SimpleASTNode* left;
              struct SimpleASTNode* right;
              }SimpleASTNode_t;
              SimpleASTNode_t AST_node[20] = {};
              static int AST_index = 0;
              SimpleASTNode_t * AST_node_roots[20];
              static int AST_note_index = 0;
              void add_AST(enum syntactic_parser_types type,char* text){
              AST_node[AST_index].type = type;
              strcpy(AST_node[AST_index].text,text);
              AST_index++;
              }
              void new_SimpleASTNode(enum syntactic_parser_types type,char* text){
              AST_node[AST_index].type = type;
              strcpy(AST_node[AST_index].text,text);
              AST_index++;
              AST_node[AST_index].left = NULL;
              AST_node[AST_index].right = NULL;
              AST_node_roots[AST_note_index] = &AST_node[AST_index-1];
              AST_note_index++;
              }
              Token_fun tokens_fun;
              SimpleASTNode_t *new_SimpleASTNode_2(enum syntactic_parser_types type,char* text){
              AST_node[AST_index].type = type;
              strcpy(AST_node[AST_index].text,text);
              AST_node[AST_index].left = NULL;
              AST_node[AST_index].right = NULL;
              AST_index++;
              return &AST_node[AST_index-1];
              }
              void Node_add_child(SimpleASTNode_t *node,SimpleASTNode_t *child){
              if(node == NULL){
              return;
              }
              if(node->left == NULL){
              node->left = child;
              }else if(node->right == NULL){
              node->right = child;
              }
              }
              #define DEBUG(x) printf(x)
              SimpleASTNode_t* additive(Save_Token_t* tokens);
              SimpleASTNode_t * primary(Save_Token_t* tokens){
              SimpleASTNode_t * node = NULL;
              Save_Token_t* token;
              token = tokens_fun.peek();
              if(token != NULL){
              if(token->type == IntLiteral){//123
              token = tokens_fun.read();
              node = new_SimpleASTNode_2(Ast_IntLiteral,token->text);
              }else if(token->type == Id){//abc
              token = tokens_fun.read();
              node = new_SimpleASTNode_2(Ast_Identifier,token->text);
              }else if(token->type == LeftParen){//(
              token = tokens_fun.read();
              node = additive(tokens);
              if(node != NULL){
              token = tokens_fun.peek();
              if((token != NULL) && (token->type == RightParen) ){
              token = tokens_fun.read();
              }else{
              printf("expecting right parenthesis");
              }
              }
              }else{
              printf("expecting an additive expression inside parenthesis");//括号内应为加法表达式
              }
              }
              return node;
              }
              SimpleASTNode_t * multiplicative2(Save_Token_t* tokens){//old
              Save_Token_t* token;
              SimpleASTNode_t *node = NULL;
              SimpleASTNode_t *child1 = primary(tokens);
              node = child1;
              token = tokens_fun.peek();
              if((token != NULL) && (child1 != NULL)){
              if((token->type == Star) || (token->type == Slash)){
              token = tokens_fun.read();//*
              SimpleASTNode_t *child2 = primary(tokens);
              if(child2 != NULL){
              node = new_SimpleASTNode_2(MulticativeExp,token->text);
              Node_add_child(node,child1);
              Node_add_child(node,child2);
              }else{
              printf("invalid multiplicative expression, expecting the right part.");
              }
              }
              }
              return node;
              }
              SimpleASTNode_t * multiplicative(Save_Token_t* tokens){
              Save_Token_t* token;
              SimpleASTNode_t *node = NULL;
              SimpleASTNode_t *child1 = primary(tokens);
              node = child1;
              if(child1 != NULL){
              while(1){
              token = tokens_fun.peek();
              if((token != NULL) && ((token->type == Star) || (token->type == Slash))){
              token = tokens_fun.read();
              SimpleASTNode_t *child2 = primary(tokens);
              node = new_SimpleASTNode_2(MulticativeExp,token->text);
              Node_add_child(node,child1);
              Node_add_child(node,child2);
              child1 = node;
              }else{
              break;
              }
              }
              }
              return node;
              }
              SimpleASTNode_t* additive2(Save_Token_t* tokens){
              Save_Token_t* token;
              SimpleASTNode_t *child1 = multiplicative(tokens);//ji suan di yi ge zhi jie dian
              SimpleASTNode_t *node = child1;
              token = tokens_fun.peek();
              if((child1 != NULL) && (token != NULL)){
              if((token->type == Plus) || (token->type == Minus) ){
              token = tokens_fun.read();
              SimpleASTNode_t *child2 = additive(token);
              if(child2 != NULL){
              node = new_SimpleASTNode_2(AdditiveExp,token->text);
              Node_add_child(node,child1);
              Node_add_child(node,child2);
              }else{
              DEBUG("invalid additive expression, expecting the right part.");
              }
              }
              }
              return node;
              }
              SimpleASTNode_t* additive(Save_Token_t* tokens){
              Save_Token_t* token;
              SimpleASTNode_t *child1 = multiplicative(tokens);//ji suan di yi ge zhi jie dian
              SimpleASTNode_t *node = child1;
              if(child1 != NULL){
              while(1){//循环应用add'
              token = tokens_fun.peek();
              if((token != NULL) && ((token->type == Plus) || (token->type == Minus) )){
              token = tokens_fun.read();
              SimpleASTNode_t *child2 = multiplicative(tokens);
              node = new_SimpleASTNode_2(AdditiveExp,token->text);
              Node_add_child(node,child1);
              Node_add_child(node,child2);
              child1 = node;
              }else{
              break;
              }
              }
              }
              return node;
              }
              int get_Tree_Depth(SimpleASTNode_t* Tree){
              int L,R;
              if(Tree == NULL){
              return 0;
              }else{
              L = get_Tree_Depth(Tree->left);
              R = get_Tree_Depth(Tree->right);
              if(L > R){
              return L + 1;
              }
              return R+1;
              }
              }
              void printf_AST(SimpleASTNode_t* Tree,int Depth,int for_depth,int hight){
              int i,j;
              //for( i = 0; i < AST_index; i++){
              //printf("%s %s\r\n",AST_string[AST_node[i].type],AST_node[i].text);
              //}
              if(hight == 0){
              for(i = 0;i < (Depth - for_depth - 2);i++){
              //printf(" ");
              }
              }
              //printf("%d%%=%s %s\r\n",hight,AST_string[Tree->type],Tree->text);
              printf("%s %s\r\n",AST_string[Tree->type],Tree->text);
              if(Tree->left != NULL){
              for(i = 0;i < hight+1;i++){
              printf(" ");
              }
              //printf("/");
              printf_AST(Tree->left,Depth,for_depth+1,hight+1);
              }
              if(Tree->right != NULL){
              for(i = 0;i < hight+1;i++){
              printf(" ");
              }
              //printf("\\");
              printf_AST(Tree->right,Depth,for_depth-1,hight+1);
              }
              }
              int evaluate(SimpleASTNode_t* node,int tab_num){
              int result = 0;
              int value1;
              int value2;
              SimpleASTNode_t* child;
              SimpleASTNode_t* child1;
              SimpleASTNode_t* child2;
              switch(node->type){
              case CharDeclaration:
              child = node->left;
              result = evaluate(child,tab_num+1);
              break;
              case AdditiveExp:
              child1 = node->left;
              value1 = evaluate(child1,tab_num+1);
              child2 = node->right;
              value2 = evaluate(child2,tab_num+1);
              if(strcmp(node->text,"+") == 0){
              result = value1 + value2;
              }
              break;
              case MulticativeExp:
              child1 = node->left;
              value1 = evaluate(child1,tab_num+1);
              child2 = node->right;
              value2 = evaluate(child2,tab_num+1);
              if(strcmp(node->text,"*") == 0){
              result = value1 * value2;
              }
              break;
              case Ast_IntLiteral:
              result = atoi(node->text);
              break;
              default :
              break;
              }
              for(int i = 0; i < tab_num;i++){
              printf(" ");
              }
              printf("Result: %d\r\n",result);
              return result;
              }
              int syntactic_parser(){
              Save_Token_t* token;
              SimpleASTNode_t* child;
              SimpleASTNode_t* node;
              tokens_fun.peek = token_peek;
              tokens_fun.read = token_read;
              while(1){
              token = tokens_fun.peek();//预读
              if(token == NULL){
              return 0;
              }
              if((token != NULL) && ((token->type == CHAR)||(token->type == INT))){
              //if( (token.type == CHAR)){
              token = tokens_fun.read();//消耗掉char
              token = tokens_fun.peek();
              if(token->type == Id){
              token = tokens_fun.read();
              //创建当前节点,并把变量名记到AST节点的文本值中,
              node = new_SimpleASTNode_2(CharDeclaration,token->text);
              token = tokens_fun.peek();//预读
              if(token->type == Assignment){
              //add_AST(AssignmentExp,token.text);
              token = tokens_fun.read();
              child = additive(token); //匹配一个表达式
              evaluate(child,0);
              Node_add_child(node,child);
              }
              }
              }else{
              token = tokens_fun.read();//消耗掉
              }
              printf("\r\n=====syntactic_parser======\r\n\r\n");
              int Depth = get_Tree_Depth(node);
              printf_AST(node,Depth,0,0);
              }
              }
              int main(){
              Lexical_analyzer();
              syntactic_parser();
              }


              IP属地:北京7楼2022-08-17 10:32
              回复