-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmatlab_parse.yy
1698 lines (1455 loc) · 55.4 KB
/
matlab_parse.yy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
SujanK: Octave's parser used for parsing matlab
*/
/*
Copyright (C) 1993-2011 John W. Eaton
Copyright (C) 2009 David Grundberg
Copyright (C) 2009-2010 VZLU Prague
This file is part of Octave.
Octave is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
Octave is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING. If not, see
<http://www.gnu.org/licenses/>.
*/
// Parser for Octave.
// C decarations.
%{
#define YYDEBUG 1
#include "sage3basic.h"
#include <cassert>
#include <cstdlib>
#include <iostream>
#include "input.h"
#include "lex.h"
#include "ourtoken.h"
#include "StatementList.h"
#include "MatlabFunctionBuilder.h"
#if defined (GNULIB_NAMESPACE)
// Calls to the following functions appear in the generated output from
// Bison without the namespace tag. Redefine them so we will use them
// via the gnulib namespace.
#define fclose GNULIB_NAMESPACE::fclose
#define fprintf GNULIB_NAMESPACE::fprintf
#define malloc GNULIB_NAMESPACE::malloc
#endif
int yylex (void);
extern void yyerror (char const *);
// Reset state after parsing function.
static SgExpression*
make_indirect_ref (SgExpression*, SgExpression*);
static void
recover_from_parsing_function ();
int beginParse(SgProject* &p, int argc, char* argv[]);
SgIfStmt* getLastIfClause(SgIfStmt* topIfClause);
SgName extractVarName(SgExpression*);
class token;
bool parsingScriptFile;
SgProject* project;
SgScopeStatement* currentScope; //the current scope on the top of scope stack
int current_function_depth;
//SgFunctionDeclaration* defaultFunction;
#define ABORT_PARSE \
do \
{ \
yyerrok; \
YYABORT; \
} \
while (0)
%}
// Bison declarations.
// Don't add spaces around the = here; it causes some versions of
// bison to fail to properly recognize the directive.
//%name-prefix="matlab_"
%defines "matlab_parse.h"
%union
{
// The type of the basic tokens returned by the lexer.
std::string *Name_type;
PreprocessingInfo *Comment_type;
char sep_type;
SgMatrixExp* Matrix_type;
SgFunctionRefExp* FunctionRefExp_type;
SgVarRefExp* VarRefExp_type;
SgExpression* Expression_type;
SgRangeExp* Colon_Expression_type;
SgMagicColonExp* Symbols_type; //For magic_colon
SgFunctionParameterList* Function_Parameter_List_type;
SgExprListExp* ExprListExp_type;
SgValueExp* VauleExp_type;
SgAggregateInitializer* Aggregate_type;
SgFunctionDeclaration* Function_type;
SgScopeStatement* Loop_type;
SgIfStmt* IfStatement_type;
//SgMatlabDeclarationStatement* DeclarationStatement_type;
SgStatement* Statement_type;
SgStatement* Block_type;
MatlabFunctionBuilder* FunctionBuilder_type;
StatementList* StatementList_type;
token* tok_val;
/*
// Comment strings that we need to deal with mid-rule.
octave_comment_list *comment_type;
// Types for the nont`erminals we generate.
char sep_type;
tree *tree_type;
tree_matrix *tree_matrix_type;
tree_cell *tree_cell_type;
tree_expression *tree_expression_type;
tree_constant *tree_constant_type;
tree_fcn_handle *tree_fcn_handle_type;
tree_anon_fcn_handle *tree_anon_fcn_handle_type;
tree_identifier *tree_identifier_type;
tree_index_expression *tree_index_expression_type;
tree_colon_expression *tree_colon_expression_type;
tree_argument_list *tree_argument_list_type;
tree_parameter_list *tree_parameter_list_type;
tree_command *tree_command_type;
tree_if_command *tree_if_command_type;
tree_if_clause *tree_if_clause_type;
tree_if_command_list *tree_if_command_list_type;
tree_switch_command *tree_switch_command_type;
tree_switch_case *tree_switch_case_type;
tree_switch_case_list *tree_switch_case_list_type;
tree_decl_elt *tree_decl_elt_type;
tree_decl_init_list *tree_decl_init_list_type;
tree_decl_command *tree_decl_command_type;
tree_statement *tree_statement_type;
tree_statement_list *tree_statement_list_type;
octave_user_function *octave_user_function_type;
void *dummy_type;*/
}
// Tokens with line and column information.
%token <tok_val> '=' ':' '-' '+' '*' '/'
%token <tok_val> ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ
%token <tok_val> EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ AND_EQ OR_EQ
%token <tok_val> LSHIFT_EQ RSHIFT_EQ LSHIFT RSHIFT
%token <tok_val> EXPR_AND_AND EXPR_OR_OR
%token <tok_val> EXPR_AND EXPR_OR EXPR_NOT
%token <tok_val> EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT
%token <tok_val> LEFTDIV EMUL EDIV ELEFTDIV EPLUS EMINUS
%token <tok_val> QUOTE TRANSPOSE
%token <tok_val> PLUS_PLUS MINUS_MINUS POW EPOW
%token <tok_val> NUM IMAG_NUM
%token <tok_val> STRUCT_ELT
%token <tok_val> NAME
%token <tok_val> END
%token <tok_val> DQ_STRING SQ_STRING
%token <tok_val> FOR WHILE DO UNTIL
%token <tok_val> IF ELSEIF ELSE
%token <tok_val> SWITCH CASE OTHERWISE
%token <tok_val> BREAK CONTINUE FUNC_RET
%token <tok_val> UNWIND CLEANUP
%token <tok_val> TRY CATCH
%token <tok_val> GLOBAL STATIC
%token <tok_val> FCN_HANDLE
%token <tok_val> PROPERTIES
%token <tok_val> METHODS
%token <tok_val> EVENTS
%token <tok_val> METAQUERY
%token <tok_val> SUPERCLASSREF
%token <tok_val> GET SET
%token <tok_val> TYPE
// Other tokens.
%token END_OF_INPUT LEXICAL_ERROR
%token FCN SCRIPT_FILE FUNCTION_FILE CLASSDEF
// %token VARARGIN VARARGOUT
%token CLOSE_BRACE
//%token TYPE
%type <Comment_type> stash_comment function_beg
%type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep
%type <StatementList_type> input
%type <VauleExp_type> string constant
%type <Symbols_type> magic_colon
%type <FunctionRefExp_type> fcn_handle
%type <Matrix_type> matrix_rows matrix_rows1
//%type <ExprListExp_type> matrix_rows matrix_rows1
%type <ExprListExp_type> cell_rows cell_rows1
%type <Matrix_type> matrix
//%type <Expression_type> matrix cell assign_lhs
%type <Expression_type> cell assign_lhs
%type <Expression_type> primary_expr postfix_expr prefix_expr binary_expr
%type <Expression_type> simple_expr colon_expr assign_expr expression
%type <VarRefExp_type> identifier// fcn_name
%type <Name_type> fcn_name
%type <FunctionBuilder_type> function1 function2
%type <Statement_type> word_list_cmd
%type <Colon_Expression_type> colon_expr1
%type <ExprListExp_type> arg_list word_list //assign_lhs
//%type <Aggregate_type> cell_or_matrix_row
%type <ExprListExp_type> cell_or_matrix_row
%type <Function_Parameter_List_type> param_list param_list1 param_list2
%type <ExprListExp_type> return_list return_list1
%type <Block_type> command
%type <StatementList_type> script_file
%type <Loop_type> loop_command
%type <Function_type> function
%type <IfStatement_type> if_command
%type <Statement_type> else_clause
%type <IfStatement_type> elseif_clause if_cmd_list1 if_cmd_list
%type <Expression_type> decl2
%type <ExprListExp_type> decl1
//%type <DeclarationStatement_type> declaration
%type <Statement_type> statement
//%type <Block_type> simple_list simple_list1 list list1
//%type <Block_type> opt_list input1
%type <StatementList_type> simple_list simple_list1 list list1
%type <StatementList_type> opt_list input1
/*
// Nonterminals we construct.
%type <comment_type> stash_comment function_beg classdef_beg
%type <comment_type> properties_beg methods_beg events_beg
%type <sep_type> sep_no_nl opt_sep_no_nl sep opt_sep
%type <tree_type> input
%type <tree_constant_type> string constant magic_colon
%type <tree_anon_fcn_handle_type> anon_fcn_handle
%type <tree_fcn_handle_type> fcn_handle
%type <tree_matrix_type> matrix_rows matrix_rows1
%type <tree_cell_type> cell_rows cell_rows1
%type <tree_expression_type> matrix cell
%type <tree_expression_type> primary_expr postfix_expr prefix_expr binary_expr
%type <tree_expression_type> simple_expr colon_expr assign_expr expression
%type <tree_identifier_type> identifier fcn_name magic_tilde
%type <tree_identifier_type> superclass_identifier meta_identifier
%type <octave_user_function_type> function1 function2 classdef1
%type <tree_index_expression_type> word_list_cmd
%type <tree_colon_expression_type> colon_expr1
%type <tree_argument_list_type> arg_list word_list assign_lhs
%type <tree_argument_list_type> cell_or_matrix_row
%type <tree_parameter_list_type> param_list param_list1 param_list2
%type <tree_parameter_list_type> return_list return_list1
%type <tree_parameter_list_type> superclasses opt_superclasses
%type <tree_command_type> command select_command loop_command
%type <tree_command_type> jump_command except_command function
%type <tree_command_type> script_file classdef
%type <tree_command_type> function_file function_list
%type <tree_if_command_type> if_command
%type <tree_if_clause_type> elseif_clause else_clause
%type <tree_if_command_list_type> if_cmd_list1 if_cmd_list
%type <tree_switch_command_type> switch_command
%type <tree_switch_case_type> switch_case default_case
%type <tree_switch_case_list_type> case_list1 case_list
%type <tree_decl_elt_type> decl2
%type <tree_decl_init_list_type> decl1
%type <tree_decl_command_type> declaration
%type <tree_statement_type> statement function_end classdef_end
%type <tree_statement_list_type> simple_list simple_list1 list list1
%type <tree_statement_list_type> opt_list input1
// These types need to be specified.
%type <dummy_type> attr
%type <dummy_type> class_event
%type <dummy_type> class_property
%type <dummy_type> properties_list
%type <dummy_type> properties_block
%type <dummy_type> methods_list
%type <dummy_type> methods_block
%type <dummy_type> opt_attr_list
%type <dummy_type> attr_list
%type <dummy_type> events_list
%type <dummy_type> events_block
%type <dummy_type> class_body
// symrec *tptr;
*/
// Precedence and associativity.
%left ';' ',' '\n'
%right '=' ADD_EQ SUB_EQ MUL_EQ DIV_EQ LEFTDIV_EQ POW_EQ EMUL_EQ EDIV_EQ ELEFTDIV_EQ EPOW_EQ OR_EQ AND_EQ LSHIFT_EQ RSHIFT_EQ
%left EXPR_OR_OR
%left EXPR_AND_AND
%left EXPR_OR
%left EXPR_AND
%left EXPR_LT EXPR_LE EXPR_EQ EXPR_NE EXPR_GE EXPR_GT
%left LSHIFT RSHIFT
%left ':'
%left '-' '+' EPLUS EMINUS
%left '*' '/' LEFTDIV EMUL EDIV ELEFTDIV
%left UNARY PLUS_PLUS MINUS_MINUS EXPR_NOT
%left POW EPOW QUOTE TRANSPOSE
%left '(' '.' '{'
// Where to start.
%start input
%%
// ==============================
// Statements and statement lists
// ==============================
input : input1
{
$1->appendAll();
if(parsingScriptFile)
{
SageBuilder::popScopeStack(); //pop out the default function
//Create a default "run" function
SgFunctionDeclaration *defaultFunction = SageBuilder::buildDefiningFunctionDeclaration("run",
SageBuilder::buildVoidType(), SageBuilder::buildFunctionParameterList(),
SageInterface::getFirstGlobalScope(project));
SageInterface::removeStatement(defaultFunction->get_definition()->get_body());
defaultFunction->get_definition()->set_body(isSgBasicBlock(currentScope));
//Add the default function to global scope
SageInterface::appendStatement(defaultFunction);
}
//Pop the global stack
SageBuilder::popScopeStack();
YYACCEPT;
globalCommand = true;
}
| function_file
{YYACCEPT;}
| simple_list parse_error
{ABORT_PARSE;}
| parse_error
{ABORT_PARSE;}
;
input1 : '\n'
{$$ = 0;}
| END_OF_INPUT
{
parser_end_of_input = 1;
$$ = 0;
}
| simple_list
{$$ = $1;}
| simple_list '\n'
{$$ = $1;}
| simple_list END_OF_INPUT
{$$ = $1;}
;
simple_list : simple_list1 opt_sep_no_nl
{ $$ = $1;}
;
simple_list1 : statement
{
$$ = dynamic_cast<StatementList*>($1);
assert($$ != NULL);
}
| simple_list1 sep_no_nl statement
{
ROSE_ASSERT(false);
}
;
opt_list : // empty
{ $$ = new StatementList();}
| list
{
$$ = $1;
}
;
list : list1 opt_sep
{ $$ = $1;}
;
list1 : statement
{
ROSE_ASSERT($1 != NULL);
$$ = new StatementList($1);
}
| list1 sep statement
{
$1->appendStatement($3);
$$ = $1;
assert($$ != NULL);
}
;
statement : expression
{$$ = SageBuilder::buildExprStatement($1);}
| command
{$$ = $1;}
| word_list_cmd
{
$$ = $1;
ROSE_ASSERT($$ != NULL);
}
;
// =================
// Word-list command
// =================
// These are not really like expressions since they can't appear on
// the RHS of an assignment. But they are also not like commands (IF,
// WHILE, etc.
word_list_cmd : identifier word_list
{
//$$ = SageMatlabBuilder::buildWordListStatement($1->get_symbol()->get_name(), $2);
}
;
word_list : string
{ $$ = SageBuilder::buildExprListExp($1);}
| word_list string
{
SageInterface::appendExpression($1, $2);
$$ = $1;
}
;
// ===========
// Expressions
// ===========
identifier : NAME
{
std::string varName = $1->text();
SgScopeStatement *activeScope = SageBuilder::topScopeStack();
SgVariableSymbol *varSymbol = SageInterface::lookupVariableSymbolInParentScopes(varName, activeScope);
if(varSymbol == NULL)
{
SgVarRefExp *varRef = SageBuilder::buildVarRefExp(varName, activeScope);
/*If there is no explicit variable declaration, we have to manually insert the symbol to the symbol table*/
activeScope->get_symbol_table()->insert(varName, varRef->get_symbol());
$$ = varRef;
}
else
{
/*The symbol already exists. The varref will point to the existing symbol.*/
$$ = SageBuilder::buildVarRefExp(varName, activeScope);
}
}
;
superclass_identifier
: SUPERCLASSREF
/*{ $$ = new tree_identifier ($1->line (), $1->column ());}*/
;
meta_identifier : METAQUERY
/*{ $$ = new tree_identifier ($1->line (), $1->column ());}*/
;
string : DQ_STRING
{ $$ = SageBuilder::buildStringVal($1->text());}
| SQ_STRING
{ $$ = SageBuilder::buildStringVal($1->text());}
;
constant : NUM
{ $$ = SageBuilder::buildDoubleVal($1->number());}
| IMAG_NUM
{ $$ = SageBuilder::buildImaginaryVal ($1->number()); }
| string
{ $$ = $1;}
;
matrix : '[' ']'
{
lexer_flags.looking_at_matrix_or_assign_lhs = false;
lexer_flags.pending_local_variables.clear ();
}
| '[' ';' ']'
{
lexer_flags.looking_at_matrix_or_assign_lhs = false;
lexer_flags.pending_local_variables.clear ();
}
| '[' ',' ']'
{
lexer_flags.looking_at_matrix_or_assign_lhs = false;
lexer_flags.pending_local_variables.clear ();
}
| '[' matrix_rows ']'
{
$$ = $2;
lexer_flags.looking_at_matrix_or_assign_lhs = false;
lexer_flags.pending_local_variables.clear ();
}
;
matrix_rows : matrix_rows1
{ $$ = $1;}
| matrix_rows1 ';' // Ignore trailing semicolon.
{ $$ = $1;}
;
matrix_rows1 : cell_or_matrix_row
{
$$ = SageBuilder::buildMatrixExp($1);
}
| matrix_rows1 ';' cell_or_matrix_row
{
$1->append_expression($3);
$$ = $1;
assert($$ != NULL);
}
;
cell : '{' '}'
/*{ $$ = new tree_constant (octave_value (Cell ()));}*/
| '{' ';' '}'
/*{ $$ = new tree_constant (octave_value (Cell ()));}*/
| '{' cell_rows '}'
{
$$ = SageBuilder::buildAggregateInitializer($2);
}
;
cell_rows : cell_rows1
{$$ = $1;}
| cell_rows1 ';' // Ignore trailing semicolon.
{$$ = $1;}
;
cell_rows1 : cell_or_matrix_row
{
$$ = SageBuilder::buildExprListExp($1);
}
| cell_rows1 ';' cell_or_matrix_row
{
SageInterface::appendExpression($1, $3);
$$ = $1;
}
;
cell_or_matrix_row
: arg_list
{
$$ = $1;
}
| arg_list ',' // Ignore trailing comma.
{
$$ = $1;
}
;
fcn_handle : '@' FCN_HANDLE
{
SgName fcnName($2->text());
SgFunctionDeclaration* fcnDeclaration = SageBuilder::buildNondefiningFunctionDeclaration(fcnName, SageBuilder::buildUnknownType(), SageBuilder::buildFunctionParameterList(), currentScope);
SgFunctionSymbol* fcnSymbol = new SgFunctionSymbol(fcnDeclaration);
$$ = SageBuilder::buildFunctionRefExp(fcnSymbol);
lexer_flags.looking_at_function_handle--;
}
;
anon_fcn_handle : '@' param_list statement
/*{$$ = make_anon_fcn_handle ($2, $3);}*/
;
primary_expr : identifier
{$$ = $1;}
| constant
{$$ = $1;}
| fcn_handle
/*{$$ = $1;}*/
| matrix
{$$ = $1;}
| cell
{$$ = $1;}
| meta_identifier
/*{$$ = $1;}*/
| superclass_identifier
/*{$$ = $1;}*/
| '(' expression ')' //TODO
{ $$ = $2; }
/*{$$ = $2->mark_in_parens ();}*/
;
magic_colon : ':'
{
$$ = SageBuilder::buildMagicColonExp();
}
;
magic_tilde : EXPR_NOT
/*{
$$ = new tree_black_hole ();
}*/
;
arg_list : expression
{$$ = SageBuilder::buildExprListExp($1);}
| magic_colon
{$$ = SageBuilder::buildExprListExp ($1);}
| magic_tilde
| arg_list ',' magic_colon
{
$1->append_expression ($3);
$$ = $1;
}
| arg_list ',' magic_tilde
/*{
$1->append ($3);
$$ = $1;
}*/
| arg_list ',' expression
{
$1->append_expression ($3);
$$ = $1;
}
;
indirect_ref_op : '.'
{lexer_flags.looking_at_indirect_ref = true;}
;
postfix_expr : primary_expr
{$$ = $1;}
| postfix_expr '(' ')'
{ $$ = SageBuilder::buildFunctionCallExp($1);}
| postfix_expr '(' arg_list ')'
{
//Treat every indexed operation as a function call
$$ = SageBuilder::buildFunctionCallExp($1, $3);
}
| postfix_expr '{' '}'
{ $$ = SageBuilder::buildFunctionCallExp($1);}
| postfix_expr '{' arg_list '}'
{ $$ = SageBuilder::buildFunctionCallExp($1, $3); }
| postfix_expr PLUS_PLUS
{$$ = SageBuilder::buildPlusPlusOp($1, SgUnaryOp::postfix);}
| postfix_expr MINUS_MINUS
{$$ = SageBuilder::buildMinusMinusOp($1, SgUnaryOp::postfix);}
| postfix_expr QUOTE
{
SgMatrixTransposeOp *transposeOp = SageBuilder::buildMatrixTransposeOp($1);
transposeOp->set_is_conjugate(true);
$$ = transposeOp;
}
| postfix_expr TRANSPOSE
{
$$ = SageBuilder::buildMatrixTransposeOp($1);
}
| postfix_expr indirect_ref_op STRUCT_ELT
{$$ = make_indirect_ref ($1, SageBuilder::buildVarRefExp($3->text()));}
| postfix_expr indirect_ref_op '(' expression ')'
{$$ = make_indirect_ref ($1, $4);}
;
prefix_expr : postfix_expr
{$$ = $1;}
| binary_expr
{$$ = $1;}
| PLUS_PLUS prefix_expr %prec UNARY
{
$$ = SageBuilder::buildPlusPlusOp($2, SgUnaryOp::prefix);
/*Turns out that Matlab does not support ++,-- operators*/
}
| MINUS_MINUS prefix_expr %prec UNARY
{
$$ = SageBuilder::buildMinusMinusOp($2, SgUnaryOp::prefix);
}
| EXPR_NOT prefix_expr %prec UNARY
{ $$ = SageBuilder::buildNotOp($2);}
| '+' prefix_expr %prec UNARY
{ $$ = $2;}
| '-' prefix_expr %prec UNARY
{ $$ = SageBuilder::buildMinusOp($2, SgUnaryOp::prefix);}
;
binary_expr : prefix_expr POW prefix_expr
{$$ = SageBuilder::buildPowerOp($1, $3);}
| prefix_expr EPOW prefix_expr
{$$ = SageBuilder::buildElementwisePowerOp($1, $3);}
| prefix_expr '+' prefix_expr
{$$ = SageBuilder::buildAddOp($1, $3);}
| prefix_expr '-' prefix_expr
{$$ = SageBuilder::buildSubtractOp($1, $3);}
| prefix_expr '*' prefix_expr
{$$ = SageBuilder::buildMultiplyOp($1, $3);}
| prefix_expr '/' prefix_expr
{$$ = SageBuilder::buildDivideOp($1, $3);}
| prefix_expr EPLUS prefix_expr
{ $$ = SageBuilder::buildElementwiseAddOp($1, $3);}
| prefix_expr EMINUS prefix_expr
{ $$ = SageBuilder::buildElementwiseSubtractOp($1, $3);}
| prefix_expr EMUL prefix_expr
{ $$ = SageBuilder::buildElementwiseMultiplyOp($1, $3);}
| prefix_expr EDIV prefix_expr
{ $$ = SageBuilder::buildElementwiseDivideOp($1, $3);}
| prefix_expr LEFTDIV prefix_expr
{ $$ = SageBuilder::buildLeftDivideOp($1, $3);}
| prefix_expr ELEFTDIV prefix_expr
{ $$ = SageBuilder::buildElementwiseLeftDivideOp($1, $3);}
;
colon_expr : prefix_expr
{
$$ = $1;
}
| colon_expr1
{
$$ = $1;
}
;
colon_expr1 : prefix_expr
{
$$ = SageBuilder::buildRangeExp($1);
}
| colon_expr1 ':' prefix_expr
{
if(($$ = $1->append($3)) == NULL)
ABORT_PARSE;
}
;
simple_expr : colon_expr
{$$ = $1;}
| simple_expr LSHIFT simple_expr
{
$$ = SageBuilder::buildLshiftOp($1, $3);
}
| simple_expr RSHIFT simple_expr
{
$$ = SageBuilder::buildRshiftOp($1, $3);
}
| simple_expr EXPR_LT simple_expr
{
$$ = SageBuilder::buildLessThanOp($1, $3);
}
| simple_expr EXPR_LE simple_expr
{
$$ = SageBuilder::buildLessOrEqualOp($1, $3);
}
| simple_expr EXPR_EQ simple_expr
{
$$ = SageBuilder::buildEqualityOp($1, $3);
}
| simple_expr EXPR_GE simple_expr
{
$$ = SageBuilder::buildGreaterOrEqualOp($1, $3);
}
| simple_expr EXPR_GT simple_expr
{
$$ = SageBuilder::buildGreaterThanOp($1, $3);
}
| simple_expr EXPR_NE simple_expr
{
$$ = SageBuilder::buildNotEqualOp($1, $3);
}
| simple_expr EXPR_AND simple_expr
{
$$ = SageBuilder::buildBitAndOp($1, $3);
}
| simple_expr EXPR_OR simple_expr
{
$$ = SageBuilder::buildBitOrOp($1, $3);
}
| simple_expr EXPR_AND_AND simple_expr
{
$$ = SageBuilder::buildAndOp($1, $3);
}
| simple_expr EXPR_OR_OR simple_expr
{
$$ = SageBuilder::buildOrOp($1, $3);
}
;
// Arrange for the lexer to return CLOSE_BRACE for `]' by looking ahead
// one token for an assignment op.
assign_lhs : simple_expr
{
$$ = $1;
}
/*{
$$ = new tree_argument_list ($1);
$$->mark_as_simple_assign_lhs ();
}*/
| '[' arg_list CLOSE_BRACE
{
$$ = $2;
lexer_flags.looking_at_matrix_or_assign_lhs = false;
/*for (std::set<std::string>::const_iterator p = lexer_flags.pending_local_variables.begin ();
p != lexer_flags.pending_local_variables.end ();
p++)
{
symbol_table::force_variable (*p);
}*/
lexer_flags.pending_local_variables.clear ();
}
;
assign_expr : assign_lhs '=' expression
{
$$ = SageBuilder::buildAssignOp($1, $3);
}
| assign_lhs ADD_EQ expression
/*{$$ = make_assign_op (ADD_EQ, $1, $2, $3);}*/
| assign_lhs SUB_EQ expression
/*{$$ = make_assign_op (SUB_EQ, $1, $2, $3);}*/
| assign_lhs MUL_EQ expression
/*{$$ = make_assign_op (MUL_EQ, $1, $2, $3);}*/
| assign_lhs DIV_EQ expression
/*{$$ = make_assign_op (DIV_EQ, $1, $2, $3);}*/
| assign_lhs LEFTDIV_EQ expression
/*{$$ = make_assign_op (LEFTDIV_EQ, $1, $2, $3);}*/
| assign_lhs POW_EQ expression
/*{$$ = make_assign_op (POW_EQ, $1, $2, $3);}*/
| assign_lhs LSHIFT_EQ expression
/*{$$ = make_assign_op (LSHIFT_EQ, $1, $2, $3);}*/
| assign_lhs RSHIFT_EQ expression
/*{$$ = make_assign_op (RSHIFT_EQ, $1, $2, $3);}*/
| assign_lhs EMUL_EQ expression
/*{$$ = make_assign_op (EMUL_EQ, $1, $2, $3);}*/
| assign_lhs EDIV_EQ expression
/*{$$ = make_assign_op (EDIV_EQ, $1, $2, $3);}*/
| assign_lhs ELEFTDIV_EQ expression
/*{$$ = make_assign_op (ELEFTDIV_EQ, $1, $2, $3);}*/
| assign_lhs EPOW_EQ expression
/*{$$ = make_assign_op (EPOW_EQ, $1, $2, $3);}*/
| assign_lhs AND_EQ expression
/*{$$ = make_assign_op (AND_EQ, $1, $2, $3);}*/
| assign_lhs OR_EQ expression
/*{$$ = make_assign_op (OR_EQ, $1, $2, $3);}*/
;
expression : simple_expr
{$$ = $1;}
| assign_expr
{$$ = $1;}
| anon_fcn_handle
/*{$$ = $1;}*/
;
// ================================================
// Commands, declarations, and function definitions
// ================================================
command :// declaration
// {$$ = $1;}
// |
select_command
/*{$$ = $1;}*/
| loop_command
/*{$$ = $1;}*/
| jump_command
/*{$$ = $1;}*/
| except_command
/*{$$ = $1;}*/
| function
{$$ = $1;}
| script_file
{$$ = $1;}
| classdef
/*{$$ = $1;}*/
;
// =====================
// Declaration statemnts
// =====================
parsing_decl_list
: // empty
{lexer_flags.looking_at_decl_list = true;}
declaration : GLOBAL parsing_decl_list decl1
{
//$$ = SageMatlabBuilder::buildGlobalDeclarationStatement($3);
//$$ = make_decl_command (GLOBAL, $1, $3);
lexer_flags.looking_at_decl_list = false;
}
| STATIC parsing_decl_list decl1
{
//$$ = SageMatlabBuilder::buildStaticDeclarationStatement($3);
//$$ = make_decl_command (STATIC, $1, $3);
lexer_flags.looking_at_decl_list = false;
}
;
decl1 : decl2
{
$$ = SageBuilder::buildExprListExp($1);
}
| decl1 decl2
{
SageInterface::appendExpression($1, $2);
}
;
decl_param_init : // empty
{lexer_flags.looking_at_initializer_expression = true;}
decl2 : identifier
{ $$ = $1;}
/*{$$ = new tree_decl_elt ($1);}*/
| identifier '=' decl_param_init expression
{
lexer_flags.looking_at_initializer_expression = false;
$$ = SageBuilder::buildAssignOp($1, $4);
}
| magic_tilde
/*{
$$ = new tree_decl_elt ($1);
}*/
;
// ====================
// Selection statements
// ====================
select_command : if_command
/*{$$ = $1;}*/
| switch_command
/*{$$ = $1;}*/
;
// ============
// If statement
// ============
if_command : IF stash_comment if_cmd_list END
{
$$ = $3;
}
;
if_cmd_list : if_cmd_list1
{$$ = $1;}
| if_cmd_list1 else_clause
{
SgIfStmt* lastIfClause = getLastIfClause($1);
lastIfClause->set_false_body($2);
$2->set_parent(lastIfClause);
$$ = $1;
}
;
if_cmd_list1 : expression opt_sep opt_list
{
$$ = SageBuilder::buildIfStmt($1, $3->getBasicBlock(), NULL);
}
| if_cmd_list1 elseif_clause
{
SgIfStmt* lastIfClause = getLastIfClause($1);
lastIfClause->set_false_body($2);
$2->set_parent(lastIfClause);
$$ = $1;
}
;
elseif_clause : ELSEIF stash_comment opt_sep expression opt_sep opt_list
{
$$ = SageBuilder::buildIfStmt($4, $6->getBasicBlock(), NULL);
}
;
else_clause : ELSE stash_comment opt_sep opt_list
{
$$ = $4->getBasicBlock(); //Ignore the comments
}
;