编译原理算符优先矩阵的构造

算符优先法中算符优先矩阵的构造需要求出firstTerm以及lastTerm,由于这个是在预测分析法上进行修改的,结构体的定义并未进行修改,其中包含该非终结符a以及其对应的first(即为firstTerm)和follow(即为lastTerm),输入格式如下(以#结束输入)
Z::=E
E::=T|E+T
T::=F|T*F
F::=(E)|i
#

#include
using namespace std;
//判断终结符和非终结符 返回1是非终结符 返回0是终结符 
int Norterminal(char c)
{
    if(c>='A'&&c<='Z') 
        return 1;
    else if(c!=':'&&c!='='&&c!='<'&&c!='>'&&c!=' '&&c!='|') 
        return 0; 

}  
struct GRAM
{
    //$代表空 
    string  a;
    string first;
    string follow;
};
struct LIST
{
    int num;
    string s;
};
int main()
{
    GRAM gram[50]; 
    string grammer; 
    cout<<"请输入文法(以#结束)"<std::vector<string> strN;
    cin>>grammer;//输入规则 
    strN.push_back(grammer); 
    char str[10][80];  
    const char *ch = "|";  
    char *result;  
    vector<string> strN1;  //存处理过“|”规则的 

    for(int i=0;i0][i]=grammer[i];
    } 
    for(int h=grammer.length();h<80;h++)
    {
            str[0][h]=NULL;
    }
    result = strtok(str[0],ch);  
    while(result!=NULL)  
    {  
        strN1.push_back(result);  
        result = strtok(NULL,ch);  
    }  
    for(int i=1;grammer!="#";i++)
    {
        cin>>grammer;
        strN.push_back(grammer); 
        /*处理转换的规则形式输入部分*/
        for(int h=0;hfor(int h=grammer.length();h<80;h++)
        {
            str[i][h]=NULL;
        }
        result = strtok(str[i],ch);  
        while(result!=NULL)  
        {  
            strN1.push_back(result);  
            result = strtok(NULL,ch);  
        }  
    } 
    /*
    获取firstTerm集合 
    */
    //直接获取firstTerm
    for(unsigned i=0;i0];
        gram[i].first="";
        if(!Norterminal(strN[i][4]))
        {
            gram[i].first=strN[i][4];
        }
        else
        {
            if(!Norterminal(strN[i][5]))
            {
                gram[i].first=strN[i][5];
            }
        }
        for(unsigned k=5;kif(strN[i][k]=='|')
            {

                if(!Norterminal(strN[i][k+1]))
                {

                    gram[i].first+=strN[i][k+1];
                }
                else
                {
                    if(!Norterminal(strN[i][k+2]))
                    {
                        gram[i].first+=strN[i][k+2];
                    }
                }
            }
        } 

    } 
    int num=strN1.size();
    //当第一个字符是非终结符时 
    for(int i=num-1;i>=0;i--)
    {
        for(unsigned l=0;l1;l++)
        {
            if(gram[l].a[0]==strN1[i][0])
            {
                    for(unsigned h=0;h1;h++)
                    {
                        if(strN1[i][4]==gram[h].a[0]&&strN1[i][4]!=strN1[i][0])
                        {

                            gram[l].first+=gram[h].first; 
                            string ustr(gram[l].first);
                            sort(ustr.begin(), ustr.end());
                            ustr.erase(unique(ustr.begin(), ustr.end()), ustr.end() );
                            gram[l].first=ustr; 
                            break;
                        }       
                    }
            }
        }        

    }
    cout<<"firstTerm集合为:"<for(unsigned i=0;i1;i++)
    {
        for(int k=0;kif(gram[i].first[k]==NULL)
            {
                gram[i].first.erase(gram[i].first.begin()+k);
            }
        }
        cout<" "<string term="";
    cout<<"终结符为:"<for(unsigned i=0;i1;i++)
    {
        for(int j=0;jif(!Norterminal(strN[i][j])&&strN[i][j]!='$')
               term+=strN[i][j];
        }
        string ustr(term);
        sort(ustr.begin(), ustr.end());
        ustr.erase(unique(ustr.begin(), ustr.end()), ustr.end() );
        term=ustr;
    }
    cout</*转换规则形式*/
    cout<<"转换后的规则形式为:"<int listnumber =0; 
    for(unsigned i=0;i1&&i-1>=0;i++)
    {

        if(strN1[i].find("::")==-1)
        {
            string add="";
            add=add+strN1[i-1][0]+"::="+strN1[i];
            strN1[i]=add;

        }   
        cout</*获取lastTerm*/
    //直接获取lastTerm
    for(int i=0;i1;i++)
    {
        for(unsigned l=0;l1;l++)
        {
                if(gram[l].a[0]==strN1[i][0])
                {
                        for(unsigned h=0;h1;h++)
                        {
                            int k=strN1[i].length();
                            if(!Norterminal(strN1[i][k-1]))
                            {

                                gram[l].follow+=strN1[i][k-1]; 
                                string ustr(gram[l].follow);
                                sort(ustr.begin(), ustr.end());
                                ustr.erase(unique(ustr.begin(), ustr.end()), ustr.end() );
                                gram[l].follow=ustr;    
                                break;
                            }
                            else
                            {
                                if(!Norterminal(strN1[i][k-2]))
                                {
                                    gram[l].follow+=strN1[i][k-2]; 
                                    string ustr(gram[l].follow);
                                    sort(ustr.begin(), ustr.end());
                                    ustr.erase(unique(ustr.begin(), ustr.end()), ustr.end() );
                                    gram[l].follow=ustr;    
                                    break;
                                }
                            }       
                        }
                }
        }        
   }
   for(int i=num-1;i>=0;i--)
   {
        for(unsigned l=0;l1;l++)
        {
            if(gram[l].a[0]==strN1[i][0])
            {
                    for(unsigned h=0;h1;h++)
                    {
                            int k=strN1[i].length();
                            if(Norterminal(strN1[i][k-1]))
                            {

                                 for(unsigned h1=0;h11;h1++)
                                 if(gram[h1].a[0]==strN1[i][k-1])
                                 {
                                        gram[l].follow+=gram[h1].follow; 
                                        string ustr(gram[l].follow);
                                        sort(ustr.begin(), ustr.end());
                                        ustr.erase(unique(ustr.begin(), ustr.end()), ustr.end() );
                                        gram[l].follow=ustr;    
                                        break;
                                 }

                            }
                    }   
            }
        }        

    }
    cout<<"lastTerm集合为:"<for(unsigned i=0;i1;i++)
    {
        cout<" "<int list[term.length()][term.length()]; 
    for(int i=0;ifor(int j=0;jlist[i][j]=0;
        }
    }
    cout<<"相等的关系为:"<for(int i=num-1;i>=0;i--)
   {
        for(int k=0;k1if(Norterminal(strN1[i][k]))
            {
                if(!Norterminal(strN1[i][k-1])&&!Norterminal(strN1[i][k+1]))
                {
                    cout<1]<1]<for(int h=0;hif(strN1[i][k-1]==term[h])
                        for(int g=0;gif(strN1[i][k+1]==term[g])
                            {
                                list[h][g]=1;
                            }
                        }
                    }
                }
            }
            else
            {
                if(!Norterminal(strN1[i][k+1]))
                {
                    cout<1]<for(int h=0;hif(strN1[i][k]==term[h])
                        for(int g=0;gif(strN1[i][k+1]==term[g])
                            {
                                list[h][g]=1;
                            }
                        }
                    }
                }
            }
        }   
   } 
   //cout<<"小于关系为:"<
   for(int i=0;i1;i++)
   {
        for(int k=0;kif(!Norterminal(strN1[i][k]))
            {
                if(Norterminal(strN1[i][k+1]))
                {
                    for(int h=0;hif(strN1[i][k]==term[h])
                        {
                            for(int j=0;j1;j++)
                            {
                                if(strN1[i][k+1]==gram[j].a[0])
                                {
                                    for(int g=0;gfor(int f=0;fif(gram[j].first[f]==term[g])
                                        {
                                            list[h][g]=2;
                                        }
                                    }
                                }
                            }
                        }
                    }

                }
            }
        }


   } 
   //大于关系    
   for(int i=0;i1;i++)
   {
        for(int k=0;kif(Norterminal(strN1[i][k]))
            {
                if(!Norterminal(strN1[i][k+1]))
                {
                    for(int h=0;hif(strN1[i][k+1]==term[h])
                            for(int j=0;j1;j++)
                            {
                                if(strN1[i][k]==gram[j].a[0])
                                {
                                    for(int g=0;gfor(int f=0;fif(gram[j].follow[f]==term[g])
                                        {
                                            list[g][h]=3;
                                        }
                                    }
                                }
                            }
                    }

                }
            }
        }
   } 
   for(int i=0;ifor(int j=0;jcout<<list[i][j]<<" ";
        }
        cout<cout<<"算符优先矩阵为:"<cout<<"    |";
    for(int i=0;icout<10)<cout<<"|";
    }
    cout<cout<<"____________________________________________________________________________"<for(int i=0;iprintf("%2c",term[i]);
        cout<<"  "<<"|";
        for(int j=0;jif(list[i][j]==0)
            cout<10)<<" "<<"|";
            if(list[i][j]==1)
            cout<10)<<"="<<"|";
            if(list[i][j]==2)
            cout<10)<<"<"<<"|";
            if(list[i][j]==3)
            cout<10)<<">"<<"|";

        }
        cout<cout<<"____________________________________________________________________________"<

运行结果如下:
编译原理算符优先矩阵的构造_第1张图片

你可能感兴趣的:(编译原理)