From a3cb4c5d02f0ee75094fc27814c71f8a025d034b Mon Sep 17 00:00:00 2001 From: sbrandt Date: Wed, 3 Apr 2013 19:12:46 +0000 Subject: Add the basic grammar files, and the changes to the piraha code necessary to parse all of Cactus. git-svn-id: http://svn.cactuscode.org/flesh/trunk@4990 17b73243-c579-4c4c-a9d2-2d5706c11dac --- src/piraha/Generic.cc | 90 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) (limited to 'src/piraha/Generic.cc') diff --git a/src/piraha/Generic.cc b/src/piraha/Generic.cc index 1695b873..f60696ee 100644 --- a/src/piraha/Generic.cc +++ b/src/piraha/Generic.cc @@ -1,5 +1,6 @@ #include "Piraha.hpp" #include +#include using namespace piraha; @@ -15,23 +16,98 @@ void read_file(const char *file,std::string& buf) { in.close(); } +void usage() { + std::cerr << "usage: generic [--perl] grammar input" << std::endl; + exit(2); +} + +#define VAR(X) " " << #X << "=" << X + +bool newEnd(std::string& in,const char *new_end,std::string& out) { + out = "/dev/null"; + int n = in.size(); + int i; + for(i=n-1;i>0;i--) { + if(in[i] == '.') { + break; + } + if(in[i] == '/') { + std::cout << VAR(in) << VAR(i) << std::endl; + break; + } + } + if(in[i] != '.') { + std::cout << VAR(in) << VAR(i) << std::endl; + return false; + } + out.clear(); + for(int j=0;j2 && arg[0]=='-' && arg[1]=='o') { + outFile = arg.substr(2,arg.size()); + oFlag = true; + } else if(narg == 0) { + grammarArg = argv[n]; + narg++; + } else if(narg == 1) { + inputArg = argv[n]; + narg++; + } else { + usage(); + } + } + if(!oFlag) { + if(perlFlag) { + newEnd(inputArg,".pm",outFile); + } else { + newEnd(inputArg,".pegout",outFile); + } } + std::cout << "reading file: " << inputArg << std::endl; std::string grammar_file, input_file; - read_file(argv[1],grammar_file); - read_file(argv[2],input_file); + read_file(grammarArg.c_str(),grammar_file); + read_file(inputArg.c_str(),input_file); smart_ptr g = new Grammar(); compileFile(g,grammar_file.c_str()); - smart_ptr mg = new Matcher (g,g->default_rule.c_str(),input_file.c_str()); + smart_ptr mg = + new Matcher (g,g->default_rule.c_str(),input_file.c_str()); if(mg->matches()) { - mg->dump(); + //std::vector vec(4096); + std::ofstream o; + //o.rdbuf()->pubsetbuf(&vec.front(),vec.size()); + o.open(outFile.c_str()); + std::cout << "writing file: " << outFile << std::endl; + smart_ptr src_file = + new Group("annot:src_file",inputArg.c_str()); + mg->children.push_back(src_file); + if(perlFlag) { + mg->dumpPerl(o); + } else { + mg->dump(o); + } + o.close(); } else { mg->showError(); + return 1; } return 0; } -- cgit v1.2.3