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

📄 parse.c

📁 该程序能够实现简单的c语言词法
💻 C
📖 第 1 页 / 共 5 页
字号:
                     printf("push < into stack");
                   else if(lex_word[pointer-1]==small_equal)
                     printf("push <= into stack");
                   else if(lex_word[pointer-1]==great)
                     printf("push > into stack");
                   else if(lex_word[pointer-1]==great_equal)
                     printf("push >= into stack");
                   else if(lex_word[pointer-1]==equal_equal)
                     printf("push == into stack");
                   else
                     printf("push != into stack");
                   break;
            case 2:
                   printf("push ( into stack");
                   break;
            case 3:
                   printf("push ) into stack");
                   break;
            case 4:
                   printf("push ! into stack");
                   break;
            case 5:
                   printf("push && into stack");
                   break;
            case 6:
                   printf("push || into stack");
                   break;
            default:
                   break;
           }

       }
       getchar();
    }
       return B;
}
int  match(int a)
{
   if(lex_word[pointer] == a)
   {
       pointer++;
       return 1;
   }
   else
   {
     /*printf("symbol %d is not match %d",lex_word[pointer],a); */
     return 0;
   }
}

int evaluate_parse()/*赋值语句分析*/
{
    int p = 0;
    if(lex_word[pointer]>=1000)
    {
        p = lex_word[pointer]%1000;
        if(symbol[p].type == 0)
          printf("erro : %s is not define before use\n",symbol[p].variable);
        printf("push ID into stack\n");
        pointer++;
        if(match(equal))
        {
           printf("push = into stack\n");
           exp_analyze();
           if(iscreate ==1)
           {
              four_element[findex].operate = '=';
              four_element[findex].arg1 = temporary-1;
              four_element[findex].arg2 = -1;
              four_element[findex].result = p+1000;
              findex++;

              iscreate =0;
           }
           symbol[p].value = E_value;
           printf("the %s's value is %d\n",symbol[p].variable,E_value);
           if(match(fenhao))
           {
               printf("push ; into stack\n");
               printf("use expression: evaluate --> ID = E;\n");
               printf("%d",lex_word[pointer]);
               return evaluate;
           }
           else
           {
               printf("there is lack of ;\n");
               return evaluate;
           }
        }
        else
        {
           printf("the expression's equal_symbol is erro\n");
           return evaluate;
        }
    }
    else
    {
        printf("the expression is erro\n");
           return evaluate;
    }
}

int declare_parse()
{
    int type =0;/*用于记录声明的变量的类型*/
    if(match(symbol_int)||match(symbol_char)||match(symbol_float)||match(symbol_long)||match(symbol_double))
    {
      if(lex_word[pointer-1]==symbol_int)
         type = symbol_int;
      else if(lex_word[pointer-1]==symbol_char)
         type = symbol_char;
      else if(lex_word[pointer-1]==symbol_float)
         type = symbol_float;
      else if(lex_word[pointer-1]==symbol_long)
         type = symbol_long;
      else
         type = symbol_double;
      printf("push key into stack\n");
      if(lex_word[pointer]>=1000)
      {
         int p = lex_word[pointer]%1000;
         symbol[p].type = type; /*将声明的变量的类型记录到符号表中*/
         printf("push ID into stack\n");
         pointer++;
         if(match(equal))
         {
             printf("push = into stack\n");
             exp_analyze();
             if(iscreate ==1)
             {
                four_element[findex].operate = '=';
                four_element[findex].arg1 = temporary-1;
                four_element[findex].arg2 = -1;
                four_element[findex].result = p+1000;
                findex++;

                iscreate =0;
             }
             symbol[p].value = E_value;
             printf("the %s's value is %d\n",symbol[p].variable,symbol[p].value);
             if(match(fenhao))
               printf("push ; into stack\n");
             else
               printf("erro: declare sentence is lack of ;\n" );
               printf("use expression: declare -->key id = E;\n");
               return 1;

         }
         else if(match(fenhao))
         {
             printf("push ; into stack\n");
             printf("use expression: declare -->key id;\n");
             return 1;
         }
         else
             printf("erro: declare sentence  is lack of ;\n");
             return 1;
      }
      else
         printf("erro: the declare sentence is wrong\n" );
    }
    else
         printf("erro: the declare sentence's key is wrong\n" );

}

