-
Notifications
You must be signed in to change notification settings - Fork 1
/
jLex.py
174 lines (164 loc) · 4.47 KB
/
jLex.py
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
#!/bin/python3
## Lexical Analyzer for JAVA in python
## - ©️ yashoswalyo <[email protected]>
#
from ast import arg
import sys
class LexicalAnalyzer():
def __init__(self,filePath:str):
self.filePath = filePath
self.input = open(filePath,mode="r")
self.output = open("lexTable.txt","w")
self.symbols = open("symbolTable.txt","w")
self.KEYWORDS = [
"abstract", "continue", "for", "new", "switch",
"assert", "default", "goto", "package", "synchronized",
"boolean", "do", "if", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "strictfp", "volatile",
"const", "float", "native", "super", "while"
]
self.OPERATORS = [
'+',
'-',
'/',
'*',
'=',
'%'
]
self.DELIMITERS = [
'(',
')',
'{',
'}',
'.',
',',
':',
';',
'#',
'[',
']',
'"'
# ' '
]
self.lexeme = [[0,"","",""]]
self.IDENTIFIER = []
def generateLexicalTable(self)->None:
LexicalAnalyzer.tokenize(self)
self.output.write("+--------+----------+------------+---------------+\n")
self.output.write("| Line | Lexeme | Token | Token Value |\n")
self.output.write("+--------+----------+------------+---------------+\n")
for i in range(1,len(self.lexeme)):
self.output.write("| {:<5}| {:<9}| {:<11}| {:<11}|\n".format(self.lexeme[i][0]+1,self.lexeme[i][1],self.lexeme[i][2],self.lexeme[i][3]))
# print("|---------------------------------------------|")
self.output.write("+--------+----------+------------+---------------+\n")
return self.lexeme, self.IDENTIFIER
def generateSymbolTable(self):
try:
self.symbols.write("+--------+----------+\n")
self.symbols.write("| Sr.NO. | Symbol |\n")
self.symbols.write("+--------+----------+\n")
for i in self.IDENTIFIER:
self.symbols.write("| {:<5} | {:<9}|\n".format(self.IDENTIFIER.index(i)+1,i))
self.symbols.write("+--------+----------+")
except Exception as e:
print(e)
def tokenize(self):
count_op = 0
count_dl = 0
lines = self.input.readlines()
lc=0
for line in lines:
base = 0
index = 0
for char in line:
if char in self.OPERATORS:
count_op += 1
self.lexeme.append(
[
lc,
char,
"OPERATOR",
"(OP,{:02d})".format(self.OPERATORS.index(char)+1)
]
)
elif char in self.DELIMITERS or char == ' ' :
LexicalAnalyzer.check_keys(self,base,index,line,lc)
if char in self.DELIMITERS:
count_dl += 1
self.lexeme.append(
[
lc,
char,
"DELIMITER",
"(DL,{:02d})".format(self.DELIMITERS.index(char)+1)
]
)
base = index+1
index+=1
lc+=1
def check_keys(self,base:int,index:int,line:str,lc:int):
if line[base:index] in self.KEYWORDS:
self.lexeme.append(
[
lc,
line[base:index],
"KEYWORD",
"(KW,{:02d})".format(self.KEYWORDS.index(line[base:index])+1)
]
)
elif line[base:index].isdigit():
self.lexeme.append(
[
lc,
line[base:index],
"CONSTANT",
"(C,{:02d})".format(int(line[base:index]))
]
)
elif line[base:index] not in self.OPERATORS and line[base:index] != "\n" and line[base] not in self.DELIMITERS:
id=0
if len(self.IDENTIFIER) > 0:
if line[base:index] in self.IDENTIFIER:
id = self.IDENTIFIER.index(line[base:index])
else:
self.IDENTIFIER.append(line[base:index])
id = self.IDENTIFIER.index(line[base:index])
else:
self.IDENTIFIER.append(line[base:index])
id = self.IDENTIFIER.index(line[base:index])
self.lexeme.append(
[
lc,
line[base:index],
"IDENTIFIER",
"(ID,{:02d})".format(id+1)
]
)
def banner():
print("yet")
if __name__ == "__main__":
args = sys.argv
try:
if len(args) > 1:
if args[1] == '-h' or args[1] == '--help':
print("""\n\nUsage:-
python3 jLex.py -i file_path
Commands:-
-i, --input <file_path> :- Path of input file
-h, --help :- Display help
""")
elif args[1] == "-i" or args[1] == "--input":
lex = LexicalAnalyzer(filePath=args[2])
lex.generateLexicalTable()
lex.generateSymbolTable()
else:
print("[-] Arguments not recognized\n Use -h or --help for help")
else:
print("[-] No Arguments passed\n Use -h or --help for help")
except Exception as e:
print(f"[-] {e}")