示例#1
0
void Parser::view_stmt(string id) {
    if (tokens[look].type == SELECT) {
        select_stmt(id);
    } else {
        extract_stmt(id);
    }
}
void extract_individual_stmts(abstractSyntaxTree ast, symbol_table_ptr st,int scope)
{
  abstractSyntaxTree temp;
  while(ast->symbol != TK_EPSILON)
  {
    temp = ast->child[0];
    extract_stmt(temp,st,scope);
    ast = ast->child[1];
  }
}
void extract_stmt(abstractSyntaxTree temp,symbol_table_ptr st, int scope )
{
  identifier_list id;

  char name[10];
  char temp_name[10];

    //printf("Stmt is : %s\n" ,tokenName[temp->symbol]);

    if(temp->symbol == TK_ASSIGNOP)
    {
      //print_tree(temp);
      //printf("Sup\n");
      //printf("%d\n",temp->childnum);

      if(temp->child[0]->child[0]->symbol == TK_EPSILON)
      {
        //print_tree(temp->child[1]);
        //printf("Child1 : %s\n",tokenName[temp->child[1]->symbol]);
        //printf("Searching for %s\n",temp->child[0]->value);
        id = get_symtable_ptr(" ",temp->child[0]->value, st, scope);
        //printf("Sym  value : %s\n",id->id_name);
        //printf("HI\n");
        create_quadruple(temp->symbol,eval_arithmetic(temp->child[1],st,scope),NULL,id);
      }
      else
      {
        //printf("Searching for %s.%s\n",temp->child[0]->value ,temp->child[0]->child[0]->value);
        id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope);
        //printf("Sym table value : %s.%s",id->record_name,id->id_name);

        create_quadruple(temp->symbol,eval_arithmetic(temp->child[1],st,scope),NULL,id);
      }
    }
    //printf("Hi\n");*/

    if(temp->symbol == TK_READ)
    {

      if(temp->child[0]->child[0]->symbol == TK_EPSILON)
      {
        id = get_symtable_ptr(" ",temp->child[0]->value, st, scope);
        //printf("Sym  value : %s\n",id->id_name);
        create_quadruple(temp->symbol,id,NULL,NULL);
      }
      else
      {
        //printf("The record is : %s\n",temp->child[0]->value);
        //printf("The id is : %s\n",temp->child[0]->child[0]->value);
        id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope);
        //printf("Sym table value : %s",id->record_name);

        create_quadruple(temp->symbol,id,NULL,NULL);
      }
    }
    else if(temp->symbol == TK_WRITE)
    {

        if(temp->child[0]->symbol == TK_NUM || temp->child[0]->symbol == TK_RNUM )
        {
          //printf("Current expr is: %s\n",temp->value);
          sprintf(name , "%d", num_no);
          strcpy(temp_name,"num");
          strcat(temp_name,name);
          id = create_identifier(3,temp->child[0]->value,temp_name,scope);
          num_no ++;

          create_quadruple(temp->symbol,id,NULL,NULL);
        }


      else if(temp->child[0]->child[0]->symbol == TK_EPSILON)
      {
        id = get_symtable_ptr(" ",temp->child[0]->value, st, scope);
        //printf("Sym  value : %s\n",id->id_name);
        create_quadruple(temp->symbol,id,NULL,NULL);
      }
      else
      {
        //printf("The record is : %s\n",temp->child[0]->value);
        //printf("The id is : %s\n",temp->child[0]->child[0]->value);
        id = get_symtable_ptr(temp->child[0]->value,temp->child[0]->child[0]->value, st, scope);
        //printf("Sym table value : %s",id->record_name);
          create_quadruple(temp->symbol,id,NULL,NULL);
      }
    }

    else if(temp->symbol == TK_IF)
    {
      int jump1,jump2;
      //print_tree(temp->child[0]);
      id = eval_boolean(temp->child[0],st,scope);
      jump1 = quad_size;
      create_quadruple(temp->symbol,id,NULL,NULL);
      extract_stmt(temp->child[1],st,scope);
      extract_individual_stmts(temp->child[2],st,scope);

      if(temp->child[3]->symbol == TK_ENDIF)
      {
        sprintf(name , "%d", quad_size);
        quad[jump1].result = create_copy(quad[jump1].result,create_identifier(0," ",name,scope));
      }
      else if(temp->child[3]->symbol == TK_ELSE)
      {
        jump2 = quad_size;
        extract_stmt(temp->child[3]->child[0],st,scope);
        extract_individual_stmts(temp->child[3]->child[1],st,scope);
        sprintf(name , "%d", quad_size);
        quad[jump1].result = create_copy(quad[jump1].result,create_identifier(0," ",name,scope));
        sprintf(name , "%d", jump2);
        quad[jump1].arg2 = create_copy(quad[jump1].arg2,create_identifier(0," ",name,scope));
      }
    }
    else if(temp->symbol == TK_WHILE)
    {
      int jump;
      create_quadruple(21,NULL,NULL,NULL);
      id = eval_boolean(temp->child[0],st,scope);

      create_quadruple(temp->symbol,id,NULL,NULL);
      jump = quad_size-1;
      extract_stmt(temp->child[1],st,scope);
      extract_individual_stmts(temp->child[2],st,scope);
      sprintf(name , "%d", quad_size);
      quad[jump].result = create_copy(quad[jump].result,create_identifier(0," ",name,scope));
    }
    //printf("Bye");

}