void body()
{
   while(lex_word[pointer]!= rightbig&&lex_word[pointer]!= -1)
   {
      if(lex_word[pointer]==symbol_int||lex_word[pointer]==symbol_float||lex_word[pointer]==symbol_char||lex_word[pointer]==symbol_long||lex_word[pointer]==symbol_double)
      {
         declare_parse();
         body();
      }
      else if(lex_word[pointer]==symbol_while)
      {
         while_parse();
         body();
      }
      else if(lex_word[pointer]>=1000)
      {
         if(lex_word[pointer+1]==equal)
         {
            evaluate_parse();
            body();
         }
         else if(lex_word[pointer+1]==leftpar)
         {
            call();
            body();
         }
      }
      else if(lex_word[pointer]==symbol_if)
      {
         if_parse();
         body();
      }
      else
      {
         printf("erro: The define structure is wrong\n");
         break;
      }

   }
}


int if_parse() /*分析IF-ELSE结构*/
{
    if(match(symbol_if))
    {
        printf("push if into stack\n");
        if(match(leftpar))
        {
           printf("push ( into stack\n");
           bool_parse();
           if(match(rightpar))
           {
               printf("push ) into stack\n");
               printf("use expression: if_head-->if(B)\n");
               if(match(leftbig))
               {
                  printf("push { into stack\n");
                  body();
                  if(match(rightbig))
                  {
                      printf("push } into stack\n");
                      printf("use expression: if_structure-->{list}\n");
                      printf("use expression: if_sentence-->if_head if_structure\n");

                  }
                  else
                     printf("erro: the if_structure is lack of }\n");
                  if(match(symbol_else))
                  {
                      printf("push else into stack\n");
                      if(match(leftbig))
                      printf("push { into stack\n");
                      else
                      printf("erro: the else_structure is lack of {\n");
                      body();
                      if(match(rightbig))
                      {
                          printf("push } into stack\n");
                          printf("use expression: if_structure-->{list}\n");
                          printf("use expression: else_structure-->{list}\n");
                          printf("use expression: if_sentence-->if_head if_structure\n");
                          printf("use expression: else_sentence-->else_structure\n");
                          printf("use expression: if_else_sentence-->if_sentence else_sentence\n");
                          return 1;

                      }
                      else
                          printf("erro: the if_structure is lack of }\n");

                  }
                  else
                  {
                      printf("use expression: if_else_sentence-->if_sentence\n");
                      return 1;
                  }
               }
           }
           else
           {
               printf("erro: there is a lack of )\n");
               return 1;
           }
        }
        else
        {
           printf("erro: the if_head expression is erro\n");
           return 1;
        }

    }
}
int while_parse()    /*分析while结构*/
{
   if(match(symbol_while))
   {
       printf("push while into stack\n");
       if(match(leftpar))
       {
          printf("push ( into stack\n");
          bool_parse();
          if(match(rightpar))
          {
             printf("push ) into stack\n");
             printf("use expression: while_head-->while(B)\n");
             if(match(leftbig))
             {
                printf("push { into stack\n");
                body();
                if(match(rightbig))
                {
                   printf("push } into stack\n");
                   printf("use expression: while_structure-->{list}\n");
                   printf("use expression: while_sentence-->while_head while_structure\n");
                   return 1;
                }
                else
                   printf("erro: the while_head is lack of }\n");
             }
             else
                printf("erro: the while_head is lack of {\n");
          }
          else
             printf("erro:  the while_head is lack of )\n");
       }
       else
          printf("erro:  the while_head is lack of (\n");
   }
   return 1;
}

int call()/*分析方法调用语句*/
{

     if(lex_word[pointer]>=1000)
    {
        int p = lex_word[pointer] %1000;
        if(symbol[p].type ==0)
        printf("erro: fuction_name %s is not define\n",symbol[p].variable);
        printf("push function_name into stack\n");
        pointer++;
        if(match(leftpar))
        {
           printf("push ( into stack\n");
           if(lex_word[pointer]!=rightpar)
           {
              while(lex_word[pointer]!=rightpar&&lex_word[pointer]!=fenhao&&lex_word[pointer]!=-1)
              {
                 pointer++;

              }
              printf("push paramer_list into stack\n");
              if(!match(rightpar))
              {
                printf("erro: call sentence is lack of )\n");
              }
              else if(!match(fenhao))
                   printf("erro: call sentence is lack of ;\n");
              else
                 printf("push ) into stack\n");
                 printf("push ; into stack\n");
              printf("use the expression: call_sentence-->fuction_name(list); \n");
              return 1;
           }
           else if(match(rightpar))

⌨️ 快捷键说明

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