-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyntaxAnalyser.py
445 lines (352 loc) · 14.2 KB
/
SyntaxAnalyser.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# -*- coding: utf-8 -*-
""" **************************************************************************
FICHERO: SyntaxAnalyser.py
FECHA: 02.10.2011
AUTORES: Marcelino Aitor Concepcion Barandela
Andres Gonzalez Placeres
Joram Real Gómez
E-MAILS: alu0100220919@alumnado.ull.es
alu0100247671@alumnado.ull.es
alu0100226300@alumnado.ull.es
ASIGNATURA: Compiladores
DESCRIPCION: Practica 4
Diseño e implementacion de un analizador sintactico recursivo predictivo para PASCAL-
COMENTARIOS:
*************************************************************************** """
import sys
from LexicalAnalyser import LexicalAnalyser
from Tokens import T
class SyntaxAnalyser:
def __init__ (self, lexical_analyser):
T.load()
self.lexical_analyser = lexical_analyser
self.lookahead = self.lexical_analyser.yylex()
def peek(self):
return self.lookahead[0]
def syntax_error(self, expected):
print (expected + " was expected, but " + T.get_name(self.lookahead[0]) + " was found in line " + str(self.lookahead[3]))
sys.exit("The program is not valid.")
def match(self, token):
if self.peek() == token:
self.lookahead = self.lexical_analyser.yylex()
#print "Matched " + T.get_name(token)
else:
self.syntax_error(T.get_name(token))
# <Program> ::= program program_name ; <Block body> .
def program(self):
self.match(T.PROGRAM)
self.match(T.ID)
self.match(T.SEMICOLON)
self.block_body()
self.match(T.PERIOD)
# <Block body> ::= [<Constant definition part>] [<Type definition part>][<Variable definition part>] {<Procedure definition>}<Compound statement>
def block_body(self):
if self.peek() == T.CONST:
self.constant_definition_part()
if self.peek() == T.TYPE:
self.type_definition_part()
if self.peek() == T.VAR:
self.variable_definition_part()
while self.peek() == T.PROCEDURE:
self.procedure_definition()
self.compound_statement()
# <Constant definition part> ::= const <Constant definition> {<Constant definition>}
def constant_definition_part(self):
self.match(T.CONST)
self.constant_definition()
while self.peek() == T.ID:
self.constant_definition()
# <Constant definition> ::= constant_name = <Constant> ;
def constant_definition(self):
self.match(T.ID)
self.match(T.EQUAL)
self.constant()
self.match(T.SEMICOLON)
# <Type definition part> ::= type <Type definition> {<Type definition>}
def type_definition_part(self):
self.match(T.TYPE)
self.type_definition()
while self.peek() == T.ID:
self.type_definition()
# <Type definition> ::= type_name = <New type> ;
def type_definition(self):
self.match(T.ID)
self.match(T.EQUAL)
self.new_type()
self.match(T.SEMICOLON)
# <New type> ::= <New array type> | <New record type>
def new_type(self):
if self.peek() == T.ARRAY:
self.new_array_type()
elif self.peek() == T.RECORD:
self.new_record_type()
else:
self.syntax_error("ARRAY or RECORD")
# <New array type> ::= array ”[”<Index range> ”]”of type_name
def new_array_type(self):
self.match(T.ARRAY)
self.match(T.LEFTBRACKET)
self.index_range()
self.match(T.RIGHTBRACKET)
self.match(T.OF)
self.match(T.ID)
# <Index range> ::= <Constant> .. <Constant>
def index_range(self):
self.constant()
self.match(T.DOUBLEDOT)
self.constant()
# <New record type> ::= record <Field list> end
def new_record_type(self):
self.match(T.RECORD)
self.field_list()
self.match(T.END)
# <Field list> ::= <Record section> {; <Record section>}
def field_list(self):
self.record_section()
while self.peek() == T.SEMICOLON:
self.match(T.SEMICOLON)
self.record_section()
# <Record section> ::= field name {, field name} : type name
def record_section(self):
self.match(T.ID)
while self.peek() == T.COMMA:
self.match(T.COMMA)
self.match(T.ID)
self.match(T.COLON)
self.match(T.ID)
# <Variable definition part> ::= var <Variable definition> {<Variable definition>}
def variable_definition_part(self):
self.match(T.VAR)
self.variable_definition()
while self.peek() == T.ID:
self.variable_definition()
# <Variable definition> ::= <Variable group> ;
def variable_definition(self):
self.variable_group()
self.match(T.SEMICOLON)
# <Variable group> ::= variable_name {, variable_name} : type_name
def variable_group(self):
self.match(T.ID)
while self.peek() == T.COMMA:
self.match(T.COMMA)
self.match(T.ID)
self.match(T.COLON)
self.match(T.ID)
# <Procedure definition> ::= procedure procedure_name <Procedure block> ;
def procedure_definition(self):
self.match(T.PROCEDURE)
self.match(T.ID)
self.procedure_block()
self.match(T.SEMICOLON)
# <Procedure block> ::= [( <Formal parameter list> )] ; <Block body>
def procedure_block(self):
if self.peek() == T.LEFTPARENTHESIS:
self.match(T.LEFTPARENTHESIS)
self.formal_parameter_list()
self.match(T.RIGHTPARENTHESIS)
self.match(T.SEMICOLON)
self.block_body()
# <Formal parameter list> ::= <Parameter definition> {; <Parameter definition>}
def formal_parameter_list(self):
self.parameter_definition()
while self.peek() == T.SEMICOLON:
self.match(T.SEMICOLON)
self.parameter_definition()
# <Parameter definition> ::= [var] <Variable group>
def parameter_definition(self):
if self.peek() == T.VAR:
self.match(T.VAR)
self.variable_group()
# <Statement> ::= <Assignment statement> | <Procedure statement> | <If statement> | <While statement> | <Compound statement> j
# MODIFICADA : <Statement> ::= ID<Statement_2> | <If statement> | <While statement> | <Compound statement> | €
def statement(self):
if self.peek() == T.ID:
self.match(T.ID)
self.statement_2()
elif self.peek() == T.IF:
self.if_statement()
elif self.peek() == T.WHILE:
self.while_statement()
elif self.peek() == T.BEGIN:
self.compound_statement()
# NUEVA : <Statement_2> ::= [( <Actual parameter list> )] | <Assignment statement 2>
def statement_2(self):
if self.peek() == T.LEFTPARENTHESIS:
self.match(T.LEFTPARENTHESIS)
self.actual_parameter_list()
self.match(T.RIGHTPARENTHESIS)
elif self.peek() == T.LEFTBRACKET or \
self.peek() == T.PERIOD or \
self.peek() == T.BECOMES:
self.assignment_statement_2()
# <Assignment statement> ::= <Variable access> := <Expression>
def assignment_statement(self):
self.variable_access()
self.match(T.BECOMES)
self.expression()
# NUEVA : <Assignment statement 2> ::= {<Selector>} := <Expression>
def assignment_statement_2(self):
while self.peek() == T.LEFTBRACKET or \
self.peek() == T.PERIOD:
self.selector()
self.match(T.BECOMES)
self.expression()
# BORRADA debido a la modificacion de statement
# <Procedure statement> ::= procedure name [( <Actual parameter list> )]
# def procedure_statement(self):
# <Actual parameter list> ::= <Actual parameter> { , <Actual parameter>}
def actual_parameter_list(self):
self.actual_parameter()
while self.peek() == T.COMMA:
self.match(T.COMMA)
self.actual_parameter()
# <Actual parameter> ::= <Expression> | <Variable access>
# MODIFICADA: <Actual parameter> ::= <Expression>
def actual_parameter(self):
self.expression()
# <If statement> ::= if <Expression> then <Statement> [else <Statement>]
def if_statement(self):
self.match(T.IF)
self.expression()
self.match(T.THEN)
self.statement()
if self.peek() == T.ELSE:
self.match(T.ELSE)
self.statement()
# <While statement> ::= while <Expression> do <Statement>
def while_statement(self):
self.match(T.WHILE)
self.expression()
self.match(T.DO)
self.statement()
# <Compound statement> ::= begin <Statement> {; <Statement>} end
def compound_statement(self):
self.match(T.BEGIN)
self.statement()
while self.peek() == T.SEMICOLON:
self.match(T.SEMICOLON)
self.statement()
self.match(T.END)
# <Expression> ::= <Simple expression> [<Relational operator> <Simple expression>]
def expression(self):
self.simple_expression()
if self.peek() == T.LESS or \
self.peek() == T.EQUAL or \
self.peek() == T.GREATER or \
self.peek() == T.NOTGREATER or \
self.peek() == T.NOTEQUAL or \
self.peek() == T.NOTLESS:
self.relational_operator()
self.simple_expression()
# <Relational operator> ::= < | = | > | <= | <> | >=
def relational_operator(self):
if self.peek() == T.LESS:
self.match(T.LESS)
elif self.peek() == T.EQUAL:
self.match(T.EQUAL)
elif self.peek() == T.GREATER:
self.match(T.GREATER)
elif self.peek() == T.NOTGREATER:
self.match(T.NOTGREATER)
elif self.peek() == T.NOTEQUAL:
self.match(T.NOTEQUAL)
elif self.peek() == T.NOTLESS:
self.match(T.NOTLESS)
else:
self.syntax_error("Relational Operator")
# <Simple expression> ::= [<Sign operator>] <Term> {<Additive operator> <Term>}
def simple_expression(self):
if self.peek() == T.MINUS or self.peek() == T.PLUS:
self.sign_operator()
self.term()
while self.peek() == T.PLUS or \
self.peek() == T.MINUS or \
self.peek() == T.OR:
self.additive_operator()
self.term()
# <Sign operator> ::= + | -
def sign_operator(self):
if self.peek() == T.PLUS:
self.match(T.PLUS)
elif self.peek() == T.MINUS:
self.match(T.MINUS)
else:
self.syntax_error("Sign Operator")
# <Additive operator> ::= + | - | or
def additive_operator(self):
if self.peek() == T.PLUS:
self.match(T.PLUS)
elif self.peek() == T.MINUS:
self.match(T.MINUS)
elif self.peek() == T.OR:
self.match(T.OR)
else:
self.syntax_error("Additive Operator")
# <Term> ::= <Factor> {<Multiplying operator> <Factor>}
def term(self):
self.factor()
while self.peek() == T.ASTERISK or \
self.peek() == T.DIV or \
self.peek() == T.MOD or \
self.peek() == T.AND:
self.multiplying_operator()
self.factor()
# <Multiplying operator> ::= * | div | mod | and
def multiplying_operator(self):
if self.peek() == T.ASTERISK:
self.match(T.ASTERISK)
elif self.peek() == T.DIV:
self.match(T.DIV)
elif self.peek() == T.MOD:
self.match(T.MOD)
elif self.peek() == T.AND:
self.match(T.AND)
else:
self.syntax_error("Multiplying Operator")
# <Factor> ::= <Constant> | <Variable access> | ( <Expression> ) | not <Factor>
# MODIFICADA ::= NUMERAL | <Variable access> | ( <Expression> ) | not <Factor>
def factor(self):
if self.peek() == T.NUMERAL :
self.match(T.NUMERAL)
elif self.peek() == T.ID:
self.variable_access()
elif self.peek() == T.LEFTPARENTHESIS:
self.match(T.LEFTPARENTHESIS)
self.expression()
self.match(T.RIGHTPARENTHESIS)
elif self.peek() == T.NOT:
self.match(T.NOT)
self.factor()
else:
self.syntax_error("Factor")
# <Variable access> ::= variable name {<Selector>}
def variable_access(self):
self.match(T.ID)
while self.peek() == T.LEFTBRACKET or \
self.peek() == T.PERIOD:
self.selector();
# <Selector> ::= <Index selector> | <Field selector>
def selector(self):
if self.peek() == T.LEFTBRACKET:
self.index_selector()
elif self.peek() == T.PERIOD:
self.field_selector()
else:
self.syntax_error("Selector")
# <Index selector> ::= ”[”<Expression> ”]”
def index_selector(self):
self.match(T.LEFTBRACKET)
self.expression()
self.match(T.RIGHTBRACKET)
# <Field Selector> ::= . field name
def field_selector(self):
self.match(T.PERIOD)
self.match(T.ID)
# <Constant> ::= Numeral | constant name
def constant(self):
if self.peek() == T.NUMERAL:
self.match(T.NUMERAL)
elif self.peek() == T.ID:
self.match(T.ID)
else:
self.syntax_error("Numeral or ID")