Debian Codegraph @

main/compile_regexes.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2008 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: May 2008 */
35 /*************************************************************************/
36 /* */
37 /* To avoid an initialization phase where regexes need to be set up we */
38 /* precompile them into statics that can be used directly -- */
39 /* (dhd championed this technique, and this is a reimplementation of */
40 /* his compilation technique this) */
41 /* */
42 /*************************************************************************/
43
44 #include <stdio.h>
45 #include <string.h>
46 #include <sys/time.h>
47 #include <unistd.h>
48
49 #include "flite.h"
50
51 static void compregex_usage()
52 {
53 printf("compregex: compile regexes into C structures\n");
54 printf("usage: compregex name regex\n"
55 " Compiles regex into a C structure called name\n");
56 exit(0);
57 }
58
59 static void regex_to_C(const char *name, const cst_regex *rgx)
60 {
61 int i;
62
63 printf("static const unsigned char %s_rxprog[] = {\n ",name);
64 for (i=0; i<rgx->regsize; i++)
65 {
66 printf("%d, ", (unsigned char)rgx->program[i]);
67 if (i%16 == 15)
68 printf("\n ");
69 }
70 printf("\n};\n");
71 printf("static const cst_regex %s_rx = {\n ",name);
72 printf("%d, ",rgx->regstart);
73 printf("%d, ",rgx->reganch);
74 if (rgx->regmust == NULL)
75 printf("NULL, ");
76 else
77 printf("%s_rxprog + %ld, ", name, (long int)(rgx->regmust - rgx->program));
78 printf("%d, ",rgx->regmlen);
79 printf("%d,\n ",rgx->regsize);
80 printf("(char *)%s_rxprog\n",name);
81 printf("};\n");
82
83 printf("const cst_regex * const %s = &%s_rx;\n\n",name, name);
84 }
85
86 int main(int argc, char **argv)
87 {
88 cst_regex *rgx;
89
90 if (argc != 3)
91 compregex_usage();
92
93 rgx = new_cst_regex(argv[2]);
94 regex_to_C(argv[1],rgx);
95 delete_cst_regex(rgx);
96
97 return 0;
98 }