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