Advertisement
HjHimansh

Regex Parser - Pramp

Aug 17th, 2020
3,694
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6.  
  7.  
  8. bool isMatch( const string &text, const string &pattern, int textStart = 0, int patternStart = 0)
  9. {
  10.  
  11.   if(textStart >= text.size()){
  12.     if(patternStart >= pattern.size()){
  13.       return true;
  14.     }else if(patternStart+1 < pattern.size() && pattern[patternStart+1] == '*'){
  15.       //i.e text is empty and it's a kleene closure...
  16.       return isMatch(text, pattern, textStart+1, patternStart+2);
  17.     }else{
  18.       return false;
  19.     }
  20.   }
  21.  
  22.   bool current_check = false;
  23.    
  24.   if(patternStart+1 < pattern.size() && pattern[patternStart+1] == '*'){
  25.     //it's a kleene closure...
  26.     //skip
  27.     bool f = isMatch(text, pattern, textStart, patternStart+2);
  28.     bool s = false, t = false;
  29.     if(pattern[patternStart] != '.' && pattern[patternStart] == text[textStart]){
  30.       s = isMatch(text, pattern, textStart+1, patternStart);
  31.     }else if(pattern[patternStart] == '.'){
  32.       t = isMatch(text, pattern, textStart+1, patternStart);
  33.     }
  34.     return f || s || t;
  35.    
  36.    
  37.   }else{
  38.     if(pattern[patternStart] != '.' && pattern[patternStart] == text[textStart]){
  39.       current_check = true;
  40.       return current_check && isMatch(text, pattern, textStart+1, patternStart+1);
  41.     }else if(pattern[patternStart] == '.'){
  42.       current_check = true;
  43.       return current_check && isMatch(text, pattern, textStart+1, patternStart+1);
  44.     }else{
  45.       current_check = false;
  46.       return false;
  47.     }
  48.   }
  49. }
  50. // (ab)2(a(cd)3)2
  51. // ababacdcdcdacdcdcd
  52. int main() {
  53.   return 0;
  54. }
  55.  
  56.  
  57.  
  58. /*
  59. if(textStart == text.size()){
  60.     if(patternStart == pattern.size()){
  61.       return true;
  62.     }else if(patternStart+1 < pattern.size() && pattern[patternStart+1] == '*'){
  63.       return isMatch(text, pattern, textStart, patternStart+2);
  64.     }else{
  65.       return false;
  66.     }
  67.   }
  68.  
  69.   if(patternStart == pattern.size()){
  70.     return false;
  71.   }
  72.  
  73.   bool answer = false;
  74.  
  75.   if(patternStart+1 < pattern.size() && pattern[patternStart+1] == '*'){
  76.     //i.e it kleene closure.
  77.     if(pattern[patternStart] == '.' || pattern[patternStart] == text[textStart]){
  78.       answer |= isMatch(text, pattern, textStart+1, patternStart);
  79.     }
  80.     answer |= isMatch(text, pattern, textStart, patternStart+2);
  81.    
  82.   }else if(text[textStart] == pattern[patternStart] || pattern[patternStart] == '.'){
  83.      //it is not a kleene closure...
  84.      answer |= isMatch(text, pattern, textStart+1, patternStart+1);
  85.   }
  86.  
  87.   return answer;
  88. */
Advertisement
RAW Paste Data Copied
Advertisement