Guest User

Untitled

a guest
Jan 21st, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. void AddEpsilonTransition(int left, int right)
  2. {
  3. pairStates temp;
  4. temp.leftState = left;
  5. temp.rightState = right;
  6. epsilonTransitions.Add(temp);
  7. }
  8.  
  9. void AddTransition(int left, int right, char c)
  10. {
  11. Transitions.Add(left.ToString() + "," + c, right);
  12. }
  13.  
  14. int newState()
  15. {
  16. this.numStates++;
  17. return this.numStates - 1;
  18. }
  19.  
  20. bool isOperator(string expression, int i)
  21. {
  22. int num = 0;
  23. while(i - 1 >= 0 && expression[i-1] == '\\')
  24. {
  25. num++;
  26. i--;
  27. }
  28. return num % 2 == 0;
  29. }
  30.  
  31. int findPar(string expression, int i)
  32. {
  33. int num = 0;
  34. for (; i < expression.Length; i++)
  35. {
  36. if (expression[i] == '(') num++;
  37. else if (expression[i] == ')') num--;
  38. if (num == 0) return i;
  39. }
  40. return -1;
  41. }
  42.  
  43. pairStates convert(string expression)
  44. {
  45. List<string> selection = new List<string>();
  46. int numPar = 0;
  47. int j = 0;
  48. int i = 0;
  49. for (; i < expression.Length; i++)
  50. {
  51. if (expression[i] == '(' && isOperator(expression, i)) numPar++;
  52. else if (expression[i] == ')' && isOperator(expression, i)) numPar--;
  53. else if(numPar == 0 && expression[i] == '|' && isOperator(expression,i))
  54. {
  55. selection.Add(expression.Substring(j,i-j));
  56. j = i + 1;
  57. }
  58. }
  59. if (selection.Count() > 0) selection.Add(expression.Substring(j, i - j));
  60.  
  61. int leftState = newState();
  62. int rightState = newState();
  63.  
  64. if (selection.Count() > 0)
  65. for (i = 0; i < selection.Count; i++)
  66. {
  67. pairStates temp = convert(selection[i]);
  68. AddEpsilonTransition(leftState, temp.leftState);
  69. AddEpsilonTransition(temp.rightState, rightState);
  70. }
  71. else
  72. {
  73. bool prefix = false;
  74. int lastState = leftState;
  75. for (i = 0; i < expression.Length; i++)
  76. {
  77. int a, b;
  78. if (prefix == true)
  79. {
  80. prefix = false;
  81. char transition;
  82. if (expression[i] == 't') transition = '\t';
  83. else if (expression[i] == 'n') transition = '\n';
  84. else if (expression[i] == '_') transition = ' ';
  85. else transition = expression[i];
  86. a = newState();
  87. b = newState();
  88. AddTransition(a, b, transition);
  89. }
  90. else
  91. {
  92. if (expression[i] == '\\')
  93. {
  94. prefix = true;
  95. continue;
  96. }
  97. if (expression[i] != '(')
  98. {
  99. a = newState();
  100. b = newState();
  101. if( expression[i] == '$') AddEpsilonTransition(a,b);
  102. else AddTransition(a,b,expression[i]);
  103. }
  104. else
  105. {
  106. j = findPar(expression,i);
  107. pairStates temp = convert(expression.Substring(i+1,j-i-1));
  108. a = temp.leftState;
  109. b = temp.rightState;
  110. i = j;
  111. }
  112. }
  113. if (i + 1 < expression.Length && expression[i + 1] == '*')
  114. {
  115. int x = a;
  116. int y = b;
  117. a = newState();
  118. b = newState();
  119. AddEpsilonTransition(a,x);
  120. AddEpsilonTransition(y, b);
  121. AddEpsilonTransition(a, b);
  122. AddEpsilonTransition(y, x);
  123. i++;
  124. }
  125. AddEpsilonTransition(lastState, a);
  126. lastState = b;
  127. }
  128. AddEpsilonTransition(lastState, rightState);
  129. }
  130. pairStates temp2;
  131. temp2.leftState = leftState;
  132. temp2.rightState = rightState;
  133. return temp2;
  134. }
Add Comment
Please, Sign In to add comment