-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathECSyntaxChecker.java
More file actions
155 lines (124 loc) · 5.58 KB
/
Copy pathECSyntaxChecker.java
File metadata and controls
155 lines (124 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package cmsc141.mp1.ec;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ECSyntaxChecker {
private static String comment = "(\\s*/\\*\\p{Blank}+(.)*\\p{Blank}+\\*/\\s*)*";
private static String lower_alpha = "[a-z]";
private static String upper_alpha = "[A-Z]";
private static String number = "[0-9]";
private static String string = "'.*'";
private static String constant = "-?" + number + "+(\\." + number + "+)?";
private static String word = "(@" + lower_alpha + "(" + upper_alpha + "|" + lower_alpha + "|" + number + ")*)";
private static String term = "(" + constant + "|" + word + ")";//"(" + constant + "|" + string + ")";
private static String hi_order_op = "(/|\\*|%)";
private static String arith_op = "(\\+|-|" + hi_order_op + ")";
private static String arithmetic = term + "\\p{Blank}+" + arith_op + "\\p{Blank}+" + term;
private static String print = "(print\\p{Blank}+(" + word + "|" + string + ")(\\s+\\+\\s+" + "(" + word + "|" + string + "))*)";
private static String print_newline = "(puts\\p{Blank}+(" + word + "|" + string + ")(\\s+\\+\\s+" + "(" + word + "|" + string + "))*)";
private static String scanner = "scan\\p{Blank}+" + word;
private static String operation = arithmetic;
private static String assignment = word + "\\p{Blank}+=\\p{Blank}+(" + operation + "|" + word + "|" + constant + "|" + string + ")";
private static String sentence = "((" + assignment + "|" + operation + "|" + comment + "|" +
print + "|" + print_newline + "|" + scanner + ")\\s+)*";
private static String expression = "(" + word + "|" + constant + "|" + string + ")";
private static String condition = "(not\\p{Blank}+)?" + expression + "\\p{Blank}+(and|or|==|!=|<|>|<=|>=)\\p{Blank}+" + expression;
private static String iteration = "((for\\p{Blank}+" + assignment + "\\p{Blank}+;\\p{Blank}+" + condition + "\\p{Blank}+;\\p{Blank}+(" + operation +
"|" + assignment + ")*\\s+do\\s+" + sentence + "end\\s+)|(" +
"while\\p{Blank}+" + condition + "\\s+do\\s+" + sentence + "end\\s+)|(" +
"do\\s+" + sentence + "while\\p{Blank}+" + condition + "\\p{Blank}+end\\s+))*";
private static String conditional = "(if\\p{Blank}+" + condition + "\\s+do\\s+" + sentence +
"(else if\\p{Blank}+" + condition + "\\s+do\\s+"+ sentence +")*(else\\s+do\\s+" + sentence + ")?end\\s+)";
private static String stmt_block = "(" + conditional + "|" + iteration + "|" + comment + ")";
private static String paragraph = "((" + sentence + "|" + stmt_block + ")*)";
private static String main = "(^\\s*main\\s+do\\s+" + paragraph + "end\\s*$)";
private static String program = main;
private Pattern pattern;
private Matcher matcher;
public String getMatchedPattern(String testString) {
String matchedString = "oops";
while (match(testString)) {
matchedString += matcher.group(1);
}
System.out.println("editedString = "+matchedString);
return matchedString;
}
private Pattern compile(String regex) {
pattern = Pattern.compile(regex);
return pattern;
}
public boolean match(String stringPattern) {
RegularExpressionUtils regularExpressionUtils =
new RegularExpressionUtils();
matcher =
regularExpressionUtils.createMatcherWithTimeout(
stringPattern, pattern, 5000);
return matcher.find();
}
public ECSyntaxChecker(boolean isTest) {
pattern = compile(program);
}
public boolean hasMatch(String testString) {
return match(testString);
}
public ECSyntaxChecker() {
pattern = compile(program);
}
class RegularExpressionUtils {
public Matcher createMatcherWithTimeout(String stringToMatch, String regularExpression, int timeoutMillis) {
Pattern pattern = Pattern.compile(regularExpression);
return createMatcherWithTimeout(stringToMatch, pattern, timeoutMillis);
}
public Matcher createMatcherWithTimeout(String stringToMatch, Pattern regularExpressionPattern, int timeoutMillis) {
CharSequence charSequence = new TimeoutRegexCharSequence(stringToMatch, timeoutMillis, stringToMatch,
regularExpressionPattern.pattern());
return regularExpressionPattern.matcher(charSequence);
}
private class TimeoutRegexCharSequence implements CharSequence {
private final CharSequence inner;
private final int timeoutMillis;
private final long timeoutTime;
private final String stringToMatch;
private final String regularExpression;
public TimeoutRegexCharSequence(CharSequence inner, int timeoutMillis, String stringToMatch, String regularExpression) {
super();
this.inner = inner;
this.timeoutMillis = timeoutMillis;
this.stringToMatch = stringToMatch;
this.regularExpression = regularExpression;
timeoutTime = System.currentTimeMillis() + timeoutMillis;
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public int indexOfLastMatch(Pattern pattern, String input) {
for (int i = input.length(); i > 0; --i) {
Matcher region = matcher.region(0, i);
if (region.matches() || region.hitEnd()) {
return i;
}
}
return 0;
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
@Override
public char charAt(int index) {
if (System.currentTimeMillis() > timeoutTime) {
return 0;
}
return inner.charAt(index);
}
@Override
public int length() {
return inner.length();
}
@Override
public CharSequence subSequence(int start, int end) {
return new TimeoutRegexCharSequence(
inner.subSequence(start, end), timeoutMillis,
stringToMatch, regularExpression);
}
@Override
public String toString() {
return inner.toString();
}
}
}
}