-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexicalAnal.java
More file actions
83 lines (70 loc) · 3.36 KB
/
Copy pathLexicalAnal.java
File metadata and controls
83 lines (70 loc) · 3.36 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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.StreamTokenizer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Stack;
public class LexicalAnal {
public static void main(String[] args) throws Exception {
int x = 100;
FileReader fileReader = null;
BufferedReader bufferedReader = null;
try {
fileReader = new FileReader("D:\\hello.txt");
bufferedReader = new BufferedReader(fileReader);
StreamTokenizer st = new StreamTokenizer(bufferedReader);
boolean eof = false;
String[] keys={"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"printf",
"unsigned","void","volatile","while","class","public","main","String","System.out.println"};
char[] oper={
'!','%','^','&','*','-','+','=','~','|','.','<','>','/','?'
};
do {
int token = st.nextToken();
switch (token) {
case StreamTokenizer.TT_EOF:
System.out.println("End of File encountered");
eof = true;
break;
case StreamTokenizer.TT_WORD:
int count=0;
for(int i=0;i<keys.length;i++){
if(keys[i].equals(st.sval)){
System.out.println(st.sval+" is a keyword");
count++;
break;
}
}
if(count==0){
ArrayList<String> list = new ArrayList<>();
list.add(st.sval);
System.out.println(st.sval+" is an indentifier");
System.out.println(list+" position: "+x++);
}
break;
case StreamTokenizer.TT_NUMBER:
System.out.println("Number: " + st.nval);
break;
default:
int counter=0;
for(int i=0;i<oper.length;i++){
if(((char) token)==oper[i] ){
System.out.println((char) token +" is an operatior");
counter++;
break;
}
}
if(counter==0){
System.out.println((char)token +" is encountered");
}
}
} while (!eof);
} catch (Exception e) {
System.out.print("");
}
}
}