-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgrammar.jison
473 lines (402 loc) · 12.1 KB
/
grammar.jison
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/**
* Copyright (c) 2013 Moacir de Oliveira
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/* description: G-Portugol Definition */
%lex
T_DEC_LIT [0-9]+
T_OCTAL_LIT [0][cC][0-8]+
T_HEX_LIT [0][xX][0-9a-fA-F]+
T_BIN_LIT [0][bB][01]+
T_INT_LIT {T_OCTAL_LIT}|{T_HEX_LIT}|{T_BIN_LIT}|{T_DEC_LIT}
T_REAL_LIT {T_DEC_LIT}[\.]{T_DEC_LIT}
ESC "\\".
%s comment
%%
"//".* { /* ignore comment */ }
"/*" { this.begin('comment'); }
<comment>"*/" { this.popState(); }
<comment>. { /* ignore comment */ }
\s+ { /* ignore space */ }
<<EOF>> { return 'EOF'; }
{T_REAL_LIT} { return "T_REAL_LIT" }
{T_INT_LIT} { return "T_INT_LIT" }
"\"\"" { return "T_STRING_LIT"; }
"\""([^"]|{ESC})*"\"" { return "T_STRING_LIT"; }
"'"(.|{ESC})"'" { return "T_CARAC_LIT"; }
"verdadeiro" { return "T_BOOL_LIT"; }
"falso" { return "T_BOOL_LIT"; }
"algoritmo" { return "ALGORITMO"; }
"variáveis" { return "VARIAVEIS"; }
"fim-variáveis" { return "FIM_VARIAVEIS"; }
"fim-enquanto" { return "FIM_ENQUANTO"; }
"fim-para" { return "FIM_PARA"; }
"fim-se" { return "FIM_SE"; }
"fim" { return "FIM"; }
"inteiro" { return "INTEIRO"; }
"real" { return "REAL"; }
"caractere" { return "CARACTERE"; }
"literal" { return "LITERAL"; }
"lógico" { return "LOGICO"; }
"início" { return "INICIO"; }
"se" { return "SE"; }
"senão" { return "SENAO"; }
"enquanto" { return "ENQUANTO"; }
"então" { return "ENTAO"; }
"faça" { return "FACA"; }
"para" { return "PARA"; }
"de" { return "DE"; }
"até" { return "ATE"; }
"e" { return "E"; }
"ou" { return "OU"; }
"não" { return "NAO"; }
"matriz" { return "MATRIZ"; }
"inteiros" { return "INTEIROS"; }
"reais" { return "REAIS"; }
"caracteres" { return "CARACTERES"; }
"literais" { return "LITERAIS"; }
"lógicos" { return "LOGICOS"; }
"função" { return "FUNCAO"; }
"retorne" { return "RETORNE"; }
"passo" { return "PASSO"; }
[a-zA-Z_][0-9a-zA-Z_]* { return "T_IDENTIFICADOR"; }
":=" { return ":="; }
";" { return ";"; }
":" { return ":"; }
"," { return ","; }
"[" { return "["; }
"]" { return "]"; }
"(" { return "("; }
")" { return ")"; }
"+" { return "+"; }
"-" { return "-"; }
"*" { return "*"; }
"/" { return "/"; }
"%" { return "%"; }
"||" { return "||"; }
"&&" { return "&&"; }
"|" { return "|"; }
"&" { return "&"; }
"^" { return "^"; }
"~" { return "~"; }
"<>" { return "<>"; }
">=" { return ">="; }
"<=" { return "<="; }
"<" { return "<"; }
">" { return ">"; }
"=" { return "="; }
/lex
%start algorithm
%%
algorithm
: stm_algorithm stm_block EOF
{{ return new yy.AlgorithmNode($1, null, null, $2); }}
| stm_algorithm var_decl_block stm_block func_decl_list EOF
{{ return new yy.AlgorithmNode($1, $2, $4, $3); }}
| stm_algorithm stm_block func_decl_list EOF
{{ return new yy.AlgorithmNode($1, null, $3, $2); }}
| stm_algorithm var_decl_block stm_block EOF
{{ return new yy.AlgorithmNode($1, $2, null, $3); }}
;
stm_algorithm
: ALGORITMO T_IDENTIFICADOR ";"
{{ $$ = $2; }}
;
var_decl_block
: VARIAVEIS FIM_VARIAVEIS
{{ $$ = new yy.VariablesBlockNode([]); }}
| VARIAVEIS var_decl_list FIM_VARIAVEIS
{{ $$ = new yy.VariablesBlockNode($2); }}
;
var_decl_list
: var_decl
{{ $$ = $1; }}
| var_decl_list var_decl
{{ $$ = $1.concat($2); }}
;
var_decl_id
: T_IDENTIFICADOR
{{ $$ = [$1]; }}
| var_decl_id "," T_IDENTIFICADOR
{{ $1.push($3); $$ = $1; }}
;
var_decl_type
: tp_primitivo
| tp_matriz
;
var_decl
: var_decl_id ":" var_decl_type ";"
{{ $$ = yy.Util.createVariableDeclarationList($1, $3); }}
;
tp_primitivo
: INTEIRO
| REAL
| CARACTERE
| LITERAL
| LOGICO
;
tp_prim_pl
: INTEIROS
| REAIS
| CARACTERES
| LITERAIS
| LOGICOS
;
tp_matriz_index_literal
: "[" T_INT_LIT "]"
;
tp_matriz_index
: tp_matriz_index_literal
| tp_matriz_index tp_matriz_index_literal
;
tp_matriz
: MATRIZ tp_matriz_index DE tp_prim_pl
;
stm_block
: INICIO stm_list FIM
{{ $$ = $2; }}
| INICIO FIM
{{ $$ = new yy.StatementListNode([]); }}
;
stm_list
: stm
{{ $$ = new yy.StatementListNode([$1]); }}
| stm_list stm
{{ $1.list.push($2); $$ = $1; }}
;
stm
: stm_attr
| stm_fcall
| stm_ret
| stm_se
| stm_enquanto
| stm_para
;
stm_fcall
: fcall ";"
{{ $$ = new yy.FunctionStatementNode($1); }}
;
stm_ret
: RETORNE expr ";"
{{ $$ = new yy.ReturnStatementNode($2); }}
;
lvalue
: T_IDENTIFICADOR
{{ $$ = new yy.IdentifierNode($1); }}
| T_IDENTIFICADOR lvalue_array
;
lvalue_array
: "[" expr "]"
| lvalue_array "[" expr "]"
;
stm_attr
: lvalue ":=" expr ";"
{{ $$ = new yy.AssignmentExpressionNode($2, $1, $3); }}
;
stm_se
: SE expr ENTAO FIM_SE
{{ $$ = new yy.IfStatementNode($2, new yy.StatementListNode([])); }}
| SE expr ENTAO stm_list FIM_SE
{{ $$ = new yy.IfStatementNode($2, $4); }}
| SE expr ENTAO stm_list SENAO stm_list FIM_SE
{{ $$ = new yy.IfStatementNode($2, $4, $6); }}
| SE expr ENTAO SENAO stm_list FIM_SE
{{ $$ = new yy.IfStatementNode($2, new yy.StatementListNode([]), $5); }}
| SE expr ENTAO stm_list SENAO FIM_SE
{{ $$ = new yy.IfStatementNode($2, $4, new yy.StatementListNode([])); }}
| SE expr ENTAO SENAO FIM_SE
{{ $$ = new yy.IfStatementNode($2, new yy.StatementListNode([]), new yy.StatementListNode([])); }}
;
stm_enquanto
: ENQUANTO expr FACA stm_list FIM_ENQUANTO
{{ $$ = new yy.WhileStatementNode($2, $4); }}
| ENQUANTO expr FACA FIM_ENQUANTO
{{ $$ = new yy.WhileStatementNode($2, new yy.StatementListNode([])); }}
;
stm_para
: PARA lvalue DE expr ATE expr stm_para_block
{{ $$ = new yy.ForStatementNode($2, $4, $6, null, $7); }}
| PARA lvalue DE expr ATE expr passo stm_para_block
{{ $$ = new yy.ForStatementNode($2, $4, $6, $7, $8); }}
;
stm_para_block
: FACA FIM_PARA
{{ $$ = new yy.StatementListNode([]); }}
| FACA stm_list FIM_PARA
{{ $$ = $2; }}
;
passo
: PASSO T_INT_LIT
{{ $$ = new yy.LiteralNode('Integer', yy.Util.createNumberFromRawString($2), $2); }}
| PASSO unary_pos_neg T_INT_LIT
{{ $$ = new yy.LiteralNode('Integer', yy.Util.createNumberFromRawString($2 + $3), $2 + $3); }}
;
termo
: fcall
| lvalue
| literal
| "(" expr ")"
{{ $$ = $2; }}
;
unary_pos_neg
: "+"
| "-"
;
unary_op
: unary_pos_neg
| "~"
| NAO
;
expr_unary
: termo
| unary_op termo
{{ $$ = new yy.UnaryExpressionNode($1, $2); }}
;
expr_multiply
: expr_unary
| expr_multiply "*" expr_unary
{{ $$ = new yy.ArithmeticExpressionNode($2, $1, $3); }}
| expr_multiply "/" expr_unary
{{ $$ = new yy.ArithmeticExpressionNode($2, $1, $3); }}
| expr_multiply "%" expr_unary
{{ $$ = new yy.ArithmeticExpressionNode($2, $1, $3); }}
;
expr_additive
: expr_multiply
| expr_additive "+" expr_multiply
{{ $$ = new yy.ArithmeticExpressionNode($2, $1, $3); }}
| expr_additive "-" expr_multiply
{{ $$ = new yy.ArithmeticExpressionNode($2, $1, $3); }}
;
expr_relational
: expr_additive
| expr_relational ">" expr_additive
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
| expr_relational ">=" expr_additive
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
| expr_relational "<" expr_additive
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
| expr_relational "<=" expr_additive
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
;
expr_equal
: expr_relational
| expr_equal "=" expr_relational
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
| expr_equal "<>" expr_relational
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
;
expr_and
: expr_equal
| expr_and "&" expr_equal
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
;
expr_or_exclusive
: expr_and
| expr_or_exclusive "^" expr_and
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
;
expr_or
: expr_or_exclusive
| expr_or "|" expr_or_exclusive
{{ $$ = new yy.BinaryExpressionNode($2, $1, $3); }}
;
expr_logical_and
: expr_or
| expr_logical_and "&&" expr_or
{{ $$ = new yy.LogicalExpressionNode($2, $1, $3); }}
| expr_logical_and E expr_or
{{ $$ = new yy.LogicalExpressionNode($2, $1, $3); }}
;
expr_logical_or
: expr_logical_and
| expr_logical_or "||" expr_logical_and
{{ $$ = new yy.LogicalExpressionNode($2, $1, $3); }}
| expr_logical_or OU expr_logical_and
{{ $$ = new yy.LogicalExpressionNode($2, $1, $3); }}
;
expr
: expr_logical_or
{{ $$ = new yy.ExpressionNode($1); }}
;
fcall
: T_IDENTIFICADOR fcall_args
{{ $$ = new yy.FunctionCallNode($1, $2); }}
;
fcall_args
: "(" ")"
{{ $$ = []; }}
| "(" fcall_arg_list ")"
{{ $$ = $2; }}
;
fcall_arg_list
: expr
{{ $$ = [$1]; }}
| fcall_arg_list "," expr
{{ $1.push($3); $$ = $1; }}
;
literal
: T_STRING_LIT
{{ $$ = new yy.LiteralNode('literal', $1, $1); }}
| T_INT_LIT
{{ $$ = new yy.LiteralNode('inteiro', yy.Util.createNumberFromRawString($1), $1); }}
| T_REAL_LIT
{{ $$ = new yy.LiteralNode('real', new Number($1), $1); }}
| T_CARAC_LIT
{{ $$ = new yy.LiteralNode('caractere', $1, $1); }}
| T_BOOL_LIT
{{ $$ = new yy.LiteralNode('lógico', $1, $1); }}
;
func_decl_list
: func_decl
{{ $$ = new yy.FunctionDeclarationListNode([$1]); }}
| func_decl_list func_decl
{{ $1.list.push($2); $$ = $1; }}
;
func_decl
: FUNCAO T_IDENTIFICADOR func_params stm_block
{{ $$ = new yy.FunctionDeclarationNode($2, null, $3, [], $4); }}
| FUNCAO T_IDENTIFICADOR func_params var_decl_list stm_block
{{ $$ = new yy.FunctionDeclarationNode($2, null, $3, $4, $5); }}
| FUNCAO T_IDENTIFICADOR func_params func_type stm_block
{{ $$ = new yy.FunctionDeclarationNode($2, $4, $3, [], $5); }}
| FUNCAO T_IDENTIFICADOR func_params func_type var_decl_list stm_block
{{ $$ = new yy.FunctionDeclarationNode($2, $4, $3, $5, $6); }}
;
func_type
: ":" tp_primitivo
{{ $$ = $2; }}
;
func_params
: "(" ")"
{{ $$ = []; }}
| "(" func_param_list ")"
{{ $$ = $2; }}
;
func_param_list
: func_param_decl
{{ $$ = [$1]; }}
| func_param_list ',' func_param_decl
{{ $1.push($3); $$ = $1; }}
;
func_param_decl
: T_IDENTIFICADOR ":" var_decl_type
{{ $$ = new yy.FunctionParameterNode($1, $3); }}
;