source: icGREP/icgrep-devel/icgrep/re/re_toolchain.cpp @ 4984

Last change on this file since 4984 was 4984, checked in by cameron, 3 years ago

Refactor IDISA, re, pablo toolchain components

File size: 3.1 KB
Line 
1/*
2 *  Copyright (c) 2015 International Characters.
3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
6
7#include <string>
8#include <iostream>
9#include <fstream>
10
11#include <re/re_toolchain.h>
12#include <re/re_cc.h>
13#include <re/re_nullable.h>
14#include <re/re_simplifier.h>
15#include <re/re_alt.h>
16#include <re/parsefailure.h>
17#include <re/re_parser.h>
18#include <re/re_compiler.h>
19#include <utf8_encoder.h>
20#include <cc/cc_compiler.h>
21#include <pablo/function.h>
22#include <re/printer_re.h>
23#include <llvm/Support/CommandLine.h>
24
25
26using namespace pablo;
27
28
29static cl::OptionCategory cRegexOutputOptions("Regex Dump Options",
30                                              "These options control printing of intermediate regular expression structures.");
31static cl::opt<bool> PrintAllREs("print-REs", cl::init(false), cl::desc("print regular expression passes"), cl::cat(cRegexOutputOptions));
32static cl::opt<bool> PrintParsedREs("print-parsed-REs", cl::init(false), cl::desc("print out parsed regular expressions"), cl::cat(cRegexOutputOptions));
33static cl::opt<bool> PrintStrippedREs("print-stripped-REs", cl::init(false), cl::desc("print out REs with nullable prefixes/suffixes removed"), cl::cat(cRegexOutputOptions));
34static cl::opt<bool> PrintNamedREs("print-named-REs", cl::init(false), cl::desc("print out named REs"), cl::cat(cRegexOutputOptions));
35static cl::opt<bool> PrintUTF8REs("print-utf8-REs", cl::init(false), cl::desc("print out UTF-8 REs"), cl::cat(cRegexOutputOptions));
36static cl::opt<bool> PrintSimplifiedREs("print-simplified-REs", cl::init(false), cl::desc("print out final simplified REs"), cl::cat(cRegexOutputOptions));
37
38re::RE * regular_expression_passes(const Encoding encoding, re::RE * re_ast)  {
39    if (PrintAllREs || PrintParsedREs) {
40        std::cerr << "Parser:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
41    }
42
43    //Optimization passes to simplify the AST.
44    re_ast = re::RE_Nullable::removeNullablePrefix(re_ast);
45    if (PrintAllREs || PrintStrippedREs) {
46        std::cerr << "RemoveNullablePrefix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
47    }
48    re_ast = re::RE_Nullable::removeNullableSuffix(re_ast);
49    if (PrintAllREs || PrintStrippedREs) {
50        std::cerr << "RemoveNullableSuffix:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
51    }
52
53    re_ast = re::RE_Simplifier::simplify(re_ast);
54    if (PrintAllREs || PrintSimplifiedREs) {
55        //Print to the terminal the AST that was generated by the simplifier.
56        std::cerr << "Simplifier:" << std::endl << Printer_RE::PrintRE(re_ast) << std::endl;
57    }
58    return re_ast;
59}
60   
61PabloFunction * re2pablo_compiler(const Encoding encoding, re::RE * re_ast) {
62    PabloFunction * function = PabloFunction::Create("process_block", 8, 2);
63    cc::CC_Compiler cc_compiler(*function, encoding);
64    re::RE_Compiler re_compiler(*function, cc_compiler);
65    re_compiler.initializeRequiredStreams();
66    re_compiler.compileUnicodeNames(re_ast);
67    re_compiler.finalizeMatchResult(re_compiler.compile(re_ast));
68    return function;
69}
70
Note: See TracBrowser for help on using the repository browser.