现在的位置: 首页 > 综合 > 正文

简单的递归下降分析

2012年11月14日 ⁄ 综合 ⁄ 共 4081字 ⁄ 字号 评论关闭

 

 

递归下降分析

程序输入输出示例:

(1)     E→TG               
(
2)     G→+TG|-TG|ε       
(
3)     T→FS               
(
4)     S →*FS|/FS|ε     
(
5)     F→(E)|i           

 

输出的格式如下:

(1)输出一个以#结束的符号串(包括+ - * / i #):

(2)输出结果:i+i*i#为合法符号串

备注:输入一符号串如i+i*#,要求输出为"非法的符号串"

 

 

////////////////////////////////////
//                                  //
//                                //
//            E→TG               //
//            G→+TG|-TG|ε       //
//            T→FS               //
//            S→*FS|/FS|ε       //
//            F→(E)|i            //
//                                //
////////////////////////////////////

#include 
<iostream>
using namespace std;

char A[100];                                //用于存放符号串,不能多于100个字符
int i=0;                                    //记录当前要判断的字符位置
void E();                                    //E()
void G();                                    //G()
void T();                                    //为每个非终节符构造一个子程序
void S();                                    //S()
void F();                                    //F()
void match(int &k);                            //此程序只是此执行加1操作,为了取到后一个终结符
void main( )
{
    
    cout
<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;
    cout
<<"+ 递归下降分析 +"<<endl;
    cout
<<"+ 作者:杨浩 +"<<endl;
    cout
<<"+ 日期:2008年4月 +"<<endl;
    cout
<<"+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"<<endl;

    cout
<<"请输入一个有待判断的符号串(以#结尾): ";
    cout
<<"输入的符号串: ";
    cin
>>A;
    cout
<<"输出的符号串: "<<A<<endl<<endl;
    cout
<<"匹配的结果: ";

    E();
    
if(A[i]!='#')
    
{
        cout
<<"此输入串不合法"<<endl;
        cout
<<"不合法的原因: 不匹配开始于在第"<<i+1<<"个终结符处,没有发现与"<<A[i]<<"相匹配的文法"<<endl;
    }

}

void E()                                                        //E->TG
{
    T();
    G();
}

void G()                                                        //G->+TG|-TG|ε
{
 
// 这里分了三个情况,“+”“-”以及“ε”三种情况。
 
// 如果是前两种,则很好,本步骤匹配 。如果没有匹配,则默认取到“ε”
    if(A[i]=='+')                                                //匹配“+”
    {
        match(i);                                    
        T();
        G();
    }

    
else if(A[i]=='-')                                            //匹配“-”
    {
        match(i);
        T();
        G();
    }

    
else return;                                                //没有与之匹配,则默认为空
}

void T()                                                        //T->FS
{
    F();
    S();
}

void S()                                                        //S->*FS|/FS|ε
{
 
// 这里分了三个情况,“*”“/”以及“ε”三种情况。
 
// 如果是前两种,则很好,本步骤匹配 。如果没有匹配,则默认取到“ε”
 
// 情况雷同G()
    if(A[i]=='*')                                
    
{
        match(i);
        F();
        S();
    }

    
else if(A[i]=='/')
    
{
        match(i);
        F();
        S();
    }

    
else return;
}

void F()                                                        //F->(E)|i
{
    
if(A[i]=='(')
    
{
        match(i);
        
if(A[i]=='#')                                            //如果是“#”则说明已经搜索匹配完毕了,肯定合法
        {    
            cout
<<"此输入串不合法"<<endl;
            cout
<<"不合法的原因: 没有与'('相配对的')'"<<endl;
            exit(
0);            
        }

        E();
        
if(A[i]==')')
        
{    
            match(i);
            
if(A[i]=='#')                                        //如果是“#”则说明已经搜索匹配完毕了,肯定合法
            {    
                
if(A[i-2]=='i')
                
{
                    cout
<<" 此输入串合法 ";
                    exit(
0);
                }

                
else 
                
{
                    cout
<<"此输入串不合法"<<endl;
                    cout
<<"不合法的原因: 前面没有与'('相配对的'i'"<<endl;
                    exit(
0);
                }

            }

        }


    }

    
else if(A[i]=='i')
    
{
        match(i);                                                
//匹配后继续判断有没有到结束
        if(A[i]=='#')                                            //如果是“#”则说明已经搜索匹配完毕了,肯定合法
        {
            
if(A[i-2]=='(')
            
{    cout<<"此输入串不合法"<<endl;
                cout
<<"不合法的原因: 没有与'('相配对的')'"<<endl;
                exit(
0);
            }

            
else 
            
{
                cout
<<" 此输入串合法 ";
                exit(
0);                
            }

        }

        
return ;
    }


    
else if (A[i]=='#')
    
{

抱歉!评论已关闭.