⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pro.txt

📁 1 掌握手工生成词法分析器的方法
💻 TXT
📖 第 1 页 / 共 3 页
字号:
                                                 //若B→αAβ (β≠>ε),则把FIRST(β)-{ε}加入FOLLOW(A)中

                                                 if(IsTerminate(proNode[j].rightSym[k+1]))  

                                                        //如果β为终结符,则将其直接加入FOLLOW(A)中

                                                        AddChar(unTInfo[i].follow,proNode[j].rightSym[k+1]);

                                                 else {      //如果β为非终结符,则将FIRST(β)-{ε}加入FOLLOW(A)中

                                                        int rightSymLoc = GetUTLoaction(UnTerminate,proNode[j].rightSym[k+1]);

                                                        int len = strlen(unTInfo[rightSymLoc].first);

                                                        for(int l = 0 ; l < len ; l++) {

                                                               if(unTInfo[rightSymLoc].first[l] != '^')

                                                                      AddChar(unTInfo[i].follow,unTInfo[rightSymLoc].first[l]);

                                                        }

                                                 }

                                          }

                                   }

                            }

                     }

              }

       }

       

       void SetSelect(ProNode proNode[],UnTInfo unTInfo[])  {//设置Select集合

              bool isNull,isVT;

              for(int i = 0 ;i < proNum; i++) { //扫描每一个产生式,求出Select集合

                     if(IsTerminate(proNode[i].rightSym[0])) {  

                            //如果产生式的右边第一字符为终结符,则将其加入select集

                            AddChar(select[i],proNode[i].rightSym[0]);

                     }

                     else if(proNode[i].rightSym[0] == '^')

                            //如果产生式的右边字符为ε,则应将左边字符的follow集加入select集

                            AddCharToChar(select[i],unTInfo[GetUTLoaction(UnTerminate,proNode[i].leftSym)].follow);

                     else {

                            isVT = false;

                            isNull = true;

                            for(int j = 0; j < proNode[i].length; j++) {

                                   //产生式右边的第一个字符为非终结符,则逐个分析

                                   if(!IsTerminate(proNode[i].rightSym[j])) { 

                                          //若α=X1X2 … Xnα′,其中Xi∈VN , 1≤i≤n;

                                          //若ε∈FIRST(X1),则将FIRST(X2)中的一切非ε的终结符加进FIRST(α),

                                          //FIRST(α)则应加入select集;以此类推

                                          int rightSymLoc = GetUTLoaction(UnTerminate,proNode[i].rightSym[j]);

                                          int firLen = strlen(unTInfo[rightSymLoc ].first);

                                          for(int k = 0 ; k < firLen ; k++)

                                                 if(unTInfo[rightSymLoc ].first[k] != '^')

                                                        AddChar(select[i],unTInfo[rightSymLoc].first[k]);

                                                 if(!IsNull(proNode[i].rightSym[j]))  {

                                                        //如果该非终结符不能推出空字符则应停止循环

                                                        isNull = false;

                                                        break;

                                                 }

                                   }

                                   else {      //遇到了终结符号,此时也应终止循环

                                          isVT = true;

                                          break;

                                   }

                            }

                            

                            if(isVT&&isNull)  //处理像E->ABaβ的产生式的情况

                                   AddChar(select[i],proNode[i].rightSym[j]);

                            if(j == proNode[i].length) {

                                   //若α=X1X2 … Xnα′,其中Xi∈VN , 1≤i≤n;

                                   //若对于一切1≤i≤n,ε∈FIRST(Xi),则将follow(α)加入select集合

                                   int leftSymLoc = GetUTLoaction(UnTerminate,proNode[i].leftSym);

                                   AddCharToChar(select[i],unTInfo[leftSymLoc].follow);

                            }     

                     }

              }

       }

       

       void SetSheet(ProNode proNode[],UnTInfo unTInfo[]) { //构造分析表

              int selLen ;

              int rowLoc,colLoc;

              for(int i = 0; i < proNum ; i++) {

                     selLen = strlen(select[i]);

                     for(int j = 0 ; j < selLen ; j++) {

                            rowLoc = GetUTLoaction(UnTerminate,proNode[i].leftSym);

                            colLoc = GetTLocaction(Terminate,select[i][j]);

                            sheet[rowLoc][colLoc] = proNode[i];

                     }

              }

              

       }

       

       void InputSym() {  //输入字符串函数

              InitQueue(Remain);

              cout<<"请输入要分析的字符串(以'#'结束):"<<endl; 

              char tmpChar;

              int i = 0 ;

              cin>>tmpChar;

              while(tmpChar != '#') {

                     EnQueue(Remain,tmpChar);

                     cin>>tmpChar;

              }

              EnQueue(Remain,tmpChar);

              

       }

       

       void Scan() {  //分析扫描的主控程序

              int i = 0 ;

              int step = 1;

              int rightLoc;

              int leftLoc;

              char a;

              char x;

              bool flag = true;

              //SymStack[i] = '#';

              //SymStack[++i] = UnTerminate[0]; //'#' 、开始符号入栈

              //SqStack s; 

              SqStack SymStack;              //符号栈 

              InitStack(SymStack);

              cout<<"步骤"<<"\t"<<"符号栈"<<"\t\t"<<"读入符号"<<"\t"<<"剩余符号串"<<"\t"<<"使用产生式"<<endl;

              cout<<step++<<"\t";

              Push(SymStack,'#');

              Push(SymStack,UnTerminate[0]); //'#' 、开始符号入栈

              PrintSym(SymStack);

              a = GetSym(Remain); //读入第一个符号

              cout<<a<<"\t\t";

              while(flag) {

                     PrintRemain(Remain);

                     x = Pop(SymStack); //从栈中弹出符号

                     leftLoc = GetUTLoaction(UnTerminate,x);

                     rightLoc = GetTLocaction(Terminate,a);

                     PrintSheet(leftLoc,rightLoc);

                     cout<<"\n";

                     if(IsTerminate(x)){

                            if(x == a) {

                                   a = GetSym(Remain);

                                   cout<<step++<<"\t";

                                   PrintSym(SymStack);

                                   cout<<a<<"\t\t";

                            }

                            else {

                                   flag = false;

                                   Error();

                            }

                     }

                     else if(x == '#') {    

                            

                            if(x == a) {     //分析成功

                                   Success();

                                   flag = 0 ; 

                            }

                            else {

                                   flag = 0 ;

                                   Error();

                            }

                     }

                     else if(sheet[GetUTLoaction(UnTerminate,x)][GetTLocaction(Terminate,a)].leftSym \

                            != '\0' ) { // X ∈Vn查分析表

                            //Pop(SymStack);

                            leftLoc = GetUTLoaction(UnTerminate,x);

                            rightLoc = GetTLocaction(Terminate,a);

                            

                            

                            if(sheet[leftLoc][rightLoc].rightSym[0] != '^') {

                                   //若X1X2…Xn != ε,则反顺序进栈

                                   

                                   int rightLen = strlen(sheet[leftLoc][rightLoc].rightSym);

                                   for(int i = rightLen - 1; i >=0 ; i--) {

                                          Push(SymStack,sheet[leftLoc][rightLoc].rightSym[i]);

                                   }

                                   cout<<step++<<"\t";

                                   PrintSym(SymStack);

                                   cout<<a<<"\t\t";

                            }

                            else {

                                   cout<<step++<<"\t";

                                   PrintSym(SymStack);

                                   cout<<a<<"\t\t";

                            }

                            

                     }

                     else {

                            flag = 0;

                            Error();

                     }

              }

              

       }

       

       char GetSym(LinkQueue &q) {

              if(q.front == q.rear) return NULL;

              QueuePtr p = (QueuePtr)malloc(sizeof(QNode));

              p = q.front->next;

              char tmp = p->data;

              q.front->next = p->next;

              if(q.rear == p) q.rear = q.front;

              free(p);

              return tmp;

       }

       

       void PrintSym(SqStack &s) {       //显示符号栈 符号

              char* i ;

              for( i = s.base ; i < s.top;i++)

                     cout<<*i;

              cout<<"\t\t";

       }

       

       void PrintRemain(LinkQueue &q) {      //显示剩余符号串

              QueuePtr d ;

              char tmp;

              if(q.front->next != NULL) {

                     d =q.front->next;

                     do {

                            tmp = d->data;

                            cout<<tmp;

                            d = d->next;

                     }while(tmp != '#');

                     cout<<"\t\t";

              }

              else return ;

       }

       void PrintSheet(int row,int col) {   //显示所使用产生式

              if(row != -1 && col != -1) {

                     cout<<sheet[row][col].leftSym<<sheet[row][col].midSym[0]<<sheet[row][col].midSym[1];

                     for(int j = 0; j < sheet[row][col].length;j++)

                            cout<<sheet[row][col].rightSym[j];

              }

              else

                     cout<<" ";

       }

 

 

       void Success() {//分析成功

       

              cout<<"Successful!"<<endl;

       }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -