zoukankan      html  css  js  c++  java
  • 12.30递归下降分析

      1 #include<stdio.h>
      2 #include<string.h>  
      3 char str[10];
      4 int index=0;
      5 void E();            //E->TX;
      6 void X();            //X->+TX|-TX| e
      7 void T();            //T->FY
      8 void Y();            //Y->*FY |/fy| e
      9 void F();            //F->(E) | id
     10 int id();           //id
     11 int main()
     12 {
     13     int len;
     14     int m;
     15 
     16         printf("请输入算数表达式:");
     17         scanf("%s",str);
     18         len=strlen(str);
     19         str[len]='#';
     20         str[len+1]='';
     21         E();
     22         printf("正确语句!
    ");
     23         strcpy(str,"");
     24         index=0;
     25     
     26     return 0;
     27 }
     28 void E()
     29 {
     30     T();
     31     X();
     32 }
     33 void X()
     34 {
     35     if(str[index]=='+')
     36     {
     37         index++;
     38         T();
     39         X();
     40     } 
     41     else if(str[index]=='-')
     42     {
     43         index++;
     44         T();
     45         X();
     46     } 
     47 }
     48 void T()
     49 {
     50     F();
     51     Y();
     52 }
     53 void Y()
     54 {
     55     if(str[index]=='*')
     56     {
     57         index++;
     58         F();
     59         Y();
     60     }
     61     else if(str[index]=='/')
     62     {
     63         index++;
     64         F();
     65         Y();
     66     }
     67 }
     68 void F()
     69 {
     70     if(id())
     71     {
     72         index++;
     73     }
     74     else if (str[index]=='(')
     75     {     
     76         index++;
     77         E();
     78         if(str[index]==')')
     79         {
     80             index++; 
     81         }else{
     82             printf("
    分析失败!
    ");
     83             exit (0);
     84         }
     85     } 
     86     else{
     87         printf("分析失败!
    "); 
     88         exit(0);
     89     }
     90  }
     91 int id()
     92 {
     93      if(str[index]>='0'&&str[index]<='9')
     94     {
     95         while( str[index+1]>='0'&&str[index+1]<='9' )
     96         {
     97             index++;
     98         }
     99         if(str[index+1]>='a'&&str[index+1]<='z' )
    100             return 0;
    101 
    102         return 1;
    103     }
    104      else if(str[index]>='a'&&str[index]<='z' )
    105      {
    106          return 1;
    107      }
    108      else 
    109          return 0;
    110      
    111 }
  • 相关阅读:
    替换configparser 中指定的值
    configparser 读取与赋值
    接口测试第五步 --》 拼接url
    接口测试第四步 --》 封装excel
    接口测试第三步 --》 封装请求方法
    接口测试第二步 --》 推荐定义一个log 文件
    超级直观理解tcp握手
    crash 潜水
    提高MySQL查询速度
    JSON文件存入MySQL数据库
  • 原文地址:https://www.cnblogs.com/zzy999/p/5092824.html
Copyright © 2011-2022 走看看