-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexical.java
More file actions
128 lines (125 loc) · 5.51 KB
/
lexical.java
File metadata and controls
128 lines (125 loc) · 5.51 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
import java.io.*;
import java.util.*;
public class lexical {
int operators(char w, char x, char y, char[] ope, int k){
if(x=='+' || x=='-' || (x=='/' && y!='/' && w!='/') || x=='*' || x=='%'|| x=='='){
ope[k]=x;
k++;
}
return k;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
ArrayList<String> keywords = new ArrayList<>(Arrays.asList(
"abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class",
"continue", "default", "do", "double", "else", "enum", "extends", "final", "finally",
"float", "for", "if", "implements", "import", "instanceof", "int", "interface", "long",
"native", "new", "package", "private", "protected", "public", "return", "short", "static",
"strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "transient", "try",
"void", "volatile", "while"
));
String s,key="";
try {
File Obj = new File("code.txt");
Scanner Reader = new Scanner(Obj);
char[] ope=new char[20];
String[] var=new String[20];
String digit1="", digit="", command="", spl="", pre="",str="",comm="";
int k=0,d=0,t=0;
lexical obj=new lexical();
while (Reader.hasNextLine()) {
String data = Reader.nextLine();
String[] words = data.split("\\s+");
System.out.println(data+" ");
char[] arr=data.toCharArray();
for(int j=0;j<arr.length;j++){
if(j!=arr.length-1 && j!=0){
k=obj.operators(arr[j-1],arr[j],arr[j+1],ope,k);
if(arr[j]=='/' && arr[j+1]=='/'){
command=data.substring(j+2);
comm+=command+", ";
}
}
if(arr[j]=='.' || arr[j]==',' || arr[j]==';' || arr[j]=='(' || arr[j]==')' || arr[j]=='{' || arr[j]=='}'){
spl+=arr[j]+" ";
}
if(arr[j]=='"' && arr[j+1]!=')'){
for(int l2=j+1;l2<arr.length;l2++){
if(arr[l2]=='"'){
str+=",";
break;}
str+=arr[l2];
}
}
if(Character.isDigit(arr[j]) && (arr[j-1]=='=' || arr[j-1]==' ' || arr[j-1]=='/' ||arr[j-1]=='*' ||arr[j-1]=='+' ||arr[j-1]=='-' ||arr[j-1]=='%')){
digit1+=arr[j];
for(int i=j+1;i<arr.length;i++){
if(Character.isDigit(arr[i])||arr[i]=='.'){
digit1+=arr[i];
if(arr[i+1]==';'){digit1+=",";break;}
}
else if(arr[i]==' '){
digit1+=",";
break;
}
}
}
if(arr[j]=='@'){
pre+=data.substring(j,arr.length)+", ";
}
if(arr[j]=='=' ||((arr[j]=='=') && (arr[j-1]==' '))){
int tem1=j-1;
do{
tem1--;
}while(arr[tem1]!=' ');
var[t]=data.substring(tem1+1, j);
t++;
}
digit+=digit1;
str+=digit1;
digit1="";
}
for(String word: words){
for(String i: keywords){
if(word.equals(i)){
key+= word+ ", ";
//System.out.println(word);
break;
}
}
}
}
System.out.println("Tokens :");
System.out.print("Lexeme of Operators : ");
for(int l=0;l<k;l++){
if(l==k-1){
System.out.println(ope[l]);
}else{
System.out.print(ope[l]+", ");
}
}
System.out.println("Operators count : "+ k);
System.out.print("Lexeme of Variables : ");
for(int l1=0;l1<t;l1++){
if(l1==t-1){
System.out.println(var[l1]);
}else{
System.out.print(var[l1]+", ");
}
}
System.out.print("Lexeme of Constants :");
System.out.println(digit.substring(0,(digit.length()-1)));
System.out.println("Lexeme of Keywords :"+ key.substring(0,(key.length()-2)));
System.out.println("Lexeme of Literals :"+ str.substring(0,(str.length()-1)));
System.out.println("Lexeme of Special Characters/ Delimiters : "+ spl);
System.out.println("Non-Tokens :");
System.out.println("Lexeme of Comments :"+ comm.substring(0,(comm.length()-2)));
System.out.println("Lexeme of Preprocessors :"+ pre.substring(0,(pre.length()-2)));
Reader.close();
}
catch (FileNotFoundException e) {
System.out.println("An error has occurred.");
e.printStackTrace();
}
}
}