-
Notifications
You must be signed in to change notification settings - Fork 0
/
LexicalAnalyser.java
182 lines (140 loc) · 4.8 KB
/
LexicalAnalyser.java
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package task2;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class test {
private static boolean isKeyword(String st){
List<String> keywords = Arrays.asList("if","then","end","while","do","return","else","else if","print");
for (String key : keywords) {
if (st.equalsIgnoreCase(key))
return true;
}
return false;
}
private static boolean isDatatype(String st){
List<String> dataType = Arrays.asList("int","float","string","long","char");
for (String data : dataType) {
if (st.equalsIgnoreCase(data)) {
return true;
}
}
return false;
}
public static String isSymbol(String st){
if (st.matches("[-+*/=<>!][=]|[-+*/(){}=;]")){
switch (st){
case "(":
return "Open_parenthesis";
case ")":
return "Close_parenthesis";
case "{":
return "Open_brace";
case "}":
return "Close_brace";
case "=":
return "Equal_sign";
case"==":
return "Equal_Equal_sign";
case "+":
return "Plus_op";
case "-":
return "Minus_op";
case "*":
return "Multi_op";
case "/":
return "Divide_op";
case "<":
return "less_than";
case ">":
return "Greater_than";
case "<=":
return "less_than or Equal";
case ">=":
return "Greater_than or Equal";
case "!=":
return "Not_Equal";
case ";":
return "Semicolon";
}
}
return "";
}
public static boolean isDigit(String st){
if(st.matches("[\\s-+*/=(]?([0-9]*[.])?[0-9]+[\\s-+*/;]")) {
return true;
}
return false;
}
private static boolean isId(String st){
if (st.matches("[a-zA-Z]+[\\s-+*/=);]|[a-zA-Z]+[0-9]+[\\s-+*/=);]"))
return true;
return false;
}
private static boolean escapeChars(char ch) {
char[] escapeChars = {'<', '(', '[', '{', '\\', '^', '$', '!', '|', ']', '}', ')', '>', ';', ' ','=','$'};
for (char e : escapeChars) {
if (ch == e) {
return true;
}
}
return false;
}
private static void readFromFile()throws Exception{
Parse parser;
File file = new File("task.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "UTF-8"));
int c;
String temp = "";
char ch;
while((c = bufferedReader.read()) != -1){
ch = (char)c;
temp += ch;
if(isKeyword(temp)){
parser=new Parse(temp);
temp="";
}
if(isId(temp) ){
String s = Character.toString(temp.charAt(temp.length() - 1));
String m = temp.substring(0,temp.length()-1);
parser=new Parse(m);
if(!isSymbol(s).isEmpty()) {
parser=new Parse(s);
}
temp="";
}
if(!isSymbol(temp).isEmpty()){
parser=new Parse(temp);//i*i+(i+i)$
temp="";
}
if(isDigit(temp)){
String intORFloat = "";
int x;
try{
x = Integer.parseInt(temp.substring(0,temp.length()-1));
intORFloat += "Int_literal";
}catch (NumberFormatException n){
intORFloat += "Float_literal";
}
parser=new Parse("num");
String s = Character.toString(temp.charAt(temp.length() - 1));
if(!isSymbol(s).isEmpty()) {
parser=new Parse(temp.substring(temp.length() - 1));//i*i+(i+i)$
}
temp="";
}
if(escapeChars(ch)){
temp = "";
}
if(ch == '\n'){
temp = "";
}
}
}
public static void main(String[] args) throws Exception {
readFromFile();
}
}