您的位置:首页 > 编程语言 > C语言/C++

编译原理LR分析法c++实现

2013-02-22 10:49 615 查看


#include <iostream>

#include<cstdlib>

#include<string>

#include<cstring>

using namespace std;

const int size=100;

class stack{

  int zt_data[size];char fh_data[size];

  int top;

public:

  stack();

  void push_stack(int zt,char fh);

  void showstack();

  void pop_stack();

  char get_fh(){return fh_data[top];}

  int get_zt(){return zt_data[top];}

};

stack::stack(){zt_data[0]=0;fh_data[0]='#';

               top=0;

               }

void stack::push_stack(int zt,char fh){

             top++;

             zt_data[top]=zt;  fh_data[top]=fh;

             cout<<"push_stack:"<<zt<<'\t'<<fh<<endl;

             }

void stack::pop_stack(){

          cout<<"pop_stack:"<<zt_data[top]<<'\t'<<fh_data[top]<<endl;

          zt_data[top]=0;fh_data[top]=NULL;

          top--;

          }

          int main()

          {

              string input;

    while(1){

        cout<<"please input the string end with '#' "<<endl;

        cin>>input;

        if(input[input.size()-1]=='#') break;

        cout<<"you put worry string";

     }

     stack lr;

     string tab[12][9];

     tab[0][0]="s5";tab[0][3]="s4";tab[0][6]="1";tab[0][7]="2";tab[0][8]="3";

     tab[1][1]="s6";tab[1][5]="acc";

     tab[2][1]="r2";tab[2][2]="s7";tab[2][4]="r2";tab[2][5]="r2";

     tab[3][1]="r4";tab[3][2]="r4";tab[3][4]="r4";tab[3][5]="r4";

     tab[4][0]="s5";tab[4][3]="s4";tab[4][6]="8";tab[4][7]="2";tab[4][8]="3";

     tab[5][1]="r6";tab[5][2]="r6";tab[5][4]="r6";tab[5][5]="r6";

     tab[6][0]="s5";tab[6][3]="s4";tab[6][7]="9";tab[6][8]="3";

     tab[7][0]="s5";tab[7][3]="s4";tab[7][8]="10";

     tab[8][1]="s6";tab[8][4]="s11";

     tab[9][1]="r1";tab[9][2]="s7";tab[9][4]="r1";tab[9][5]="r1";

     tab[10][1]="r3";tab[10][2]="r3";tab[10][4]="r3";tab[10][5]="r3";

     tab[11][1]="r5";tab[11][2]="r5";tab[11][4]="r5";tab[11][5]="r5";

     cout<<"showthetab:"<<endl;

     for(int i=0;i<12;i++)

     {

         for(int j=0;j<9;j++)

         cout<<tab[i][j]<<"\t";

         cout<<endl;

     }

     string unend_char[6]={"E+T","T","T*F","F","(E)","i"};

     char unend_char_l[6]={'E','E','T','T','F','F'};

        char*p=&input[0];

    while(!(('E'==lr.get_fh())&&(*p=='#')))

     {

        int n;

        switch(*p){

        case 'i':n=0;break;

        case '+':n=1;break;

        case '*':n=2;break;

        case '(':n=3;break;

        case ')':n=4;break;

        case '#':n=5;break;

        case 'E':n=6;break;

        case 'T':n=7;break;

        case 'F':n=8;break;

        }

        int m=lr.get_zt();

        string temp=tab[m]
;

        if(temp[0]=='s'){

           int   zt;

         if(temp=="s0")

           zt=0;

         if(temp=="s1")

           zt=1;

          else if(temp=="s2")

           zt=2;

           else if(temp=="s3")

           zt=3;

           else if(temp=="s4")

           zt=4;

           else if(temp=="s5")

           zt=5;

           else if(temp=="s6")

           zt=6;

           else if(temp=="s7")

           zt=7;

           else if(temp=="s8")

           zt=8;

           else if(temp=="s9")

           zt=9;

           else if(temp=="s10")

           zt=10;

           else if (temp=="s11")

           zt=11;

           lr.push_stack(zt,*p);

           p++;

           }

           else  if (temp[0]=='r') {

               int r_temp;

               if (temp=="r1")

                r_temp=1;

                else if (temp=="r2")

                r_temp=2;

                else if (temp=="r3")

                r_temp=3;

                else if (temp=="r4")

                r_temp=4;

                else if (temp=="r5")

                r_temp=5;

                else if (temp=="r6")

                r_temp=6;

                string find_string=unend_char[r_temp-1];

                for(int i=0;i<(int)find_string.size();i++)

                lr.pop_stack();

                char fh_temp=unend_char_l[r_temp-1];

                 int  zt_temp=lr.get_zt();

                int j;

                switch(fh_temp){

                  case 'i':j=0;break;

                  case '+':j=1;break;

                  case '*':j=2;break;

                  case '(':j=3;break;

                  case ')':j=4;break;

                  case '#':j=5;break;

                  case 'E':j=6;break;

                  case 'T':j=7;break;

                  case 'F':j=8;break;

              }

                 string zt_temp2=tab[zt_temp][j];

                 //cout<<"hahahahaha"<<zt_temp2<<endl;

                  int zt_temp4=atoi(zt_temp2.c_str());

                 /*char *zt_temp3=const_cast<char*>(zt_temp2.c_str());

                  int zt_temp4=atoi(zt_temp3);*/

                // char zt_temp4=char(zt_temp4);

                //cout<<zt_temp4<<endl;

                lr.push_stack(zt_temp4,fh_temp);

          }

     }

          }

//纪念我的代码第一次突破150行。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息