Guest User

Untitled

a guest
Jan 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 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.  
  36.   for(int i = 0; i < t.size(); i++)
  37.   {
  38.     if(t.isFinal(i))
  39.     {
  40.       finals[i] = i;
  41.     }
  42.     fwprintf(stdout, L"%d %d\n", i, t.isFinal(i));
  43.   }
  44.   MatchExe me(t, finals);
  45.   MatchState ms;
  46.   ms.init(me.getInitial());
  47.   ms.step(L'b');
  48.   ms.step(L'e');
  49.   ms.step(L'e');
  50.   ms.step(L'r');
  51.   int val = ms.classifyFinals(me.getFinals());
  52.   fwprintf(stdout, L"%d\n", val);
  53. /*
  54.   for(wstring::iterator it = str.begin(); it != str.end(); it++)
  55.   {
  56.     ms.step(*it);
  57.     wcout << *it << endl;
  58.   }
  59.   int val = ms.classifyFinals(me.getFinals());
  60.   fwprintf(stdout, L"%d\n", val);
  61. */
  62. }
  63.  
  64. int main (int argc, char** argv)
  65. {
  66.   Alphabet alphabet;
  67.   Transducer t;
  68.   RegexpCompiler re;
  69.   bool matched;
  70.  
  71.   LtLocale::tryToSetLocale();
  72.  
  73.   if(argc < 3)
  74.   {
  75.     wcout << L"Usage: lt-regexp <pattern> <string to match>" << endl;
  76.     exit(-1);
  77.   }
  78.  
  79.   FILE *output = stdout;
  80.   wstring pattern = ws(argv[1]);
  81.   wstring s = ws(argv[2]);
  82.  
  83.  
  84.   re.initialize(&alphabet);
  85.   re.compile(pattern);
  86.   t = re.getTransducer();
  87.   t.minimize();
  88.  
  89.   t.show(alphabet, output);
  90.  
  91.   matched = match(t, s, alphabet);
  92.  
  93.   wcout << endl << pattern << " " << s << endl;
  94. }
Add Comment
Please, Sign In to add comment