forked from arleenchr/IF2124-Tugas-Besar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
64 lines (56 loc) · 2.13 KB
/
lexer.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
import re #regex
import sys
from token import list_token
from fa import check_variabel_name
from fa import *
#from colorama import Back, Style
def red() :
print("\033[31m", end='')
def green() :
print("\033[32m", end='')
def reset() :
print("\033[0m", end='')
def lexer(inputText,listToken):
# Membuat lexer untuk parsing program js
# Contoh: "int a = 3;" di-split menjadi 5 lexemes: "int, a, =, 3, ;"
currentChar = 0 #indeks yang menunjuk tiap karakter dalam input teks
currentLine = 1 #indeks yang menunjuk baris dalam input teks
lexemes = []
while (currentChar<len(inputText)):
if (inputText[currentChar]=='\n'):
currentLine += 1 # jika baris baru, indeks penunjuk currentLine bertambah
for tkn in listToken:
# iterasi semua elemen di list_token
regexPattern, token = tkn
regex = re.compile(regexPattern) # compile regex
match = regex.match(inputText,currentChar) # cari token yang match
if (match != None):
if token:
#print(match.group())
if (token=="INT") and (match.end(0)<len(inputText) and inputText[match.end(0)] != '\n'):
if (inputText[match.end(0)] in uppercase or inputText[match.end(0)] in lowercase):
match = None
if (token == "ID") and (not check_variabel_name(match.group())):
match = None
lexemes.append(token)
break
#print(match)
if (match == None):
#print("here")
#print(Back.CYAN + "Processing...", end='')
#print(Style.RESET_ALL)
#print()
red()
print("Syntax Error")
reset()
sys.exit()
else:
currentChar = match.end(0) # update currentChar menunjuk ke setelah suatu token
return lexemes
def tokenToStr(filename):
file = open(filename)
text = file.read()
file.close()
lexemes = lexer(text,list_token)
return " ".join(lexemes)
#print(tokenToStr('inputAcc.js'))