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