stage1_2/iec.y
changeset 91 ac119a256fc0
parent 90 dc6df1511570
child 92 a1b03af4b26a
equal deleted inserted replaced
90:dc6df1511570 91:ac119a256fc0
     1 /*
     1 /*
     2  * (c) 2003 Mario de Sousa
     2  * (c) 2003-2007 Mario de Sousa
     3  *
     3  *
     4  * Offered to the public under the terms of the GNU General Public License
     4  * Offered to the public under the terms of the GNU General Public License
     5  * as published by the Free Software Foundation; either version 2 of the
     5  * as published by the Free Software Foundation; either version 2 of the
     6  * License, or (at your option) any later version.
     6  * License, or (at your option) any later version.
     7  *
     7  *
    13  * This code is made available on the understanding that it will not be
    13  * This code is made available on the understanding that it will not be
    14  * used in safety-critical situations without a full and competent review.
    14  * used in safety-critical situations without a full and competent review.
    15  */
    15  */
    16 
    16 
    17 /*
    17 /*
    18  * An IEC 61131-3 IL and ST compiler.
    18  * An IEC 61131-3 IL, ST and SFC compiler.
    19  *
    19  *
    20  * Based on the
    20  * Based on the
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    21  * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
    22  *
    22  *
    23  */
    23  */
    25 /*
    25 /*
    26  * Stage 2
    26  * Stage 2
    27  * =======
    27  * =======
    28  *
    28  *
    29  *  This file contains the syntax definition of the textual
    29  *  This file contains the syntax definition of the textual
    30  *  languages IL and ST. The syntax parser, comprising the
    30  *  languages IL and ST, as well as the textual version of SFC.
    31  *  2nd stage of the overall compiler, is generated by runing
    31  *  The syntax parser, comprising the 2nd stage of the overall 
    32  *  bison on this file.
    32  *  compiler, is generated by runing bison on this file.
    33  */
    33  */
    34 
    34 
    35 
    35 
    36 
    36 
    37 
    37 
   170 
   170 
   171 %union {
   171 %union {
   172     symbol_c 	*leaf;
   172     symbol_c 	*leaf;
   173     list_c	*list;
   173     list_c	*list;
   174     char 	*ID;	/* token value */
   174     char 	*ID;	/* token value */
       
   175 /*
   175     struct {
   176     struct {
   176       symbol_c	*first;
   177       symbol_c	*first;
   177       symbol_c	*second;
   178       symbol_c	*second;
   178     } tmp_symbol; /* used as a temorary reference to symbols by:
   179     } tmp_symbol;
       
   180 */ 
       
   181 /* used as a temorary reference to symbols by:
   179                                      il_simple_operator_clash_il_operand
   182                                      il_simple_operator_clash_il_operand
   180                              */
   183                              */
   181 }
   184 }
   182 
   185 
   183 /*
   186 /*
   971 %type  <leaf>	il_instruction
   974 %type  <leaf>	il_instruction
   972 %type  <leaf>	il_incomplete_instruction
   975 %type  <leaf>	il_incomplete_instruction
   973 %type  <leaf>	label
   976 %type  <leaf>	label
   974 %type  <leaf>	il_simple_operation
   977 %type  <leaf>	il_simple_operation
   975 // helper symbol for il_simple_operation
   978 // helper symbol for il_simple_operation
   976 %type <tmp_symbol> il_simple_operator_clash_il_operand
   979 //%type <tmp_symbol> il_simple_operator_clash_il_operand
   977 %type  <leaf>	il_expression
   980 %type  <leaf>	il_expression
   978 %type  <leaf>	il_jump_operation
   981 %type  <leaf>	il_jump_operation
   979 %type  <leaf>	il_fb_call
   982 %type  <leaf>	il_fb_call
   980 %type  <leaf>	il_formal_funct_call
   983 %type  <leaf>	il_formal_funct_call
   981 // helper symbol for il_formal_funct_call
   984 // helper symbol for il_formal_funct_call
   982 %type  <leaf> il_expr_operator_clash_eol_list
   985 %type  <leaf> il_expr_operator_clash_eol_list
   983 %type  <leaf>	il_operand
   986 %type  <leaf>	il_operand
   984 %type  <list>	il_operand_list
   987 %type  <list>	il_operand_list
       
   988 // helper symbol for il_simple_operation
       
   989 %type  <list>	il_operand_list2
   985 %type  <list>	simple_instr_list
   990 %type  <list>	simple_instr_list
   986 %type  <leaf>	il_simple_instruction
   991 %type  <leaf>	il_simple_instruction
   987 %type  <list>	il_param_list
   992 %type  <list>	il_param_list
   988 %type  <list>	il_param_instruction_list
   993 %type  <list>	il_param_instruction_list
   989 %type  <leaf>	il_param_instruction
   994 %type  <leaf>	il_param_instruction
  3286 standard_function_name_simpleop_only_clashes:
  3291 standard_function_name_simpleop_only_clashes:
  3287 ;
  3292 ;
  3288 */
  3293 */
  3289 
  3294 
  3290 standard_function_name_expression_clashes:
  3295 standard_function_name_expression_clashes:
       
  3296   AND	{$$ = new identifier_c(strdup("AND"), locloc(@$));}
       
  3297 | OR	{$$ = new identifier_c(strdup("OR"), locloc(@$));}
       
  3298 | XOR	{$$ = new identifier_c(strdup("XOR"), locloc(@$));}
       
  3299 | ADD	{$$ = new identifier_c(strdup("ADD"), locloc(@$));}
       
  3300 | SUB	{$$ = new identifier_c(strdup("SUB"), locloc(@$));}
       
  3301 | MUL	{$$ = new identifier_c(strdup("MUL"), locloc(@$));}
       
  3302 | DIV	{$$ = new identifier_c(strdup("DIV"), locloc(@$));}
       
  3303 | MOD	{$$ = new identifier_c(strdup("MOD"), locloc(@$));}
       
  3304 | GT	{$$ = new identifier_c(strdup("GT"), locloc(@$));}
       
  3305 | GE	{$$ = new identifier_c(strdup("GE"), locloc(@$));}
       
  3306 | EQ	{$$ = new identifier_c(strdup("EQ"), locloc(@$));}
       
  3307 | LT	{$$ = new identifier_c(strdup("LT"), locloc(@$));}
       
  3308 | LE	{$$ = new identifier_c(strdup("LE"), locloc(@$));}
       
  3309 | NE	{$$ = new identifier_c(strdup("NE"), locloc(@$));}
       
  3310 /*
  3291   AND_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3311   AND_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3292 //NOTE: AND2 (corresponding to the source code string '&') does not clash
  3312 //NOTE: AND2 (corresponding to the source code string '&') does not clash
  3293 //      with a standard function name, so should be commented out!
  3313 //      with a standard function name, so should be commented out!
  3294 //| AND2_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3314 //| AND2_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3295 | OR_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3315 | OR_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3303 | GE_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3323 | GE_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3304 | EQ_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3324 | EQ_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3305 | LT_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3325 | LT_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3306 | LE_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3326 | LE_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3307 | NE_operator	{$$ = il_operator_c_2_identifier_c($1);}
  3327 | NE_operator	{$$ = il_operator_c_2_identifier_c($1);}
       
  3328 */
  3308 ;
  3329 ;
  3309 
  3330 
  3310 
  3331 
  3311 derived_function_name:
  3332 derived_function_name:
  3312   identifier
  3333   identifier
  4358 il_simple_operation:
  4379 il_simple_operation:
  4359   il_simple_operator
  4380   il_simple_operator
  4360 	{$$ = new il_simple_operation_c($1, NULL, locloc(@$));}
  4381 	{$$ = new il_simple_operation_c($1, NULL, locloc(@$));}
  4361 | il_simple_operator_noclash il_operand
  4382 | il_simple_operator_noclash il_operand
  4362 	{$$ = new il_simple_operation_c($1, $2, locloc(@$));}
  4383 	{$$ = new il_simple_operation_c($1, $2, locloc(@$));}
       
  4384 /*
  4363 | il_simple_operator_clash_il_operand
  4385 | il_simple_operator_clash_il_operand
  4364 	{$$ = new il_simple_operation_c($1.first, $1.second, locloc(@$));}
  4386 	{$$ = new il_simple_operation_c($1.first, $1.second, locloc(@$));}
       
  4387 */
       
  4388 | il_simple_operator_clash il_operand
       
  4389 	{$$ = new il_simple_operation_c($1, $2, locloc(@$));}
  4365 /* NOTE: the line
  4390 /* NOTE: the line
  4366  *         | il_simple_operator
  4391  *         | il_simple_operator
  4367  *       already contains the 'NOT' operator, as well as all the
  4392  *       already contains the 'NOT' operator, as well as all the
  4368  *       expression operators ('MOD', 'AND', etc...), all of which
  4393  *       expression operators ('MOD', 'AND', etc...), all of which
  4369  *       may also be a function name! This means that these operators/functions,
  4394  *       may also be a function name! This means that these operators/functions,
  4409  *       function_name_expression_clashes and function_name_simpleop_clashes
  4434  *       function_name_expression_clashes and function_name_simpleop_clashes
  4410  *       are followed by a il_operand_list with __two__ or more il_operands!!
  4435  *       are followed by a il_operand_list with __two__ or more il_operands!!
  4411  */
  4436  */
  4412 | function_name_no_clashes il_operand_list
  4437 | function_name_no_clashes il_operand_list
  4413 	{$$ = new il_function_call_c($1, $2, locloc(@$));}
  4438 	{$$ = new il_function_call_c($1, $2, locloc(@$));}
       
  4439 /*
  4414 | il_simple_operator_clash_il_operand ',' il_operand_list
  4440 | il_simple_operator_clash_il_operand ',' il_operand_list
  4415 	{list_c *list = new il_operand_list_c(locloc(@$));
  4441 	{list_c *list = new il_operand_list_c(locloc(@$));
  4416 	 list->add_element($1.second);
  4442 	 list->add_element($1.second);
  4417 	 FOR_EACH_ELEMENT(elem, $3, {list->add_element(elem);})
  4443 	 FOR_EACH_ELEMENT(elem, $3, {list->add_element(elem);})
  4418 	 $$ = new il_function_call_c(il_operator_c_2_identifier_c($1.first), list, locloc(@$));
  4444 	 $$ = new il_function_call_c(il_operator_c_2_identifier_c($1.first), list, locloc(@$));
  4419 	 /* TODO: free the memory used up by the no longer used $3 object! */
       
  4420 	 /*       I don't do it now because I would have to test the change, and am
       
  4421 	  *       currently frying bigger fish... (Mario)
       
  4422 	  */
       
  4423 	 /*   free($3); */
       
  4424 	}
  4445 	}
  4425 ;
  4446 */
  4426 
  4447 | il_simple_operator_clash il_operand_list2
  4427 
  4448 	{
  4428 
  4449 	 $$ = new il_function_call_c($1, $2, locloc(@$));
       
  4450 	}
       
  4451 ;
       
  4452 
       
  4453 
       
  4454 
       
  4455 /*
  4429 il_simple_operator_clash_il_operand:
  4456 il_simple_operator_clash_il_operand:
  4430   il_simple_operator_clash il_operand
  4457   il_simple_operator_clash il_operand
  4431 	{$$.first = $1; $$.second = $2;}
  4458 	{$$.first = $1; $$.second = $2;}
  4432 ;
  4459 ;
       
  4460 */
       
  4461 
  4433 
  4462 
  4434 
  4463 
  4435 
  4464 
  4436 il_expression:
  4465 il_expression:
  4437   il_expr_operator_noclash '(' eol_list ')'
  4466   il_expr_operator_noclash '(' eol_list ')'
  4556 
  4585 
  4557 
  4586 
  4558 il_operand_list:
  4587 il_operand_list:
  4559   il_operand
  4588   il_operand
  4560 	{$$ = new il_operand_list_c(locloc(@$)); $$->add_element($1);}
  4589 	{$$ = new il_operand_list_c(locloc(@$)); $$->add_element($1);}
  4561 | il_operand_list ',' il_operand
  4590 | il_operand_list2
       
  4591 ;
       
  4592 
       
  4593 
       
  4594 /* List with 2 or more il_operands */ 
       
  4595 il_operand_list2:
       
  4596   il_operand ',' il_operand 
       
  4597 	{$$ = new il_operand_list_c(locloc(@$)); $$->add_element($1); $$->add_element($3);}
       
  4598 | il_operand_list2 ',' il_operand
  4562 	{$$ = $1; $$->add_element($3);}
  4599 	{$$ = $1; $$->add_element($3);}
  4563 ;
  4600 ;
  4564 
  4601 
  4565 
  4602 
  4566 simple_instr_list:
  4603 simple_instr_list: