⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.c

📁 该程序能够实现简单的c语言词法
💻 C
📖 第 1 页 / 共 5 页
字号:
           {
               printf("push ( into stack\n");
              if(!match(fenhao))
                 printf("erro: call sentence is lack of ;\n");
              else
                 printf("push ; into stack\n");
              printf("use the expression: call_sentence-->fuction_name();\n ");
              return 1;
           }

        }

        else
        {
           printf("erro: the call_sentence is lack of (\n");
           return 1;
        }
    }
    else
    {
        printf("erro: the Function_name  is not exit\n");
           return 1;
    }
}
int method_body()/*分析"方法定义函数"--method方法的核心部分*/
{
     if(match(leftpar))
          {
            printf("push ( into stack\n");
            if(lex_word[pointer]!=rightpar)
            {
              while(lex_word[pointer]!=rightpar&&lex_word[pointer]!=-1)
              {
                 pointer++;

              }
              printf("push paramer_list into stack\n");
              if(!match(rightpar))
              {
                printf("erro: method_head is lack of )\n");
              }
              else
                 printf("push ) into stack\n");
                 printf("push ; into stack\n");
              printf("use the expression: method_head-->fuction_name(parameter_list); \n");
            }
            else if(match(rightpar))
            {
               printf("push ) into stack\n");
               printf("use the expression: method_head-->fuction_name();\n ");
            }
            if(match(leftbig))
            {
                printf("push { into stack\n");
                body();
                if(match(rightbig))
                   printf("push } into stack\n");
                else
                   printf("the method_structure is lack of }\n");
                 printf("use the expression: method_body-->{list}\n ");
                 printf("use the expression: method-->method_head method_body\n ");

            }
            else
               printf("erro: The method_structure is lack of {\n");

          }
          else
               printf("erro: the method_head is lack of (\n");
}
int method()
{
    if(match(symbol_int)||match(symbol_float)||match(symbol_void))
    {
       printf("push int  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_int;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else if(match(symbol_char))
    {
       printf("push char  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_char;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else if(match(symbol_float))
    {
        printf("push char  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_float;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else if(match(symbol_double))
    {
        printf("push char  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_double;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else if(match(symbol_long))
    {
        printf("push char  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_long;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else if(match(symbol_void))
    {
        printf("push char  into stack\n");
       if(lex_word[pointer]>=1000)
       {
          int symbol_index = lex_word[pointer]%1000;
          printf("push function_name into stack\n");
          symbol[symbol_index].type = symbol_void;
          pointer++;
          method_body();
       }
       else
          printf("erro: the function_head is wrong\n");
    }
    else
       printf("erro: The method's key_type is not exit\n");
     return 1;
}

int  main_method()
{
     int i = pointer;
     if(i !=0)
     {
         printf("use the expression: external-->define_declare_list\n ");
     }
     printf("push main into stack\n");
     pointer++;
     if(match(leftpar))
     {
        printf("push ( into stack\n");
        if(match(rightpar))
        {
           printf("push ) into stack\n");
           printf("use the expression: main_method_head-->fuction_name();\n ");
        }
        else
           printf("erro: the main_method_head is lack of )\n ");
        if(match(leftbig))
        {
          printf("push { into stack\n");
          body();
          if(match(rightbig))
            printf("push } into stack\n");
          else
            printf("erro: the method_structure is lack of }\n");
            printf("use the expression: main_method_body-->{list}\n ");
            printf("use the expression: main_method-->method_head method_body\n ");
          if(i ==0)
          {
            printf("use the expression: programme-->main_method\n ");
          }
          else
          {
            printf("use the expression: programme-->external main_method\n ");
          }


        }
        else
          printf("erro: the main_method's structure  is lack of {\n");

    }
    else
          printf("erro: the main_method's head is wrong\n");

}

void  all_function()
{
    while(lex_word[pointer]!= -1)
   {
      if(lex_word[pointer]==symbol_int||lex_word[pointer]==symbol_float||lex_word[pointer]==symbol_char||lex_word[pointer]==symbol_void||lex_word[pointer]==symbol_long||lex_word[pointer]==symbol_double)
      {
         if(lex_word[pointer+2]==leftpar)
         {
             method();
             all_function();
         }
         else
         {
             declare_parse();
             all_function();
         }
      }
      else if(lex_word[pointer]>=1000)
      {
         main_method();
         all_function();
      }
      else
      {
         printf("erro: there is a not right define in the programe\n");
         break;
      }

   }
}
void print_symbol()
{
   int i;
   fprintf(chart,"index\t标示符\tvalue\ttype\n");
   for(i =0;i<index;i++)
   {
      fprintf(chart,"%d\t%s\t%d\t",i,symbol[i].variable,symbol[i].value);
      switch(symbol[i].type)
      {
         case 24:
              fprintf(chart,"int\n");
              break;
         case 25:
              fprintf(chart,"float\n");
              break;
         case 26:
              fprintf(chart,"char\n");
              break;
         case 36:
              fprintf(chart,"void\n");
              break;
         case 37:
              printf(chart,"long\n");
              break;
         case 38:
              fprintf(chart,"double\n");
              break;
         default :
              fprintf(chart,"void\n");

      }

   }
}
void print_four_element()
{
    int i;
    printf("operator\tnumber1\tnumber2\tresult\n");
    for(i=0;i<findex;i++)
    {
       printf("%c\t\t",four_element[i].operate);
       if( four_element[i].arg1>=1000)
          printf("%s\t",symbol[four_element[i].arg1%1000].variable);
       else
          printf("T%d\t",four_element[i].arg1);
       if( four_element[i].arg2>=1000)
          printf("%s\t",symbol[four_element[i].arg2%1000].variable);
       else if( four_element[i].arg2>=0)
          printf("T%d\t",four_element[i].arg2);
       else
          printf("_\t");
       if( four_element[i].result>=1000)
          printf("%s\n",symbol[four_element[i].result%1000].variable);
       else
          printf("T%d\n",four_element[i].result);


    }

}
void main()
{
    init();/*初始化关键字符号表*/
    symbol=(struct symbolize *)calloc(init_size,sizeof(struct symbolize));
    if(symbol==NULL)
    {
        printf("malloc memery error,exit\n");
        exit(1);
    }
    symbol_size=init_size;
    if((fp = fopen("test.txt","r")) == NULL)/*以只读方式打开测试文件*/
    {
        printf("cannot open the file\n");
    }
    if((output = fopen("file\\out.txt","w+")) == NULL)/*以可写方式打开输出文件*/

        {
                printf("cannot open the file\n");
        }
    if((erro = fopen("file\\erro.txt","w+")) == NULL)/*以可写方式打开错误文件 */
        {
                printf("cannot open the file\n");
        }
    if((chart = fopen("file\\symbol.txt","w")) == NULL)/*以可写方式打开符号表文件*/
        {
                printf("cannot open the file\n");
        }
    fprintf(chart,"index\tid\t\t\tvalue\n");
    while( ! feof(fp))
    {
       lex_word[lex_index] = lex();
     /*printf("%d",lex_word[lex_index]);*/
       lex_index++;
    }
    fclose(fp);/*关闭打开的4个文件的指针  */
    printf("press enter to beginning grammar analys\n");
    getchar();
    all_function();
    print_symbol();
    printf("complete parse analyze,please press enter to show the four_elemnt_model\n");
    getchar();
    printf("*********************four_elemnt_model******************************\n\n");
    print_four_element();
    fclose(chart);
    fclose(output);
    fclose(erro);
    getchar();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -