compile_regexes.c 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 (awb@cs.cmu.edu) */
  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. #include <stdio.h>
  44. #include <string.h>
  45. #include <sys/time.h>
  46. #include <unistd.h>
  47. #include "flite.h"
  48. static void compregex_usage()
  49. {
  50. printf("compregex: compile regexes into C structures\n");
  51. printf("usage: compregex name regex\n"
  52. " Compiles regex into a C structure called name\n");
  53. exit(0);
  54. }
  55. static void regex_to_C(const char *name, const cst_regex *rgx)
  56. {
  57. int i;
  58. printf("static const unsigned char %s_rxprog[] = {\n ",name);
  59. for (i=0; i<rgx->regsize; i++)
  60. {
  61. printf("%d, ", (unsigned char)rgx->program[i]);
  62. if (i%16 == 15)
  63. printf("\n ");
  64. }
  65. printf("\n};\n");
  66. printf("static const cst_regex %s_rx = {\n ",name);
  67. printf("%d, ",rgx->regstart);
  68. printf("%d, ",rgx->reganch);
  69. if (rgx->regmust == NULL)
  70. printf("NULL, ");
  71. else
  72. printf("%s_rxprog + %ld, ", name, (long int)(rgx->regmust - rgx->program));
  73. printf("%d, ",rgx->regmlen);
  74. printf("%d,\n ",rgx->regsize);
  75. printf("(char *)%s_rxprog\n",name);
  76. printf("};\n");
  77. printf("const cst_regex * const %s = &%s_rx;\n\n",name, name);
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. cst_regex *rgx;
  82. if (argc != 3)
  83. compregex_usage();
  84. rgx = new_cst_regex(argv[2]);
  85. regex_to_C(argv[1],rgx);
  86. delete_cst_regex(rgx);
  87. return 0;
  88. }