回复:帮助提升语法

时间:2010-04-19 19:50:42

标签: c++ grammar boost-spirit

我重新设计并扩展了我之前提到的语法,如下所示:

// BIFAnalyser.cpp : Defines the entry point for the console application.
//
//
/*=============================================================================
    Copyright (c) Temitope Jos Onunkun 2010 
    http://www.dcs.kcl.ac.uk/pg/onun/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
////////////////////////////////////////////////////////////////////////////
//                                                                        //
//  B Machine parser using the Boost "Grammar" and "Semantic Actions".    // 
//                                                                        //
////////////////////////////////////////////////////////////////////////////

#include <boost/spirit/core.hpp>
#include <boost/tokenizer.hpp>
#include <iostream>
#include <string>
#include <fstream>
#include <vector>


////////////////////////////////////////////////////////////////////////////
using namespace std;
using namespace boost::spirit;

////////////////////////////////////////////////////////////////////////////
//
//  Semantic Actions
//
////////////////////////////////////////////////////////////////////////////
//
//
namespace 
{



 //semantic action function on individual lexeme
    void    do_noint(char const* start, char const* end)
    {
        string  str(start, end);

 if (str != "NAT1")
        cout << "PUSH(" << str << ')' << endl;
    }

 //semantic action function on addition of lexemes
    void do_add(char const*, char const*)    
 { 
  cout << "ADD" << endl; 
 // for(vector<string>::iterator vi = strVect.begin(); vi < strVect.end(); ++vi)
 //  cout << *vi << " ";
 }

 //semantic action function on subtraction of lexemes
    void do_subt(char const*, char const*)   
 { 
  cout << "SUBTRACT" << endl; 
 }

 //semantic action function on multiplication of lexemes
    void do_mult(char const*, char const*)   
 { 
  cout << "\nMULTIPLY" << endl;
 }

 //semantic action function on division of lexemes
    void do_div(char const*, char const*)    
 { 
  cout << "\nDIVIDE" << endl; 

 }


//

//

 vector<vector<string> > flowTable;


 //semantic action function on simple substitution
    void do_sSubst(char const* start, char const* end)    
 { 
  string  str(start, end);

  //use boost tokenizer to break down tokens
  typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
  boost::char_separator<char> sep(" -+/*:=()",0,boost::drop_empty_tokens); // char separator definition
        Tokenizer tok(str, sep);
  Tokenizer::iterator tok_iter = tok.begin();

  pair<string, string > dependency; //create a pair object for dependencies


  //create a vector object to store all tokens 
  vector<string> dx;
   // 

  int counter = 0; // tracks token position

  for(tok.begin(); tok_iter != tok.end(); ++tok_iter)  //save all tokens in vector
  {
   dx.push_back(*tok_iter );
  }
  counter = dx.size();

  //
  vector<string> d_hat; //stores set of dependency pairs

  string dep; //pairs variables as string object

  //
  dependency.first = *tok.begin();
  vector<string> FV;

  for(int unsigned i=1; i < dx.size(); i++)
  {   
    //
   if(!atoi(dx.at(i).c_str()) && (dx.at(i) !=" ")) 
   {
    dependency.second = dx.at(i);
    dep = dependency.first + "|->" + dependency.second + " ";
    d_hat.push_back(dep);

    vector<string> row; 
    row.push_back(dependency.first);  //push x_hat into first column of each row

    for(unsigned int j=0; j<2; j++)
    {

     row.push_back(dependency.second);//push an element (column) into the row
    }

    flowTable.push_back(row); //Add the row to the main vector

   }
  }

  //displays internal representation of information flow table
  cout << "\n******************************\nDependency Table\n******************************\n";
  cout << "X_Hat\tDx\tG_Hat\n";
  cout << "-----------------------------\n";
  for(unsigned int  i=0; i < flowTable.size(); i++)
  {
   for(unsigned int j=0; j<2; j++)
   {
    cout << flowTable[i][j] << "\t ";
   }
   if (*tok.begin() != "WHILE" ) //if there are no global flows,
    cout << "\t{}";     //display empty set 

   cout << "\n";
  } 
  cout << "*****************************\n\n";


  for(int unsigned j=0; j < FV.size(); j++)
  {  
   if(FV.at(j) != dependency.second)
    dep = dependency.first + "|->" + dependency.second + " ";
   d_hat.push_back(dep);
  }





  cout << "PUSH(" << str << ')' << endl;

  cout << "\n****************\nDependency pairs\n****************\n";

  for(int unsigned i=0; i < d_hat.size(); i++)
   cout << d_hat.at(i) << "\n...\n";

  cout << "\nSIMPLE SUBSTITUTION\n\n";

 }

    //semantic action function on multiple substitution
 void do_mSubst(char const* start, char const* end)    
 { 
  string  str(start, end);


  cout << "PUSH(" << str << ')' << endl;
  //cout << "\nMULTIPLE SUBSTITUTION\n\n";
 } 


 //semantic action function on unbounded choice substitution
 void do_mChoice(char const* start, char const* end)    
 { 
  string  str(start, end);

  cout << "PUSH(" << str << ')' << endl;
  cout << "\nUNBOUNDED CHOICE SUBSTITUTION\n\n";
 } 


 void do_logicExpr(char const* start, char const* end)    
 { 
  string  str(start, end);

  //use boost tokenizer to break down tokens
  typedef boost::tokenizer<boost::char_separator<char> > Tokenizer;
  boost::char_separator<char> sep(" -+/*=:()><",0,boost::drop_empty_tokens); // char separator definition
        Tokenizer tok(str, sep);
  Tokenizer::iterator tok_iter = tok.begin();

  //pair<string, string > dependency; //create a pair object for dependencies


  //create a vector object to store all tokens 
  vector<string> dx;

  for(tok.begin(); tok_iter != tok.end(); ++tok_iter)  //save all tokens in vector
  {
   dx.push_back(*tok_iter );
  }

  for(unsigned int i=0; i<dx.size(); i++)
   if(!atoi(dx.at(i).c_str()) && (dx.at(i) !=" ") ) 
   {
    cout << "\nFree Variables: " << dx.at(i)<< endl;
   }


  cout << "PUSH(" << str << ')' << endl;
  cout << "\nPREDICATE\n\n";
 } 


 void  do_predicate(char const* start, char const* end)    
 { 
  string  str(start, end);

  cout << "PUSH(" << str << ')' << endl;
  cout << "\nMULTIPLE PREDICATE\n\n";
 } 


 void do_ifSelectPre(char const* start, char const* end)    
 { 
  string  str(start, end);

  //if

  cout << "PUSH(" << str << ')' << endl;
  cout << "\nPROTECTED SUBSTITUTION\n\n";
 } 


 //semantic action function on machine substitution
 void do_machSubst(char const* start, char const* end)    
 { 
  string  str(start, end);



  cout << "PUSH(" << str << ')' << endl;
  cout << "\nMACHINE SUBSTITUTION\n\n";
 } 
} 

////////////////////////////////////////////////////////////////////////////
//
//  Machine Substitution Grammar
//
////////////////////////////////////////////////////////////////////////////

//  Simple substitution grammar parser with integer values removed
struct Substitution : public grammar<Substitution>
{
    template <typename ScannerT>
    struct definition
    {
        definition(Substitution const& )
        {

   machine_subst
     = (  (simple_subst)
        | (multi_subst)
        | (if_select_pre_subst)
        | (unbounded_choice) )[&do_machSubst]
     ;


   unbounded_choice
     = str_p("ANY") >> ide_list
     >> str_p("WHERE") >> predicate
     >> str_p("THEN")   
     >> machine_subst
     >> str_p("END")
     ;

   if_select_pre_subst
     = (  (  str_p("IF") >> predicate >> str_p("THEN") >> machine_subst 
     >> *( str_p("ELSIF") >> predicate >> machine_subst )
     >> !( str_p("ELSE") >> machine_subst) 
     >> str_p("END") )
     | ( str_p("SELECT") >> predicate >> str_p("THEN") >> machine_subst
     >> *( str_p("WHEN") >> predicate >> machine_subst )
     >> !( str_p("ELSE") >> machine_subst) 
     >> str_p("END"))
     | ( str_p("PRE") >> predicate >> str_p("THEN") >> machine_subst 
     >> str_p("END") )  )[&do_ifSelectPre]
     ;



   multi_subst
     = ( (machine_subst) 
     >> *( ( str_p("||") >> (machine_subst) ) 
     |     ( str_p("[]") >> (machine_subst) ) )  ) [&do_mSubst]
     ;

   simple_subst
     = (identifier
     >> str_p(":=") >> arith_expr) [&do_sSubst]
     ;

   expression
     = predicate
     | arith_expr
     ;

   predicate
     = ( (logic_expr) 
     >> *( ( ch_p('&') >> (logic_expr) )
     |   ( str_p("OR") >> (logic_expr) ) ) )[&do_predicate]
     ;

   logic_expr 
     = ( identifier
     >> (str_p("<") >> arith_expr)
     | (str_p("<")  >> arith_expr)
     | (str_p("/:")  >> arith_expr)
     | (str_p("<:")  >> arith_expr)
     | (str_p("/<:")  >> arith_expr)
     | (str_p("<<:")  >> arith_expr)
     | (str_p("/<<:")  >> arith_expr)
     | (str_p("<=")  >> arith_expr)
     | (str_p("=")  >> arith_expr)
     | (str_p(">=")  >> arith_expr)
     | (str_p("=>")  >> arith_expr)
     )  [&do_logicExpr]
     ;

   arith_expr
     =   term
     >>  *(   ('+' >> term)[&do_add]
     |   ('-' >> term)[&do_subt] )
          ;

   term
     =   factor
     >>  *(   ('*' >> factor)[&do_mult]
     |   ('/' >> factor)[&do_div]  )
     ;


   factor
     =   lexeme_d[( identifier | +digit_p)[&do_noint]]
          |   '(' >> expression >> ')'
          |   ('+' >> factor)
          ;


   ide_list
     = identifier 
     >> *( ch_p(',') >> identifier )
     ;


   identifier
     = alpha_p >> +( alnum_p | ch_p('_') )
     ;

  }

  rule<ScannerT> machine_subst, unbounded_choice, if_select_pre_subst, multi_subst, 
        simple_subst, expression, predicate, logic_expr, arith_expr, 
        term, factor, ide_list, identifier;

        rule<ScannerT> const&
        start() const 
  { 
   return predicate; 
   //return multi_subst; 
   //return machine_subst; 
  }
    };
};

////////////////////////////////////////////////////////////////////////////
//
//  Main program
//
////////////////////////////////////////////////////////////////////////////
int
main()
{
    cout << "************************************************************\n\n";
    cout << "\t\t...Machine Parser...\n\n";
    cout << "************************************************************\n\n";
   // cout << "Type an expression...or [q or Q] to quit\n\n";

 string str;
 int machineCount = 0;
 char strFilename[256]; //file name store as a string object
 do
 {

 cout << "Please enter a filename...or [q or Q] to quit:\n\n "; //prompt for file name to be input
 //char strFilename[256]; //file name store as a string object
 cin >> strFilename;

 if(*strFilename == 'q' || *strFilename == 'Q') //termination condition
   return 0;

 ifstream inFile(strFilename); // opens file object for reading
 //output file for truncated machine (operations only)

   if (inFile.fail())    
   cerr << "\nUnable to open file for reading.\n" << endl;

 inFile.unsetf(std::ios::skipws); 


    Substitution elementary_subst;  //  Simple substitution parser object


    string next;

 while (inFile >> str)
    {
  getline(inFile, next);

  str += next;

        if (str.empty() || str[0] == 'q' || str[0] == 'Q')
            break;

   parse_info<> info = parse(str.c_str(), elementary_subst >> !end_p, space_p);

   if (info.full)
   {
    cout << "\n-------------------------\n";
    cout << "Parsing succeeded\n";
    cout << "\n-------------------------\n";
   }
   else
   {
    cout << "\n-------------------------\n";
    cout << "Parsing failed\n";
    cout << "stopped at: " << info.stop << "\"\n";
    cout << "\n-------------------------\n";
   }

 }


}
while ( (*strFilename != 'q' || *strFilename !='Q')); 

    return 0;
}

但是,我在测试时遇到以下意外行为:

我使用的文本文件是:

f1.txt,  ... containing ...:  debt:=(LoanRequest+outstandingLoan1)*20 .
f2.txt,  ... containing ...:  debt:=(LoanRequest+outstandingLoan1)*20 || newDebt := loanammount-paidammount || price := purchasePrice + overhead + bb .
f3.txt,  ... containing ...:  yy < (xx+7+ww) .
f4.txt,  ... containing ...:  yy < (xx+7+ww) & yy : NAT .

当我使用multi_subst作为启动规则时,两个文件(f1和f2)都被正确解析;

当我使用machine_subst作为启动规则文件f1正确解析时,文件f2失败,产生错误:“Parsing failed stopped at: || newDebt := loanammount-paidammount || price := purchasePrice + overhead + bb”

当我使用谓词作为起始符号时,文件f3正确解析,但文件f4产生错误:“ “Parsing failed stopped at: & yy : NAT”

请问有人可以帮助语法吗?看来我到目前为止无法发现的语法存在问题。

1 个答案:

答案 0 :(得分:1)

我重新设计了如下语法,这似乎解决了这个问题:

SUBST                     = multi_choice                     | machine_subst                     ;

        multi_choice 
                = machine_subst
                >> +( str_p("[]") >> machine_subst )
                ;


        machine_subst
                = (  multi_subst 
                | simple_subst
                | if_select_pre_subst
                | unbounded_choice )[&do_machSubst]
                ;

... ...

multi_subst                     =(simple_subst

  
    

+(str_p(“||”)&gt;&gt; simple_subst)                           )[&amp; do_mSubst]                         ;

  
        simple_subst
                = (identifier
                >> str_p(":=") >> arith_expr) [&do_sSubst]
                ;

        expression
                = predicate
                | logic_expr
                | arith_expr
                ;

        predicate
                = ( logic_expr 
                >> +( ( str_p("&") >> logic_expr )
                |     ( str_p("OR") >> logic_expr ) ) )[&do_predicate]
                ;

        logic_expr 
                = ( identifier
                >> ( (str_p("<") >> arith_expr)
                | (str_p(">")  >> arith_expr)
                | (str_p("/:")  >> arith_expr)
                | (str_p("<:")  >> arith_expr)
                | (str_p("/<:")  >> arith_expr)
                | (str_p("<<:")  >> arith_expr)
                | (str_p("/<<:")  >> arith_expr)
                | (str_p("<=")  >> arith_expr)
                | (str_p("=")  >> arith_expr)
                | (str_p(">=")  >> arith_expr)
                | (str_p("=>")  >> arith_expr) )
                )  [&do_logicExpr]
                ;

... ... 我现在对文件f1.txt和f2.txt使用启动规则“subst”,对f3.txt和f4.txt使用“expression”。

start()const         {             返回;             // return machine_subst;             //返回表达式;             //返回if_select_pre_subst;             //返回multi_choice;             //返回unbounded_choice;         }

我仍在构建语法,所以如果我有任何进一步的问题,我会发布它。