實驗三 遞迴下降剖析器實驗

來源:互聯網
上載者:User

標籤:printf   strlen   輸入   .com   進一步   長度   exe   std   分析   

一、        實驗目的

     編製一個遞迴下降剖析器。

 

二、        實驗內容和要求

     輸入:正規式

輸出:判斷該正規式是否正確。

 

三、        實驗方法、步驟及結果測試

   1.    來源程式名:遞迴下降剖析器.c

可執行程式名:遞迴下降剖析器.exe

  2.

 

 

 3.主要程式段及其解釋:

#include<stdio.h>    #include<string> char str[50]; int index=0; void E();                //E->TX; void X();                //X->+TX | e void T();                //T->FY void Y();                //Y->*FY | e void F();                //F->(E) | i int main()                /*遞迴分析*/{     int len;     int m;     printf("請輸入要測試的次數:");     scanf("%d",&m);     while(m--)     {         printf("請輸入字串(長度<50>):\n");         scanf("%s",str);        len=strlen(str);        //str[len]=‘#‘;        str[len+1]=‘\0‘;         E();         printf("%s為合法符號串!\n",str);         strcpy(str,"");         index=0;    } return 0; } void E() {     T();    X();}void X() {     if(str[index]==‘+‘)    {     index++;         T();         X();     } } void T() {     F();     Y(); } void Y() {     if(str[index]==‘*‘) {         index++;         F();         Y();     } } void F() {     if(str[index]==‘i‘)    {    index++; }     else if (str[index]==‘(‘)     {         index++;         E();         if(str[index]==‘)‘)         {     index++; }else        {             printf("\n非法的符號串!\n");            exit (0);         }     }     else    {        printf("非法的符號串!\n");         exit(0);     } }

4.      運行結果及分析

 

 

四、        實驗總結

通過這次的實驗,對於遞迴又有了新的認識,對於工作原理也進一步的掌握了。本次實驗對我協助很大。

實驗三 遞迴下降剖析器實驗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.