1
|
1 /* parse.y -*- text -*- |
|
2 |
|
3 Copyright (C) 1992, 1993 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 // Parser for Octave. |
|
24 |
|
25 /* |
|
26 * C decarations. |
|
27 */ |
|
28 %{ |
|
29 #define YYDEBUG 1 |
|
30 |
|
31 #include "SLStack.h" |
|
32 |
|
33 #include "Matrix.h" |
|
34 |
|
35 #include "error.h" |
|
36 #include "variables.h" |
|
37 #include "user-prefs.h" |
|
38 #include "input.h" |
|
39 #include "utils.h" |
|
40 #include "tree.h" |
|
41 #include "symtab.h" |
|
42 #include "builtins.h" |
|
43 #include "octave.h" |
|
44 #include "parse.h" |
|
45 #include "lex.h" |
|
46 |
|
47 // Identifier to define if we are reading an M-fie. |
|
48 tree_identifier *id_to_define; |
|
49 |
|
50 // Nonzero means we're in the middle of defining a function. |
|
51 int defining_func = 0; |
|
52 |
|
53 // Nonzero means we're in the middle of defining a loop. |
|
54 int looping = 0; |
|
55 |
|
56 // Nonzero means we're in the middle of defining a conditional expression. |
|
57 int iffing = 0; |
|
58 |
|
59 // Nonzero means we need to do some extra lookahead to avoid being |
|
60 // screwed by bogus function syntax. |
|
61 int maybe_screwed = 0; |
|
62 |
|
63 // Nonzero means we need to do some extra lookahead to avoid being |
|
64 // screwed by bogus function syntax. |
|
65 int maybe_screwed_again = 0; |
|
66 |
|
67 // Temporary symbol table pointer used to cope with bogus function syntax. |
|
68 symbol_table *tmp_local_sym_tab = (symbol_table *) NULL; |
|
69 |
|
70 // Stack to hold list of literal matrices. |
|
71 SLStack <tree_matrix *> ml; |
|
72 |
|
73 // A nonzero element corresponding to an element of ml means we just |
|
74 // started reading a new matrix. This should probably be part of a |
|
75 // new struct for matrix lists... |
|
76 SLStack <int> mlnm; |
|
77 |
|
78 // The current input line number. |
|
79 int input_line_number = 0; |
|
80 |
|
81 // The column of the current token. |
|
82 int current_input_column = 0; |
|
83 |
|
84 // Buffer for help text snagged from M-files. |
|
85 // Probably shouldn't be a fixed size... |
|
86 char help_buf [HELP_BUF_LENGTH]; |
|
87 |
|
88 // Nonzero means we're working on a plot command. |
|
89 int plotting = 0; |
|
90 |
|
91 // Nonzero means we're looking at the range part of a plot command. |
|
92 int in_plot_range = 0; |
|
93 |
|
94 // Nonzero means we're looking at the using part of a plot command. |
|
95 int in_plot_using = 0; |
|
96 |
|
97 // Nonzero means we're looking at the style part of a plot command. |
|
98 int in_plot_style = 0; |
|
99 |
|
100 // The type of an END token. This declaration is repeated in lex.l. |
|
101 enum end_tok_type |
|
102 { |
|
103 simple_end, |
|
104 for_end, |
|
105 function_end, |
|
106 if_end, |
|
107 while_end, |
|
108 }; |
|
109 |
|
110 // The type of a PLOT token. This declaration is repeated in lex.l. |
|
111 enum plot_tok_type |
|
112 { |
|
113 two_dee = 2, |
|
114 three_dee = 3, |
|
115 }; |
|
116 |
|
117 // Error mesages for mismatched end statements. |
|
118 static void end_error (char *type, end_tok_type ettype); |
|
119 |
|
120 // Generic error messages. |
|
121 static void yyerror (char *s); |
|
122 |
|
123 static tree *maybe_convert_to_ans_assign (tree *expr); |
|
124 static void maybe_warn_assign_as_truth_value (tree *expr); |
|
125 |
|
126 #define ABORT_PARSE \ |
|
127 do \ |
|
128 { \ |
|
129 global_command = NULL_TREE; \ |
|
130 reset_parser (); \ |
|
131 yyerrok; \ |
|
132 if (interactive) \ |
|
133 YYACCEPT; \ |
|
134 else \ |
|
135 YYABORT; \ |
|
136 } \ |
|
137 while (0) |
|
138 |
|
139 %} |
|
140 |
|
141 /* |
|
142 * Bison declarations. |
|
143 */ |
|
144 %union |
|
145 { |
|
146 tree *tree_type; |
|
147 tree_constant *tree_constant_type; |
|
148 tree_matrix *tree_matrix_type; |
|
149 tree_identifier *tree_identifier_type; |
|
150 tree_function *tree_function_type; |
|
151 tree_index_expression *tree_index_expression_type; |
|
152 tree_colon_expression *tree_colon_expression_type; |
|
153 tree_argument_list *tree_argument_list_type; |
|
154 tree_parameter_list *tree_parameter_list_type; |
|
155 tree_word_list *tree_word_list_type; |
|
156 tree_command *tree_command_type; |
|
157 tree_if_command *tree_if_command_type; |
|
158 tree_command_list *tree_command_list_type; |
|
159 tree_word_list_command *tree_word_list_command_type; |
|
160 tree_plot_command *tree_plot_command_type; |
|
161 tree_subplot_list *tree_subplot_list_type; |
|
162 tree_plot_limits *tree_plot_limits_type; |
|
163 tree_plot_range *tree_plot_range_type; |
|
164 tree_subplot_using *tree_subplot_using_type; |
|
165 tree_subplot_style *tree_subplot_style_type; |
|
166 symbol_record *sym_rec; |
|
167 double number; |
|
168 char *string; |
|
169 end_tok_type ettype; |
|
170 plot_tok_type pttype; |
|
171 } |
|
172 |
|
173 /* |
|
174 * There are 20 shift/reduce conflicts, ok? |
|
175 */ |
|
176 %expect 20 |
|
177 |
|
178 /* |
|
179 * Reserved words. |
|
180 */ |
|
181 %token FOR WHILE IF ELSEIF ELSE FCN BREAK CONTINUE FUNC_RET SCREW_TWO |
|
182 %token END_OF_INPUT GLOBAL CLEAR |
|
183 |
|
184 %token USING TITLE WITH COLON OPEN_BRACE CLOSE_BRACE |
|
185 |
|
186 // tree |
|
187 %type <tree_type> input command |
|
188 %type <tree_type> ans_expression expression simple_expr simple_expr1 |
|
189 %type <tree_type> title |
|
190 |
|
191 // tree_matrix |
|
192 %type <tree_matrix_type> matrix |
|
193 |
|
194 // tree_identifier |
|
195 %type <tree_identifier_type> identifier |
|
196 |
|
197 // tree_function |
|
198 %type <tree_function_type> func_def func_def1 func_def2 func_def3 |
|
199 |
|
200 // tree_index_expression |
|
201 %type <tree_index_expression_type> variable |
|
202 |
|
203 // tree_colon_expression |
|
204 %type <tree_colon_expression_type> colon_expr |
|
205 |
|
206 // tree_argument_list |
|
207 %type <tree_argument_list_type> arg_list arg_list1 |
|
208 |
|
209 // tree_parameter_list |
|
210 %type <tree_parameter_list_type> param_list param_list1 func_def1a |
|
211 |
|
212 // tree_word_list |
|
213 %type <tree_word_list_type> word_list word_list1 |
|
214 |
|
215 // tree_command |
|
216 %type <tree_command_type> statement |
|
217 |
|
218 // tree_if_command |
|
219 %type <tree_if_command_type> elseif |
|
220 |
|
221 // tree_command_list |
|
222 %type <tree_command_list_type> simple_list simple_list1 list list1 opt_list |
|
223 |
|
224 // tree_word_list_command |
|
225 %type <tree_word_list_command_type> word_list_cmd |
|
226 |
|
227 // tree_plot_command |
|
228 %type <tree_plot_command_type> plot_command |
|
229 |
|
230 // tree_subplot_list |
|
231 %type <tree_subplot_list_type> plot_command1 plot_command2 plot_options |
|
232 |
|
233 // tree_plot_limits |
|
234 %type <tree_plot_limits_type> ranges |
|
235 |
|
236 // tree_plot_range |
|
237 %type <tree_plot_range_type> ranges1 |
|
238 |
|
239 // tree_subplot_using |
|
240 %type <tree_subplot_using_type> using using1 |
|
241 |
|
242 // tree_subplot_style |
|
243 %type <tree_subplot_style_type> style |
|
244 |
|
245 %token <number> NUM IMAG_NUM |
|
246 %token <sym_rec> NAME SCREW |
|
247 %token <string> TEXT STYLE |
|
248 %token <ettype> END |
|
249 %token <pttype> PLOT |
|
250 |
|
251 %left ';' ',' '\n' |
|
252 %right '=' |
|
253 %left EXPR_AND EXPR_OR |
|
254 %left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT |
|
255 %left ':' |
|
256 %left '-' '+' |
|
257 %left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV |
|
258 %left QUOTE TRANSPOSE |
|
259 %left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT |
|
260 %right POW EPOW |
|
261 |
|
262 %start input |
|
263 |
|
264 /* |
|
265 * Grammar rules. |
|
266 */ |
|
267 %% |
|
268 |
|
269 input : '\n' |
|
270 { |
|
271 global_command = NULL_TREE; |
|
272 promptflag = 1; |
|
273 YYACCEPT; |
|
274 } |
|
275 | END_OF_INPUT |
|
276 { |
|
277 global_command = NULL_TREE; |
|
278 promptflag = 1; |
|
279 YYABORT; |
|
280 } |
|
281 | simple_list '\n' |
|
282 { |
|
283 global_command = $1; |
|
284 promptflag = 1; |
|
285 YYACCEPT; |
|
286 } |
|
287 | simple_list END_OF_INPUT |
|
288 { |
|
289 global_command = $1; |
|
290 promptflag = 1; |
|
291 YYACCEPT; |
|
292 } |
|
293 | error |
|
294 { ABORT_PARSE; } |
|
295 | error '\n' |
|
296 { ABORT_PARSE; } |
|
297 | error END_OF_INPUT |
|
298 { ABORT_PARSE; } |
|
299 | simple_list error |
|
300 { ABORT_PARSE; } |
|
301 | simple_list error END_OF_INPUT |
|
302 { ABORT_PARSE; } |
|
303 ; |
|
304 |
|
305 simple_list : semi_comma |
|
306 { $$ = (tree_command_list *) NULL; } |
|
307 | comma_semi |
|
308 { $$ = (tree_command_list *) NULL; } |
|
309 | simple_list1 |
|
310 { $$ = $1->reverse (); } |
|
311 | simple_list1 semi_comma |
|
312 { |
|
313 $1->set_print_flag (0); |
|
314 $$ = $1->reverse (); |
|
315 } |
|
316 | simple_list1 comma_semi |
|
317 { $$ = $1->reverse (); } |
|
318 ; |
|
319 |
|
320 simple_list1 : command |
|
321 { $$ = new tree_command_list ($1); } |
|
322 | semi_comma command |
|
323 { $$ = new tree_command_list ($2); } |
|
324 | comma_semi command |
|
325 { $$ = new tree_command_list ($2); } |
|
326 | simple_list1 semi_comma command |
|
327 { |
|
328 $1->set_print_flag (0); |
|
329 $$ = $1->chain ($3); |
|
330 } |
|
331 | simple_list1 comma_semi command |
|
332 { $$ = $1->chain ($3); } |
|
333 ; |
|
334 |
|
335 semi_comma : ';' |
|
336 | semi_comma ',' |
|
337 | semi_comma ';' |
|
338 ; |
|
339 |
|
340 comma_semi : ',' |
|
341 | comma_semi ';' |
|
342 | comma_semi ',' |
|
343 ; |
|
344 |
|
345 comma_nl_sep : ',' |
|
346 | '\n' |
|
347 | comma_nl_sep sep |
|
348 ; |
|
349 |
|
350 semi_sep : ';' |
|
351 | semi_sep sep |
|
352 ; |
|
353 |
|
354 opt_list : // empty |
|
355 { $$ = new tree_command_list (); } |
|
356 | list |
|
357 { $$ = $1; } |
|
358 |
|
359 list : list1 |
|
360 { $$ = $1->reverse (); } |
|
361 | list1 comma_nl_sep |
|
362 { $$ = $1->reverse (); } |
|
363 | list1 semi_sep |
|
364 { |
|
365 $1->set_print_flag (0); |
|
366 $$ = $1->reverse (); |
|
367 } |
|
368 ; |
|
369 |
|
370 list1 : command |
|
371 { $$ = new tree_command_list ($1); } |
|
372 | list1 comma_nl_sep command |
|
373 { $$ = $1->chain ($3); } |
|
374 | list1 semi_sep command |
|
375 { |
|
376 $1->set_print_flag (0); |
|
377 $$ = $1->chain ($3); |
|
378 } |
|
379 ; |
|
380 |
|
381 command : plot_command |
|
382 { $$ = $1; } |
|
383 | statement |
|
384 { $$ = $1; } |
|
385 | ans_expression |
|
386 { $$ = $1; } |
|
387 | func_def |
|
388 { $$ = $1; } |
|
389 | global_decl |
|
390 { $$ = NULL_TREE; } |
|
391 ; |
|
392 |
|
393 plot_command : PLOT plot_command1 |
|
394 { |
|
395 tree_subplot_list *tmp = $2->reverse (); |
|
396 $$ = new tree_plot_command (tmp, $1); |
|
397 plotting = 0; |
|
398 in_plot_range = 0; |
|
399 in_plot_using = 0; |
|
400 in_plot_style = 0; |
|
401 } |
|
402 | PLOT ranges plot_command1 |
|
403 { |
|
404 tree_subplot_list *tmp = $3->reverse (); |
|
405 $$ = new tree_plot_command (tmp, $2, $1); |
|
406 plotting = 0; |
|
407 in_plot_range = 0; |
|
408 in_plot_using = 0; |
|
409 in_plot_style = 0; |
|
410 } |
|
411 ; |
|
412 |
|
413 ranges : ranges1 |
|
414 { $$ = new tree_plot_limits ($1); } |
|
415 | ranges1 ranges1 |
|
416 { $$ = new tree_plot_limits ($1, $2); } |
|
417 | ranges1 ranges1 ranges1 |
|
418 { $$ = new tree_plot_limits ($1, $2, $3); } |
|
419 ; |
|
420 |
|
421 ranges1 : OPEN_BRACE expression COLON expression CLOSE_BRACE |
|
422 { $$ = new tree_plot_range ($2, $4); } |
|
423 | OPEN_BRACE COLON expression CLOSE_BRACE |
|
424 { $$ = new tree_plot_range (NULL, $3); } |
|
425 | OPEN_BRACE expression COLON CLOSE_BRACE |
|
426 { $$ = new tree_plot_range ($2, NULL); } |
|
427 | OPEN_BRACE COLON CLOSE_BRACE |
|
428 { $$ = new tree_plot_range (); } |
|
429 | OPEN_BRACE CLOSE_BRACE |
|
430 { $$ = new tree_plot_range (); } |
|
431 ; |
|
432 |
|
433 plot_command1 : plot_command2 |
|
434 { $$ = $1; } |
|
435 | plot_command1 ',' plot_command2 |
|
436 { $$ = $1->chain ($3); } |
|
437 ; |
|
438 |
|
439 plot_command2 : expression |
|
440 { $$ = new tree_subplot_list ($1); } |
|
441 | expression plot_options |
|
442 { $$ = $2->set_data ($1); } |
|
443 ; |
|
444 |
|
445 plot_options : using |
|
446 { $$ = new tree_subplot_list ($1, NULL, NULL); } |
|
447 | title |
|
448 { $$ = new tree_subplot_list (NULL, $1, NULL); } |
|
449 | style |
|
450 { $$ = new tree_subplot_list (NULL, NULL, $1); } |
|
451 | using title |
|
452 { $$ = new tree_subplot_list ($1, $2, NULL); } |
|
453 | title using |
|
454 { $$ = new tree_subplot_list ($2, $1, NULL); } |
|
455 | using style |
|
456 { $$ = new tree_subplot_list ($1, NULL, $2); } |
|
457 | style using |
|
458 { $$ = new tree_subplot_list ($2, NULL, $1); } |
|
459 | title style |
|
460 { $$ = new tree_subplot_list (NULL, $1, $2); } |
|
461 | style title |
|
462 { $$ = new tree_subplot_list (NULL, $2, $1); } |
|
463 | using title style |
|
464 { $$ = new tree_subplot_list ($1, $2, $3); } |
|
465 | using style title |
|
466 { $$ = new tree_subplot_list ($1, $3, $2); } |
|
467 | title using style |
|
468 { $$ = new tree_subplot_list ($2, $1, $3); } |
|
469 | title style using |
|
470 { $$ = new tree_subplot_list ($3, $1, $2); } |
|
471 | style using title |
|
472 { $$ = new tree_subplot_list ($2, $3, $1); } |
|
473 | style title using |
|
474 { $$ = new tree_subplot_list ($3, $2, $1); } |
|
475 ; |
|
476 |
|
477 using : using1 |
|
478 { |
|
479 $$ = $1; |
|
480 in_plot_using = 0; |
|
481 } |
|
482 | using1 expression |
|
483 { |
|
484 $$ = $1->set_format ($2); |
|
485 in_plot_using = 0; |
|
486 } |
|
487 ; |
|
488 |
|
489 using1 : USING expression |
|
490 { |
|
491 tree_subplot_using *tmp = new tree_subplot_using (); |
|
492 $$ = tmp->add_qualifier ($2); |
|
493 } |
|
494 | using1 COLON expression |
|
495 { $$ = $1->add_qualifier ($3); } |
|
496 ; |
|
497 |
|
498 title : TITLE expression |
|
499 { $$ = $2; } |
|
500 ; |
|
501 |
|
502 style : WITH STYLE |
|
503 { $$ = new tree_subplot_style ($2); } |
|
504 | WITH STYLE expression |
|
505 { $$ = new tree_subplot_style ($2, $3); } |
|
506 | WITH STYLE expression bogus_syntax expression |
|
507 { $$ = new tree_subplot_style ($2, $3, $5); } |
|
508 ; |
|
509 |
|
510 bogus_syntax : // empty |
|
511 ; |
|
512 |
|
513 ans_expression : expression |
|
514 { $$ = maybe_convert_to_ans_assign ($1); } |
|
515 ; |
|
516 |
|
517 global_decl : GLOBAL global_decl1 |
|
518 { } |
|
519 ; |
|
520 |
|
521 global_decl1 : NAME |
|
522 { force_global ($1->name ()); } |
|
523 | NAME '=' expression |
|
524 { |
|
525 symbol_record *sr = force_global ($1->name ()); |
|
526 tree_identifier *id = new tree_identifier (sr); |
|
527 tree_simple_assignment_expression *expr = |
|
528 new tree_simple_assignment_expression (id, $3); |
|
529 expr->eval (0); |
|
530 } |
|
531 | global_decl1 optcomma NAME |
|
532 { force_global ($3->name ()); } |
|
533 | global_decl1 optcomma NAME '=' expression |
|
534 { |
|
535 symbol_record *sr = force_global ($3->name ()); |
|
536 tree_identifier *id = new tree_identifier (sr); |
|
537 tree_simple_assignment_expression *expr = |
|
538 new tree_simple_assignment_expression (id, $5); |
|
539 expr->eval (0); |
|
540 } |
|
541 ; |
|
542 |
|
543 optcomma : // empty |
|
544 | ',' |
|
545 { |
|
546 if (user_pref.warn_comma_in_global_decl) |
|
547 warning ("comma in global declaration not\ |
|
548 interpreted as a command separator"); |
|
549 } |
|
550 ; |
|
551 |
|
552 statement : WHILE expression optsep opt_list END |
|
553 { |
|
554 maybe_warn_assign_as_truth_value ($2); |
|
555 if ($5 != while_end && $5 != simple_end) |
|
556 { |
|
557 yyerror ("parse error"); |
|
558 end_error ("while", $5); |
|
559 ABORT_PARSE; |
|
560 } |
|
561 looping--; |
|
562 $$ = new tree_while_command ($2, $4); |
|
563 } |
|
564 | FOR identifier '=' expression optsep opt_list END |
|
565 { |
|
566 if ($7 != for_end && $7 != simple_end) |
|
567 { |
|
568 yyerror ("parse error"); |
|
569 end_error ("for", $7); |
|
570 ABORT_PARSE; |
|
571 } |
|
572 looping--; |
|
573 $$ = new tree_for_command ($2, $4, $6); |
|
574 } |
|
575 | IF expression optsep opt_list END |
|
576 { |
|
577 maybe_warn_assign_as_truth_value ($2); |
|
578 if ($5 != if_end && $5 != simple_end) |
|
579 { |
|
580 yyerror ("parse error"); |
|
581 end_error ("if", $5); |
|
582 ABORT_PARSE; |
|
583 } |
|
584 iffing--; |
|
585 $$ = new tree_if_command ($2, $4); |
|
586 } |
|
587 | IF expression optsep opt_list ELSE optsep opt_list END |
|
588 { |
|
589 maybe_warn_assign_as_truth_value ($2); |
|
590 if ($8 != if_end && $8 != simple_end) |
|
591 { |
|
592 yyerror ("parse error"); |
|
593 end_error ("if", $8); |
|
594 ABORT_PARSE; |
|
595 } |
|
596 iffing--; |
|
597 tree_if_command *t1 = new tree_if_command ($7); |
|
598 $$ = t1->chain ($2, $4); |
|
599 } |
|
600 | IF expression optsep opt_list elseif END |
|
601 { |
|
602 maybe_warn_assign_as_truth_value ($2); |
|
603 if ($6 != if_end && $6 != simple_end) |
|
604 { |
|
605 yyerror ("parse error"); |
|
606 end_error ("if", $6); |
|
607 ABORT_PARSE; |
|
608 } |
|
609 iffing--; |
|
610 tree_if_command *t1 = $5->reverse (); |
|
611 // Add the if list to the new head of the elseif |
|
612 // list, and return the list. |
|
613 $$ = t1->chain ($2, $4); |
|
614 } |
|
615 | IF expression optsep opt_list elseif ELSE optsep opt_list END |
|
616 { |
|
617 maybe_warn_assign_as_truth_value ($2); |
|
618 if ($9 != if_end && $9 != simple_end) |
|
619 { |
|
620 yyerror ("parse error"); |
|
621 end_error ("if", $9); |
|
622 ABORT_PARSE; |
|
623 } |
|
624 iffing--; |
|
625 // Add the else list to the head of the elseif list, |
|
626 // then reverse the list. |
|
627 tree_if_command *t1 = $5->chain ($8); |
|
628 t1 = t1->reverse (); |
|
629 // Add the if list to the new head of the elseif |
|
630 // list, and return the list. |
|
631 $$ = t1->chain ($2, $4); |
|
632 } |
|
633 | BREAK |
|
634 { |
|
635 if (!looping) |
|
636 { |
|
637 yyerror ("parse error"); |
|
638 error ("break: only meaningful within a `for'\ |
|
639 or `while' loop"); |
|
640 ABORT_PARSE; |
|
641 } |
|
642 $$ = new tree_break_command (); |
|
643 } |
|
644 | CONTINUE |
|
645 { |
|
646 if (!looping) |
|
647 { |
|
648 yyerror ("parse error"); |
|
649 error ("continue: only meaningful within a\ |
|
650 `for' or `while' loop"); |
|
651 ABORT_PARSE; |
|
652 } |
|
653 $$ = new tree_break_command (); |
|
654 } |
|
655 | FUNC_RET |
|
656 { |
|
657 if (!defining_func) |
|
658 { |
|
659 yyerror ("parse error"); |
|
660 error ("return: only meaningful within a function"); |
|
661 ABORT_PARSE; |
|
662 } |
|
663 $$ = new tree_return_command (); |
|
664 } |
|
665 ; |
|
666 |
|
667 elseif : ELSEIF optsep expression optsep opt_list |
|
668 { |
|
669 maybe_warn_assign_as_truth_value ($3); |
|
670 $$ = new tree_if_command ($3, $5); |
|
671 } |
|
672 | elseif ELSEIF optsep expression optsep opt_list |
|
673 { |
|
674 maybe_warn_assign_as_truth_value ($4); |
|
675 $$ = $1->chain ($4, $6); |
|
676 } |
|
677 ; |
|
678 |
|
679 optsep : // empty |
|
680 | sep |
|
681 ; |
|
682 |
|
683 sep : ',' |
|
684 | ';' |
|
685 | '\n' |
|
686 | sep ',' |
|
687 | sep ';' |
|
688 | sep '\n' |
|
689 ; |
|
690 |
|
691 screwed_again : // empty |
|
692 { maybe_screwed_again++; } |
|
693 ; |
|
694 |
|
695 expression : variable '=' expression |
|
696 { $$ = new tree_simple_assignment_expression ($1, $3); } |
|
697 | '[' screwed_again matrix_row SCREW_TWO '=' expression |
|
698 { |
|
699 |
|
700 // Will need a way to convert the matrix list to a list of |
|
701 // identifiers. If that fails, we can abort here, without losing |
|
702 // anything -- no other possible syntax is valid if we've seen the |
|
703 // equals sign as the next token after the `]'. |
|
704 |
|
705 $$ = (tree_multi_assignment_expression *) NULL; |
|
706 maybe_screwed_again--; |
|
707 tree_matrix *tmp = ml.pop (); |
|
708 tmp = tmp->reverse (); |
|
709 tree_return_list *id_list = tmp->to_return_list (); |
|
710 if (id_list == NULL_TREE) |
|
711 { |
|
712 yyerror ("parse error"); |
|
713 error ("invalid identifier list for assignment"); |
|
714 $$ = (tree_multi_assignment_expression *) NULL; |
|
715 ABORT_PARSE; |
|
716 } |
|
717 else |
|
718 $$ = new tree_multi_assignment_expression (id_list, $6); |
|
719 } |
|
720 | NUM '=' expression |
|
721 { |
|
722 yyerror ("parse error"); |
|
723 error ("invalid assignment to a number"); |
|
724 $$ = (tree_simple_assignment_expression *) NULL; |
|
725 ABORT_PARSE; |
|
726 } |
|
727 | simple_expr |
|
728 { $$ = $1; } |
|
729 ; |
|
730 |
|
731 simple_expr : simple_expr1 |
|
732 { $$ = $1; } |
|
733 | identifier PLUS_PLUS |
|
734 { $$ = new tree_postfix_expression ($1, tree::increment); } |
|
735 | identifier MINUS_MINUS |
|
736 { $$ = new tree_postfix_expression ($1, tree::decrement); } |
|
737 | simple_expr QUOTE |
|
738 { $$ = new tree_unary_expression ($1, tree::hermitian); } |
|
739 | simple_expr TRANSPOSE |
|
740 { $$ = new tree_unary_expression ($1, tree::transpose); } |
|
741 | simple_expr POW simple_expr |
|
742 { $$ = new tree_binary_expression ($1, $3, tree::power); } |
|
743 | simple_expr EPOW simple_expr |
|
744 { $$ = new tree_binary_expression ($1, $3, tree::elem_pow); } |
|
745 | simple_expr '+' simple_expr |
|
746 { $$ = new tree_binary_expression ($1, $3, tree::add); } |
|
747 | simple_expr '-' simple_expr |
|
748 { $$ = new tree_binary_expression ($1, $3, tree::subtract); } |
|
749 | simple_expr '*' simple_expr |
|
750 { $$ = new tree_binary_expression ($1, $3, tree::multiply); } |
|
751 | simple_expr '/' simple_expr |
|
752 { $$ = new tree_binary_expression ($1, $3, tree::divide); } |
|
753 | simple_expr EMUL simple_expr |
|
754 { $$ = new tree_binary_expression ($1, $3, tree::el_mul); } |
|
755 | simple_expr EDIV simple_expr |
|
756 { $$ = new tree_binary_expression ($1, $3, tree::el_div); } |
|
757 | simple_expr LEFTDIV simple_expr |
|
758 { $$ = new tree_binary_expression ($1, $3, tree::leftdiv); } |
|
759 | simple_expr ELEFTDIV simple_expr |
|
760 { $$ = new tree_binary_expression ($1, $3, |
|
761 tree::el_leftdiv); } |
|
762 | simple_expr EXPR_LT simple_expr |
|
763 { $$ = new tree_binary_expression ($1, $3, tree::cmp_lt); } |
|
764 | simple_expr EXPR_LE simple_expr |
|
765 { $$ = new tree_binary_expression ($1, $3, tree::cmp_le); } |
|
766 | simple_expr EXPR_EQ simple_expr |
|
767 { $$ = new tree_binary_expression ($1, $3, tree::cmp_eq); } |
|
768 | simple_expr EXPR_GE simple_expr |
|
769 { $$ = new tree_binary_expression ($1, $3, tree::cmp_ge); } |
|
770 | simple_expr EXPR_GT simple_expr |
|
771 { $$ = new tree_binary_expression ($1, $3, tree::cmp_gt); } |
|
772 | simple_expr EXPR_NE simple_expr |
|
773 { $$ = new tree_binary_expression ($1, $3, tree::cmp_ne); } |
|
774 | simple_expr EXPR_AND simple_expr |
|
775 { $$ = new tree_binary_expression ($1, $3, tree::and); } |
|
776 | simple_expr EXPR_OR simple_expr |
|
777 { $$ = new tree_binary_expression ($1, $3, tree::or); } |
|
778 ; |
|
779 |
|
780 simple_expr1 : NUM |
|
781 { $$ = new tree_constant ($1); } |
|
782 | IMAG_NUM |
|
783 { $$ = new tree_constant (Complex (0.0, $1)); } |
|
784 | TEXT |
|
785 { $$ = new tree_constant ($1); } |
|
786 | word_list_cmd |
|
787 { $$ = $1; } |
|
788 | '(' expression ')' |
|
789 { |
|
790 if ($2->is_assignment_expression ()) |
|
791 ((tree_assignment_expression *) $2) -> in_parens++; |
|
792 $$ = $2; |
|
793 } |
|
794 | variable |
|
795 { $$ = $1; } |
|
796 | matrix |
|
797 { $$ = $1; } |
|
798 | colon_expr |
|
799 { $$ = $1; } |
|
800 | PLUS_PLUS identifier %prec UNARY |
|
801 { $$ = new tree_prefix_expression ($2, tree::increment); } |
|
802 | MINUS_MINUS identifier %prec UNARY |
|
803 { $$ = new tree_prefix_expression ($2, tree::decrement); } |
|
804 | EXPR_NOT simple_expr |
|
805 { $$ = new tree_unary_expression ($2, tree::not); } |
|
806 | '+' simple_expr %prec UNARY |
|
807 { $$ = $2; } |
|
808 | '-' simple_expr %prec UNARY |
|
809 { $$ = new tree_unary_expression ($2, tree::uminus); } |
|
810 ; |
|
811 |
|
812 colon_expr : simple_expr ':' simple_expr |
|
813 { $$ = new tree_colon_expression ($1, $3); } |
|
814 | colon_expr ':' simple_expr |
|
815 { |
|
816 $$ = $1->chain ($3); |
|
817 if ($$ == (tree_colon_expression *) NULL) |
|
818 { |
|
819 yyerror ("parse error"); |
|
820 ABORT_PARSE; |
|
821 } |
|
822 } |
|
823 ; |
|
824 |
|
825 word_list_cmd : identifier word_list |
|
826 { $$ = new tree_word_list_command ($1, $2); } |
|
827 | CLEAR |
|
828 { |
|
829 if (defining_func) |
|
830 { |
|
831 yyerror ("parse error"); |
|
832 error ("clear: invalid within function body"); |
|
833 ABORT_PARSE; |
|
834 } |
|
835 symbol_record *sr = global_sym_tab->lookup ("clear", 1, 0); |
|
836 assert (sr != (symbol_record *) NULL); |
|
837 tree_identifier *tmp = new tree_identifier (sr); |
|
838 $$ = new tree_word_list_command (tmp, |
|
839 (tree_word_list *) NULL); |
|
840 } |
|
841 | CLEAR word_list |
|
842 { |
|
843 if (defining_func) |
|
844 { |
|
845 yyerror ("parse error"); |
|
846 error ("clear: invalid within function body"); |
|
847 ABORT_PARSE; |
|
848 } |
|
849 symbol_record *sr = global_sym_tab->lookup ("clear", 1, 0); |
|
850 assert (sr != (symbol_record *) NULL); |
|
851 tree_identifier *tmp = new tree_identifier (sr); |
|
852 $$ = new tree_word_list_command (tmp, $2); |
|
853 } |
|
854 ; |
|
855 |
|
856 word_list : word_list1 |
|
857 { $$ = $1->reverse (); } |
|
858 ; |
|
859 |
|
860 word_list1 : TEXT |
|
861 { $$ = new tree_word_list ($1); } |
|
862 | word_list1 TEXT |
|
863 { $$ = $1->chain ($2); } |
|
864 ; |
|
865 |
|
866 // This is truly disgusting. |
|
867 |
|
868 g_symtab : // empty |
|
869 { curr_sym_tab = global_sym_tab; } |
|
870 ; |
|
871 |
|
872 local_symtab : // empty |
|
873 { curr_sym_tab = tmp_local_sym_tab; } |
|
874 ; |
|
875 |
|
876 safe : // empty |
|
877 { maybe_screwed = 0; } |
|
878 ; |
|
879 |
|
880 are_we_screwed : // empty |
|
881 { maybe_screwed = 1; } |
|
882 ; |
|
883 |
|
884 func_def : FCN g_symtab are_we_screwed func_def1 |
|
885 { |
|
886 curr_sym_tab = top_level_sym_tab; |
|
887 defining_func = 0; |
|
888 $$ = (tree_function *) NULL; |
|
889 } |
|
890 | FCN g_symtab are_we_screwed func_def2 |
|
891 { |
|
892 curr_sym_tab = top_level_sym_tab; |
|
893 defining_func = 0; |
|
894 $$ = (tree_function *) NULL; |
|
895 } |
|
896 ; |
|
897 |
|
898 func_def1 : SCREW safe g_symtab '=' func_def2 |
|
899 { |
|
900 tree_identifier *tmp = new tree_identifier ($1); |
|
901 tree_parameter_list *tpl = new tree_parameter_list (tmp); |
|
902 tpl = tpl->reverse (); |
|
903 tpl->mark_as_formal_parameters (); |
|
904 $$ = $5->define_ret_list (tpl); |
|
905 } |
|
906 | func_def1a ']' g_symtab '=' func_def2 |
|
907 { |
|
908 tree_parameter_list *tpl = $1->reverse (); |
|
909 tpl->mark_as_formal_parameters (); |
|
910 $$ = $5->define_ret_list (tpl); |
|
911 } |
|
912 ; |
|
913 |
|
914 func_def1a : '[' safe local_symtab identifier |
|
915 { $$ = new tree_parameter_list ($4); } |
|
916 | func_def1a ',' identifier |
|
917 { $$ = $1->chain ($3); } |
|
918 ; |
|
919 |
|
920 func_def2 : identifier safe local_symtab func_def3 |
|
921 { |
|
922 char *id_name = $1->name (); |
|
923 // if (is_text_function_name (id_name)) |
|
924 // { |
|
925 // yyerror ("parse error"); |
|
926 // error ("invalid use of reserved word %s", id_name); |
|
927 // ABORT_PARSE; |
|
928 // } |
|
929 |
|
930 // If input is coming from a file, issue a warning if the name of the |
|
931 // file does not match the name of the function stated in the file. |
|
932 // Matlab doesn't provide a diagnostic (it ignores the stated name). |
|
933 |
|
934 if (reading_m_file) |
|
935 { |
|
936 if (strcmp (curr_m_file_name, id_name) != 0) |
|
937 warning ("function name `%s' does not agree\ |
|
938 with M-file name `%s.m'", id_name, curr_m_file_name); |
|
939 |
|
940 id_to_define->define ($4); |
|
941 id_to_define->document (help_buf); |
|
942 } |
|
943 else |
|
944 { |
|
945 if (reading_script_file |
|
946 && strcmp (curr_m_file_name, id_name) == 0) |
|
947 warning ("function `%s' defined within\ |
|
948 script file `%s.m'", id_name, curr_m_file_name); |
|
949 |
|
950 $1->define ($4); |
|
951 $1->document (help_buf); |
|
952 top_level_sym_tab->clear (id_name); |
|
953 } |
|
954 |
|
955 $$ = $4; |
|
956 } |
|
957 ; |
|
958 |
|
959 func_def3 : param_list optsep opt_list fcn_end_or_eof |
|
960 { |
|
961 tree_function *fcn = new tree_function ($3, curr_sym_tab); |
|
962 $$ = fcn->define_param_list ($1); |
|
963 } |
|
964 | '(' ')' optsep opt_list fcn_end_or_eof |
|
965 { $$ = new tree_function ($4, curr_sym_tab); } |
|
966 | optsep opt_list fcn_end_or_eof |
|
967 { $$ = new tree_function ($2, curr_sym_tab); } |
|
968 ; |
|
969 |
|
970 fcn_end_or_eof : END |
|
971 { |
|
972 if ($1 != function_end && $1 != simple_end) |
|
973 { |
|
974 yyerror ("parse error"); |
|
975 end_error ("function", $1); |
|
976 ABORT_PARSE; |
|
977 } |
|
978 |
|
979 if (reading_m_file) |
|
980 check_for_garbage_after_fcn_def (); |
|
981 } |
|
982 | END_OF_INPUT |
|
983 { |
|
984 if (! (reading_m_file || reading_script_file)) |
|
985 YYABORT; |
|
986 } |
|
987 ; |
|
988 |
|
989 variable : identifier |
|
990 { $$ = new tree_index_expression ($1); } |
|
991 | identifier '(' arg_list ')' |
|
992 { $$ = new tree_index_expression ($1, $3); } |
|
993 | identifier '(' ')' |
|
994 { |
|
995 $$ = new tree_index_expression |
|
996 ($1, (tree_argument_list *) NULL); |
|
997 } |
|
998 | identifier '[' |
|
999 { |
|
1000 yyerror ("parse error"); |
|
1001 error ("use `(\' and `)\' as index operators, not\ |
|
1002 `[\' and `]\'"); |
|
1003 $$ = (tree_index_expression *) NULL; |
|
1004 ABORT_PARSE; |
|
1005 } |
|
1006 ; |
|
1007 |
|
1008 param_list : param_list1 ')' |
|
1009 { |
|
1010 tree_parameter_list *tmp = $1->reverse (); |
|
1011 tmp->mark_as_formal_parameters (); |
|
1012 $$ = tmp; |
|
1013 } |
|
1014 |
|
1015 param_list1 : '(' identifier |
|
1016 { $$ = new tree_parameter_list ($2); } |
|
1017 | param_list1 ',' identifier |
|
1018 { $$ = $1->chain ($3); } |
|
1019 | '(' error |
|
1020 { |
|
1021 error ("parameter lists may only contain identifiers"); |
|
1022 $$ = (tree_parameter_list *) NULL; |
|
1023 } |
|
1024 | param_list1 ',' error |
|
1025 { |
|
1026 error ("parameter lists may only contain identifiers"); |
|
1027 $$ = (tree_parameter_list *) NULL; |
|
1028 } |
|
1029 ; |
|
1030 |
|
1031 identifier : NAME |
|
1032 { $$ = new tree_identifier ($1); } |
|
1033 |
|
1034 arg_list : arg_list1 |
|
1035 { $$ = $1->reverse (); } |
|
1036 ; |
|
1037 |
|
1038 arg_list1 : ':' |
|
1039 { |
|
1040 tree_constant *colon; |
|
1041 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1042 $$ = new tree_argument_list (colon); |
|
1043 } |
|
1044 | arg_list1 ',' ':' |
|
1045 { |
|
1046 tree_constant *colon; |
|
1047 colon = new tree_constant (tree_constant_rep::magic_colon); |
|
1048 $$ = $1->chain (colon); |
|
1049 if ($$ == NULL_TREE) |
|
1050 { |
|
1051 yyerror ("parse error"); |
|
1052 ABORT_PARSE; |
|
1053 } |
|
1054 } |
|
1055 | expression |
|
1056 { $$ = new tree_argument_list ($1); } |
|
1057 | arg_list1 ',' expression |
|
1058 { |
|
1059 $$ = $1->chain ($3); |
|
1060 if ($$ == NULL_TREE) |
|
1061 { |
|
1062 yyerror ("parse error"); |
|
1063 ABORT_PARSE; |
|
1064 } |
|
1065 } |
|
1066 ; |
|
1067 |
|
1068 matrix : '[' ']' |
|
1069 { |
|
1070 mlnm.pop (); |
|
1071 $$ = new tree_matrix (); |
|
1072 } |
|
1073 | '[' ';' ']' |
|
1074 { |
|
1075 mlnm.pop (); |
|
1076 $$ = new tree_matrix (); |
|
1077 } |
|
1078 | '[' screwed_again rows ']' |
|
1079 { |
|
1080 mlnm.pop (); |
|
1081 maybe_screwed_again--; |
|
1082 tree_matrix *tmp = ml.pop (); |
|
1083 $$ = tmp->reverse (); |
|
1084 } |
|
1085 ; |
|
1086 |
|
1087 rows : matrix_row |
|
1088 | rows ';' // Ignore trailing semicolon. |
|
1089 | rows ';' matrix_row |
|
1090 ; |
|
1091 |
|
1092 matrix_row : expression // First element on row. |
|
1093 { |
|
1094 if (mlnm.top ()) |
|
1095 { |
|
1096 mlnm.pop (); |
|
1097 mlnm.push (0); |
|
1098 tree_matrix *tmp = new tree_matrix ($1, tree::md_none); |
|
1099 ml.push (tmp); |
|
1100 } |
|
1101 else |
|
1102 { |
|
1103 tree_matrix *tmp = ml.pop (); |
|
1104 tmp = tmp->chain ($1, tree::md_down); |
|
1105 ml.push (tmp); |
|
1106 } |
|
1107 } |
|
1108 | matrix_row ',' // Ignore trailing comma. |
|
1109 | matrix_row ',' expression |
|
1110 { |
|
1111 tree_matrix *tmp = ml.pop (); |
|
1112 tmp = tmp->chain ($3, tree::md_right); |
|
1113 ml.push (tmp); |
|
1114 } |
|
1115 ; |
|
1116 |
|
1117 %% |
|
1118 |
|
1119 static void |
|
1120 yyerror (char *s) |
|
1121 { |
|
1122 char *line = current_input_line; |
|
1123 int err_col = current_input_column; |
|
1124 if (err_col == 0) |
|
1125 err_col = strlen (current_input_line) + 1; |
|
1126 |
|
1127 // Print a message like `parse error'. |
|
1128 fprintf (stderr, "\n%s", s); |
|
1129 |
|
1130 // Maybe print the line number and file name. |
|
1131 if (reading_m_file || reading_script_file) |
|
1132 fprintf (stderr, " near line %d of file %s.m", input_line_number, |
|
1133 curr_m_file_name); |
|
1134 |
|
1135 int len = strlen (line); |
|
1136 if (line[len-1] == '\n') |
|
1137 { |
|
1138 len--; |
|
1139 line[len] = '\0'; |
|
1140 } |
|
1141 |
|
1142 // Print the line, maybe with a pointer near the error token. |
|
1143 if (err_col > len) |
|
1144 fprintf (stderr, ":\n\n %s\n\n", line); |
|
1145 else |
|
1146 fprintf (stderr, ":\n\n %s\n %*s\n\n", line, err_col, "^"); |
|
1147 } |
|
1148 |
|
1149 static void |
|
1150 end_error (char *type, end_tok_type ettype) |
|
1151 { |
|
1152 switch (ettype) |
|
1153 { |
|
1154 case simple_end: |
|
1155 error ("%s command matched by `end'", type); |
|
1156 break; |
|
1157 case for_end: |
|
1158 error ("%s command matched by `endfor'", type); |
|
1159 break; |
|
1160 case function_end: |
|
1161 error ("%s command matched by `endfunction'", type); |
|
1162 break; |
|
1163 case if_end: |
|
1164 error ("%s command matched by `endif'", type); |
|
1165 break; |
|
1166 case while_end: |
|
1167 error ("%s command matched by `endwhile'", type); |
|
1168 break; |
|
1169 default: |
|
1170 panic_impossible (); |
|
1171 break; |
|
1172 } |
|
1173 } |
|
1174 |
|
1175 /* |
|
1176 * Need to make sure that the expression isn't already an identifier |
|
1177 * that has a name, or an assignment expression. |
|
1178 * |
|
1179 * Note that an expression can't be just an identifier anymore -- it |
|
1180 * must at least be an index expression (see the definition of the |
|
1181 * non-terminal `variable' above). |
|
1182 */ |
|
1183 tree * |
|
1184 maybe_convert_to_ans_assign (tree *expr) |
|
1185 { |
|
1186 tree *retval = expr; |
|
1187 |
|
1188 symbol_record *sr = global_sym_tab->lookup ("ans", 1, 0); |
|
1189 |
|
1190 assert (sr != (symbol_record *) NULL); |
|
1191 |
|
1192 if (expr->is_index_expression ()) |
|
1193 { |
|
1194 tree_index_expression *idx_expr = (tree_index_expression *) expr; |
|
1195 tree_argument_list *args = idx_expr->arg_list (); |
|
1196 |
|
1197 if (args == (tree_argument_list *) NULL) |
|
1198 { |
|
1199 tree_identifier *tmp = idx_expr->ident (); |
|
1200 tree *defn = tmp->def (); |
|
1201 if (defn != NULL_TREE && ! defn->is_builtin ()) |
|
1202 { |
|
1203 return retval; |
|
1204 } |
|
1205 } |
|
1206 } |
|
1207 else if (expr->is_assignment_expression ()) |
|
1208 { |
|
1209 return retval; |
|
1210 } |
|
1211 |
|
1212 tree_identifier *ans = new tree_identifier (sr); |
|
1213 retval = new tree_simple_assignment_expression (ans, expr); |
|
1214 |
|
1215 return retval; |
|
1216 } |
|
1217 |
|
1218 void |
|
1219 maybe_warn_assign_as_truth_value (tree *expr) |
|
1220 { |
|
1221 if (user_pref.warn_assign_as_truth_value |
|
1222 && expr->is_assignment_expression () |
|
1223 && ((tree_assignment_expression *) expr) -> in_parens < 2) |
|
1224 { |
|
1225 warning ("suggest parenthesis around assignment used as truth value"); |
|
1226 } |
|
1227 } |