-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlexic.py
More file actions
175 lines (153 loc) · 4.85 KB
/
lexic.py
File metadata and controls
175 lines (153 loc) · 4.85 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
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
from collections import namedtuple
import syntacticanalyzer
node = namedtuple("node", "Lexeme Token")
c = node("start", "start")
d = node("varstart", "varstart")
e = node("varend", "varend")
f = node("write", "write")
g = node("read", "read")
h = node("if", "if")
i = node("then", "then")
j = node("endif", "endif")
k = node("end", "end")
l = node("integer", "integer")
m = node("then", "then")
n = node("literal", "literal")
o = node("real", "real")
simbleTable = [c, d, e, f, g, h, i, j, k, l, m, n, o]
tokens = []
lexemes = []
def lexic(line):
i = 1
if(line[0].isalpha() == True):
while(len(line) < i or line[i].isalpha() == True or line[i].isdigit() == True or line[i] == "_"):
i = i + 1
createNode(line[:i], "id")
syntacticanalyzer.__settype__("id", len(tokens))
if(line[i:] != ""):
if(line[i] == " "):
i += 1
lexic(line[i:])
elif(line[0].isdigit() == True):
while(len(line) < i or line[i].isdigit() == True or line[i] == "E" or line[i] == "."):
i = i + 1
createNode(line[:i], "num")
syntacticanalyzer.__settype__("num", len(tokens))
if (line[i:] != ""):
if (line[i] == " "):
i += 1
lexic(line[i:])
elif(line[0] == "("):
createNode(line[:1], "ab_p")
syntacticanalyzer.__settype__("(", len(tokens))
if (line[1:] != ""):
lexic(line[1:])
elif(line[0] == ")"):
createNode(line[:1], "fc_p")
syntacticanalyzer.__settype__(")", len(tokens))
if (line[1:] != ""):
lexic(line[1:])
elif(line[0] == "+" or line[0] == "-" or line[0] == "/" or line[0] == "*"):
aux = line[0]
createNode(line[:1], "opm")
syntacticanalyzer.__settype__(aux, len(tokens))
if (line[1:] != ""):
lexic(line[1:])
elif(line[0] == ";"):
createNode(line[:1], "pt_v")
syntacticanalyzer.__settype__(";", len(tokens))
if (line[1:] != ""):
lexic(line[1:])
elif(line[0] == "<" and line[1] == "-"):
createNode(line[:2], "rcb")
syntacticanalyzer.__settype__("=", len(tokens))
if (line[2:] != ""):
lexic(line[2:])
elif(line[0] == "<" and line[1] == "="):
aux = line[0]
aux = aux + line[1]
createNode(line[:2], "opr")
syntacticanalyzer.__settype__(aux, len(tokens))
if (line[2:] != ""):
lexic(line[2:])
elif(line[0] == ">" and line[1] == "="):
aux = line[0]
aux = aux + line[1]
createNode(line[:2], "opr")
syntacticanalyzer.__settype__(aux, len(tokens))
if (line[2:] != ""):
lexic(line[2:])
elif(line[0] == "<" and line[1] == ">"):
aux = line[0]
aux = aux + line[1]
createNode(line[:2], "opr")
syntacticanalyzer.__settype__(aux, len(tokens))
if (line[2:] != ""):
lexic(line[2:])
elif(line[0] == "<" or line[0] == ">" or line[0] == "="):
aux = line[0]
createNode(line[:1], "opr")
syntacticanalyzer.__settype__(aux, len(tokens))
if (line[1:] != ""):
lexic(line[1:])
elif(line[0] == '"'):
while(len(line) < i or line[i] != '"'):
i = i + 1
createNode(line[:i], "literal")
syntacticanalyzer.__settype__("literal", len(tokens))
i += 1
if (line[i:] != ""):
if (line[i] == " "):
i += 1
lexic(line[i:])
elif(line[0] == ""):
createNode("", "EOF")
syntacticanalyzer.__settype__("EOF", len(tokens))
elif (line[0] == "$"):
createNode("$", "$")
syntacticanalyzer.__settype__("$", len(tokens))
elif line[0] == "\n":
print("Ignoring linebreak")
else:
print(f"Error. Invalid token {line[0]}")
def createNode(lexeme, token):
new = node(lexeme, token)
newrsv = node(lexeme, lexeme)
check = nodeExists(new, newrsv)
if (check == 2):
tokens.append(lexeme)
lexemes.append(lexeme)
else:
simbleTable.append(new)
tokens.append(token)
lexemes.append(lexeme)
def __gettoken__(i):
return tokens[i]
def __getlexeme__(i):
return lexemes[i]
def __settoken__(token, i):
tokens[i] = token
def __setlexeme__(lexeme, i):
lexemes[i] = lexeme
def __getindex__(lexeme):
return lexemes.index(lexeme)
def nodeExists(node, nodersv):
i = 0
while (len(simbleTable) > i):
if(nodersv == simbleTable[i]):
return 2
elif (node == simbleTable[i]):
return 1
i = i + 1
return 0
def __init__():
file = open("text.alg", "r")
line = file.readline()
while(line != ""):
lexic(line)
line = file.readline()
if(line == ""):
createNode("$", "EOF")
file.close()
for p in simbleTable:
print(p)