Guest User

Untitled

a guest
Jan 23rd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.02 KB | None | 0 0
  1. /*
  2. * g++ -o lt-regexp lt-regexp.cc -I/usr/include/libxml2 -I/home/fran/local/include/lttoolbox-3.2 -L/home/fran/local/lib -llttoolbox3 -llibxml2
  3. */
  4. #include <cwchar>
  5. #include <cstdio>
  6. #include <cerrno>
  7. #include <string>
  8. #include <iostream>
  9. #include <list>
  10. #include <set>
  11.  
  12. #include <lttoolbox/ltstr.h>
  13. #include <lttoolbox/lt_locale.h>
  14. #include <lttoolbox/transducer.h>
  15. #include <lttoolbox/alphabet.h>
  16. #include <lttoolbox/pool.h>
  17. #include <lttoolbox/state.h>
  18. #include <lttoolbox/regexp_compiler.h>
  19. #include <lttoolbox/match_exe.h>
  20. #include <lttoolbox/match_state.h>
  21. #include <lttoolbox/xml_parse_util.h>
  22.  
  23. wstring ws(char *arg)
  24. {
  25. wchar_t buf[1024];
  26. memset(buf, '\0', 1024);
  27. size_t num_chars = mbstowcs(buf, arg, strlen(arg));
  28. wstring ws(buf, num_chars);
  29. return ws;
  30. }
  31.  
  32. bool match(Transducer t, wstring str, Alphabet a)
  33. {
  34. map<int, int> finals;
  35. for(int i = 0; i < t.size(); i++)
  36. {
  37. if(t.isFinal(i))
  38. {
  39. continue;
  40. }
  41. finals[i] = i;
  42. }
  43. MatchExe me(t, finals);
  44. MatchState ms;
  45. ms.clear();
  46. ms.init(me.getInitial());
  47. wcout << ms.size() << " b" << endl;
  48. ms.step(L'b');
  49. wcout << ms.size() << " b" << endl;
  50.  
  51. for(wstring::iterator it = str.begin(); it != str.end(); it++)
  52. {
  53. wcout << ms.size() << " " << *it << endl;
  54. ms.step(*it);
  55. }
  56. int val = ms.classifyFinals(me.getFinals());
  57. fwprintf(stdout, L"%d\n", val);
  58.  
  59. if(val != -1)
  60. {
  61. return true;
  62. }
  63. return false;
  64. }
  65.  
  66. int main (int argc, char** argv)
  67. {
  68. Alphabet alphabet;
  69. Transducer t;
  70. RegexpCompiler re;
  71. bool matched;
  72.  
  73. LtLocale::tryToSetLocale();
  74.  
  75. if(argc < 3)
  76. {
  77. wcout << L"Usage: lt-regexp <pattern> <string to match>" << endl;
  78. exit(-1);
  79. }
  80.  
  81. FILE *output = stdout;
  82. wstring pattern = ws(argv[1]);
  83. wstring s = ws(argv[2]);
  84.  
  85. re.initialize(&alphabet);
  86. re.compile(pattern);
  87. t = re.getTransducer();
  88. t.minimize();
  89.  
  90. t.show(alphabet, output);
  91.  
  92. matched = match(t, s, alphabet);
  93.  
  94. wcout << endl << pattern << " " << s << endl;
  95. }
Add Comment
Please, Sign In to add comment