Changeset 4328


Ignore:
Timestamp:
Dec 9, 2014, 8:08:34 AM (4 years ago)
Author:
cameron
Message:

-e -f options for multiple regular expressions

Location:
icGREP/icgrep-devel/icgrep
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/compiler.cpp

    r4287 r4328  
    1414#include <re/re_nullable.h>
    1515#include <re/re_simplifier.h>
     16#include <re/re_alt.h>
    1617#include <re/parsefailure.h>
    1718#include <re/re_parser.h>
    1819#include <re/re_compiler.h>
    19 #include "hrtime.h"
    2020#include <utf8_encoder.h>
    2121#include <cc/cc_compiler.h>
     
    4040namespace icgrep {
    4141
    42 LLVM_Gen_RetVal compile(const Encoding encoding, const std::string input_string, const bool show_compile_time, const bool enable_multiplexing) {
     42LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing) {
     43    std::vector<RE *> REs;
    4344    RE * re_ast = nullptr;
    44     try
    45     {
    46         re_ast = RE_Parser::parse(input_string);
     45    for (int i = 0; i < regexps.size(); i++) {
     46        try
     47        {
     48            re_ast = RE_Parser::parse(regexps[i]);
     49        }
     50        catch (ParseFailure failure)
     51        {
     52            std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
     53            std::cerr << regexps[i] << std::endl;
     54            exit(1);
     55        }
     56        REs.push_back(re_ast);
    4757    }
    48     catch (ParseFailure failure)
    49     {
    50         std::cerr << "REGEX PARSING FAILURE: " << failure.what() << std::endl;
    51         std::cerr << input_string << std::endl;
    52         exit(1);
     58    if (REs.size() > 1) {
     59        re_ast = makeAlt(REs.begin(), REs.end());
    5360    }
    5461
     
    122129
    123130    PabloCompiler pablo_compiler(basisBits);
    124     unsigned long long cycles = 0;
    125     double timer = 0;
    126     if (show_compile_time)
    127     {
    128         cycles = get_hrcycles();
    129         timer = getElapsedTime();
    130     }
    131131
    132132    LLVM_Gen_RetVal retVal = pablo_compiler.compile(main);
    133     if (show_compile_time)
    134     {
    135         cycles = get_hrcycles() - cycles;
    136         timer = getElapsedTime() - timer;
    137         std::cout << "LLVM compile time -  cycles:       " << cycles  << std::endl;
    138         std::cout << "LLVM compile time -  milliseconds: " << timer << std::endl;
    139     }
    140133
    141134    PabloAST::release_memory();
  • icGREP/icgrep-devel/icgrep/compiler.h

    r4287 r4328  
    1414namespace icgrep {
    1515
    16 pablo::LLVM_Gen_RetVal compile(const Encoding encoding, const std::string input_string, const bool show_compile_time = false, const bool enable_multiplexing = false);
     16pablo::LLVM_Gen_RetVal compile(const Encoding encoding, const std::vector<std::string> regexps, const bool enable_multiplexing = false);
    1717
    1818}
  • icGREP/icgrep-devel/icgrep/icgrep.cpp

    r4327 r4328  
    66
    77#include <string>
     8#include <iostream>
     9#include <fstream>
    810
    911#include "icgrep.h"
     
    1618
    1719int main(int argc, char *argv[]) {
    18     static cl::opt<std::string> regexp1(cl::Positional, cl::Required, cl::desc("<regexp>"));
    19     static cl::list<std::string> inputFiles(cl::Positional, cl::desc("<input file ...>"), cl::OneOrMore);
     20    int firstInputFile = 1;  // Normal case when first positional arg is a regex. 
     21    cl::list<std::string> inputFiles(cl::Positional, cl::desc("<regex> <input file ...>"), cl::OneOrMore);
    2022   
    21     static cl::opt<bool>CountOnly("c", cl::desc("Count and display the matching lines per file only."));
     23    cl::opt<bool> CountOnly("c", cl::desc("Count and display the matching lines per file only."));
    2224    cl::alias CountOnlyLong("-count", cl::desc("Alias for -c"), cl::aliasopt(CountOnly));
    2325   
    24     static cl::opt<bool>ShowFileNames("H", cl::desc("Show the file name with each matching line."));
     26    cl::opt<bool> ShowFileNames("H", cl::desc("Show the file name with each matching line."));
    2527    cl::alias ShowFileNamesLong("-with-filename", cl::desc("Alias for -H"), cl::aliasopt(ShowFileNames));
    2628   
    27     static cl::opt<bool>ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
     29    cl::opt<bool> ShowLineNumbers("n", cl::desc("Show the line number with each matching line."));
    2830    cl::alias ShowLineNumbersLong("-line-number", cl::desc("Alias for -n"), cl::aliasopt(ShowLineNumbers));
    2931   
    30     //cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("<filename>"));
     32    cl::list<std::string> regexVector("e", cl::desc("Regular exprssion"), cl::ZeroOrMore);
     33   
     34    cl::opt<std::string> RegexFilename("f", cl::desc("Take regular expressions (one per line) from a file"), cl::value_desc("regex file"), cl::init(""));
    3135 
    3236    // Does -b mean the byte offset of the line, or the byte offset of the match start within the line?
     
    3741
    3842    cl::ParseCommandLineOptions(argc, argv);
    39 
    40     const auto llvm_codegen = icgrep::compile(encoding, regexp1, false, false);
     43   
     44    //std::vector<std::string> regexVector;
     45    if (RegexFilename != "") {
     46        std::ifstream regexFile(RegexFilename.c_str());
     47        std::string r;
     48        if (regexFile.is_open()) {
     49            while (std::getline(regexFile, r)) {
     50                regexVector.push_back(r);
     51            }
     52            regexFile.close();
     53        }
     54    }
     55   
     56    // if there are no regexes specified through -e or -f, the first positional argument
     57    // must be a regex, not an input file.
     58   
     59    if (regexVector.size() == 0) {
     60        regexVector.push_back(inputFiles[0]);
     61        firstInputFile = 1;
     62    }
     63    else {
     64        firstInputFile = 0;
     65    }
     66   
     67    const auto llvm_codegen = icgrep::compile(encoding, regexVector, false);
    4168
    4269    if (llvm_codegen.process_block_fptr != 0) {
     
    4673        grepEngine.setCountOnlyOption(CountOnly);
    4774        grepEngine.setShowLineNumberOption(ShowLineNumbers);
    48         if (inputFiles.size() > 1 || ShowFileNames) grepEngine.setShowFileNameOption();
    49         for (unsigned i = 0; i != inputFiles.size(); ++i) {
     75            if (inputFiles.size() > (firstInputFile + 1) || ShowFileNames) {
     76            grepEngine.setShowFileNameOption();
     77        }
     78        for (unsigned i = firstInputFile; i != inputFiles.size(); ++i) {
    5079            grepEngine.doGrep(inputFiles[i].c_str());
    5180        }
Note: See TracChangeset for help on using the changeset viewer.