get.cpp

来自「一个语法分析程序,C++写的,内含原码和exe文件. 开发工具:DEV-C」· C++ 代码 · 共 90 行

CPP
90
字号
#include "global.h"
/*-----------------------------------------
  To get grammer from the standard input
   stream.
    avoid file processing :P  
------------------------------------------*/

string choice; // for user's decision 

int lineno = 0;

list<string> production[MAXLINE]; 

void showMessage()
{
  cout << " LL(1) parsing machine @ Math Frog && FLower @ " << endl;
  cout << endl;
  cout << " ** Please input the grammer you want to process ** \n";
  cout << "\n ** REMIND : Just type a space after entering a symbol **";
  cout << "\n ** press # to end inputing production **";
  cout << "\n ** press $ to end inputing grammer **" << endl; 
  cout << "***********************************" << endl;
  }

void getGrammer()
{
while(true){
  showMessage();
  string tmp; // holding input string temporily  
  for(;lineno < MAXLINE;){
     cout << "line " << lineno << ": ";
     cin >> tmp;
     while(tmp != "#" && tmp != "$"){
     production[lineno].push_back(tmp);
     cin >> tmp;
     } 
  if(tmp == "$") break; // end inputing grammer
  lineno++;
   }   
   cout << "***********************************" << endl;
  cout << endl;
for(int j = 0;j < lineno;j++)
  print(production[j]);
  cout << endl;
   cout << "***********************************" << endl;
  cout << endl;
  cout << "\nIs the grammer you want to anlysis ? " << endl;
  cout << endl;
  cout << "(Type Y for yes and N for no)" << endl;  
  cin >> choice;
  if(choice == "Y")
   break;
  for(int i = 0;i < lineno;i++) // everything just begins 
   production[i].erase(production[i].begin(),production[i].end());
   lineno = 0; 
}
}


void getCoGrammer()
{
list<string> hold;
for(int line = 0;line < lineno;line++){
  list<string>::iterator iter = production[line].begin();
   iter++; // to the ->
   iter++; // to the first on the right of the production
  while(iter != production[line].end()){
   hold.push_back(production[line].front());
     hold.push_back("->");
     if(iter == --production[line].end())
       hold.push_back(*iter);
     else{
       while(*iter != "|" && iter != production[line].end()){
         hold.push_back(*iter);
         if(iter == --production[line].end()) break;
         iter++;
       }
     }
     coProduction[colineno++] = hold; // just use the overloaded =
     hold.erase(hold.begin(),hold.end()); // make hold empty list
     iter++;
   }
  }
} 
bool isLeftRecursion()
{
    return false;
}    

⌨️ 快捷键说明

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