-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax.py
More file actions
executable file
·173 lines (149 loc) · 4.37 KB
/
syntax.py
File metadata and controls
executable file
·173 lines (149 loc) · 4.37 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
class SyntaxisAnalyzer:
def __init__(self, lexeme_list, table):
self.lexeme_list = lexeme_list
self.index = 0
self.lexem = None
self.table = table
self.error_code = 0
def extract_next_lexeme(self):
self.lexem = self.lexeme_list.pop()
def get_next_lexeme(self):
return self.lexeme_list[-1]
def eq(self, str):
if self.table[self.lexem[0]-1][self.lexem[1]] == str:
return True
else:
return False
def error(self, index):
self.error_code = index
if index == 1:
raise Exception("Missing ':'")
elif index == 2:
raise Exception("Missing type of variable")
elif index == 3:
raise Exception("Missing ';'")
elif index == 4:
raise Exception("Missing begin keyword")
elif index == 5:
raise Exception("Missing end keyword")
elif index == 6:
raise Exception("Missing ')'")
elif index == 7:
raise Exception("Missing ']'")
def id(self):
return self.lexem[0] == 4
def num(self):
return self.lexem[0] == 3
def identifier(self):
return self.lexem[0] == 4
def number(self):
pass
def description(self):
while self.identifier():
self.extract_next_lexeme()
if self.eq(','):
self.extract_next_lexeme()
if not self.eq(':'):
self.error(1)
else:
self.extract_next_lexeme()
if not (self.eq('integer') or self.eq('real')):
self.error(2)
else:
self.extract_next_lexeme()
if not self.eq(';'):
self.error(3)
def begin(self):
if not self.eq('begin'):
self.error(4)
else:
self.extract_next_lexeme()
def operators(self):
while self.assign():
pass
def operator(self):
pass
def end(self):
if not self.eq('end'):
self.error(5)
else:
self.extract_next_lexeme()
def program(self):
if self.eq('program'):
self.extract_next_lexeme()
self.identifier()
self.extract_next_lexeme()
if not self.eq(';'):
self.error(3)
else:
self.extract_next_lexeme()
if self.eq('var'):
self.extract_next_lexeme()
self.description()
self.extract_next_lexeme()
self.begin()
self.operators()
self.end()
def assign(self):
self.variable()
self.extract_next_lexeme()
if self.eq(':='):
self.extract_next_lexeme()
self.expression()
else:
return False
return True
def expression(self):
self.term()
self.extract_next_lexeme()
while self.eq('+') or self.eq('-'):
self.extract_next_lexeme()
self.term()
return True
def term(self):
self.primary()
while self.eq('*') or self.eq('/'):
self.extract_next_lexeme()
self.primary()
return True
def primary(self):
if self.id():
self.variable()
elif self.num():
self.number()
elif self.eq('('):
self.extract_next_lexeme()
self.expression()
if self.eq(')'):
self.extract_next_lexeme()
else:
self.error(6)
else:
return False
return True
def variable(self):
self.identifier()
if self.eq('['):
self.extract_next_lexeme()
self.subList()
if self.eq(']'):
self.extract_next_lexeme()
else:
self.error(7)
def subList(self):
self.expression()
while self.eq(','):
self.extract_next_lexeme()
self.expression()
def run_analysis(self):
try:
self.index = 0
self.extract_next_lexeme()
self.program()
except Exception as e:
print("Error: " + str(e))
finally:
if self.error_code == 0:
print('Syntax analysis finished successfully!')
else:
print('Syntax analysis finished with errors!')