stage1_2/iec.y
changeset 85 a6e651ba55c2
parent 79 7417b346e5a3
child 86 1988bf1634da
equal deleted inserted replaced
84:a74c279f0a2e 85:a6e651ba55c2
   559 %type  <leaf>	enumerated_spec_init
   559 %type  <leaf>	enumerated_spec_init
   560 %type  <leaf>	enumerated_specification
   560 %type  <leaf>	enumerated_specification
   561 /* helper symbol for enumerated_value */
   561 /* helper symbol for enumerated_value */
   562 %type  <list>	enumerated_value_list
   562 %type  <list>	enumerated_value_list
   563 %type  <leaf>	enumerated_value
   563 %type  <leaf>	enumerated_value
   564 %type  <leaf>	enumerated_value_without_identifier
   564 //%type  <leaf>	enumerated_value_without_identifier
   565 
   565 
   566 %type  <leaf>	array_type_declaration
   566 %type  <leaf>	array_type_declaration
   567 %type  <leaf>	array_spec_init
   567 %type  <leaf>	array_spec_init
   568 %type  <leaf>	array_specification
   568 %type  <leaf>	array_specification
   569 /* helper symbol for array_specification */
   569 /* helper symbol for array_specification */
   833 %type  <leaf>	action_time
   833 %type  <leaf>	action_time
   834 %type  <leaf>	indicator_name
   834 %type  <leaf>	indicator_name
   835 %type  <leaf>	transition
   835 %type  <leaf>	transition
   836 %type  <leaf>	steps
   836 %type  <leaf>	steps
   837 %type  <list>	step_name_list
   837 %type  <list>	step_name_list
   838 %type  <tmp_symbol> transition_header
   838 %type  <leaf>	transition_condition
   839 %type  <leaf>	transition_condition_il
       
   840 %type  <leaf>	transition_condition_st
       
   841 %type  <tmp_symbol> action_header
       
   842 %type  <leaf>	action
   839 %type  <leaf>	action
   843 %type  <leaf>	transition_name
   840 %type  <leaf>	transition_name
   844 
   841 
   845 
   842 
   846 // %token ASSIGN
   843 // %token ASSIGN
  1172 /* B 3.2.2 Subprogram Control Statements */
  1169 /* B 3.2.2 Subprogram Control Statements */
  1173 /*****************************************/
  1170 /*****************************************/
  1174 %type <leaf>	subprogram_control_statement
  1171 %type <leaf>	subprogram_control_statement
  1175 %type <leaf>	return_statement
  1172 %type <leaf>	return_statement
  1176 %type <leaf>	fb_invocation
  1173 %type <leaf>	fb_invocation
  1177 %type <leaf>	param_assignment
  1174 // %type <leaf>	param_assignment
  1178 /* helper symbol for fb_invocation */
  1175 %type <leaf>	param_assignment_formal
  1179 %type <list> param_assignment_list
  1176 %type <leaf>	param_assignment_nonformal
       
  1177 /* helper symbols for fb_invocation */
       
  1178 %type <list> param_assignment_formal_list
       
  1179 %type <list> param_assignment_nonformal_list
  1180 
  1180 
  1181 // %token ASSIGN
  1181 // %token ASSIGN
  1182 // %token SENDTO   /* "=>" */
  1182 // %token SENDTO   /* "=>" */
  1183 %token RETURN
  1183 %token RETURN
  1184 
  1184 
  2285 	{$$ = $1; $$->add_element($3);}
  2285 	{$$ = $1; $$->add_element($3);}
  2286 ;
  2286 ;
  2287 
  2287 
  2288 
  2288 
  2289 enumerated_value:
  2289 enumerated_value:
  2290   identifier
  2290   identifier 
  2291 | prev_declared_enumerated_type_name '#' any_identifier
  2291 | prev_declared_enumerated_type_name '#' any_identifier
  2292 	{$$ = new enumerated_value_c($1, $3, locloc(@$));}
  2292 	{$$ = new enumerated_value_c($1, $3, locloc(@$));}
  2293 ;
  2293 ;
  2294 
  2294 
  2295 
  2295 
       
  2296 /*
  2296 enumerated_value_without_identifier:
  2297 enumerated_value_without_identifier:
  2297   prev_declared_enumerated_type_name '#' any_identifier
  2298   prev_declared_enumerated_type_name '#' any_identifier
  2298 	{$$ = new enumerated_value_c($1, $3, locloc(@$));}
  2299 	{$$ = new enumerated_value_c($1, $3, locloc(@$));}
  2299 ;
  2300 ;
       
  2301 */
  2300 
  2302 
  2301 
  2303 
  2302 array_type_declaration:
  2304 array_type_declaration:
  2303 /*  array_type_name ':' array_spec_init */
  2305 /*  array_type_name ':' array_spec_init */
  2304   identifier ':' array_spec_init
  2306   identifier ':' array_spec_init
  3770 | step_name_list ',' step_name
  3772 | step_name_list ',' step_name
  3771 	{$$ = $1; $$->add_element($3);}
  3773 	{$$ = $1; $$->add_element($3);}
  3772 ;
  3774 ;
  3773 
  3775 
  3774 
  3776 
  3775 transition_header:
  3777 /* NOTE: flex will automatically pop() out of body_state to previous state.
  3776   TRANSITION FROM steps TO steps
  3778  *       We do not need to give a command from bison to return to previous flex state!
  3777 	{$$.first = NULL; $$.second = NULL; $$.third = $3; $$.fourth = $5; cmd_goto_body_state();}
  3779  */
  3778 | TRANSITION transition_name FROM steps TO steps 
  3780 transition:
  3779 //| TRANSITION identifier FROM steps TO steps 
  3781   TRANSITION 
  3780 	{$$.first = $2; $$.second = NULL; $$.third = $4; $$.fourth = $6; cmd_goto_body_state();}
  3782     FROM steps TO steps 
  3781 | TRANSITION '(' PRIORITY ASSIGN integer ')' FROM steps TO steps
  3783     {cmd_goto_body_state();} transition_condition 
  3782 	{$$.first = NULL; $$.second = $5; $$.third = $8; $$.fourth = $10; cmd_goto_body_state();}
  3784   END_TRANSITION 
  3783 | TRANSITION transition_name '(' PRIORITY ASSIGN integer ')' FROM steps TO steps
  3785 	{$$ = new transition_c(NULL, NULL, $3, $5, $7, NULL, locloc(@$));}
  3784 //| TRANSITION identifier '(' PRIORITY ASSIGN integer ')' FROM steps TO steps
  3786 //| TRANSITION identifier FROM steps TO steps ... 
  3785 	{$$.first = $2; $$.second = $6; $$.third = $9; $$.fourth = $11; cmd_goto_body_state();}
  3787 | TRANSITION transition_name 
  3786 ;
  3788     FROM steps TO steps 
  3787 
  3789     {cmd_goto_body_state();} transition_condition 
  3788 transition_condition_il:
  3790   END_TRANSITION 
       
  3791 	{$$ = new transition_c($2, NULL, $4, $6, $8, NULL, locloc(@$));}
       
  3792 | TRANSITION '(' PRIORITY ASSIGN integer ')' 
       
  3793     FROM steps TO steps 
       
  3794     {cmd_goto_body_state();} transition_condition 
       
  3795   END_TRANSITION
       
  3796         {$$ = new transition_c(NULL, $5, $8, $10, $12, NULL, locloc(@$));}
       
  3797 //| TRANSITION identifier '(' PRIORITY ASSIGN integer ')' FROM steps TO steps ...
       
  3798 | TRANSITION transition_name '(' PRIORITY ASSIGN integer ')' 
       
  3799     FROM steps TO steps 
       
  3800     {cmd_goto_body_state();} transition_condition 
       
  3801   END_TRANSITION
       
  3802         {$$ = new transition_c($2, $6, $9, $11, $13, NULL, locloc(@$));}
       
  3803 ;
       
  3804 
       
  3805 
       
  3806 
       
  3807 transition_condition:
  3789   ':' eol_list simple_instr_list
  3808   ':' eol_list simple_instr_list
  3790 	{$$ = $3;}
  3809 	{$$ = $3;}
  3791 ;
  3810 | ASSIGN expression ';'
  3792 
       
  3793 transition_condition_st:
       
  3794   ASSIGN expression ';'
       
  3795 	{$$ = $2}
  3811 	{$$ = $2}
  3796 ;
  3812 ;
  3797 
  3813 
  3798 
  3814 
  3799 /* TODO: Can't we clean this up a bit ? */
       
  3800 transition:
       
  3801   transition_header transition_condition_il END_TRANSITION
       
  3802         {$$ = new transition_c($1.first, $1.second, $1.third, $1.fourth, $2, NULL, locloc(@$));}
       
  3803         /* TODO: free the memory used up by the no longer used object $1 */
       
  3804 |  transition_header transition_condition_st END_TRANSITION
       
  3805         {$$ = new transition_c($1.first, $1.second, $1.third, $1.fourth, NULL, $2, locloc(@$));}
       
  3806 ;
       
  3807 
       
  3808 action_header:
       
  3809   ACTION action_name ':' 
       
  3810 //  ACTION identifier ':' 
       
  3811 	{$$.first = $2; cmd_goto_body_state();}
       
  3812 ;
       
  3813 
       
  3814 
  3815 
  3815 action:
  3816 action:
  3816   action_header function_block_body END_ACTION
  3817 //  ACTION identifier ':' ... 
  3817 	{$$ = new action_c($1.first, $2, locloc(@$));}
  3818   ACTION action_name ':' {cmd_goto_body_state();} function_block_body END_ACTION
       
  3819 	{$$ = new action_c($2, $5, locloc(@$));}
  3818 ;
  3820 ;
  3819 
  3821 
  3820 
  3822 
  3821 /********************************/
  3823 /********************************/
  3822 /* B 1.7 Configuration elements */
  3824 /* B 1.7 Configuration elements */
  3901    optional_instance_specific_initializations
  3903    optional_instance_specific_initializations
  3902  END_CONFIGURATION
  3904  END_CONFIGURATION
  3903 	{$$ = new configuration_declaration_c($2, $3, $4, $5, $6, locloc(@$));
  3905 	{$$ = new configuration_declaration_c($2, $3, $4, $5, $6, locloc(@$));
  3904 	 library_element_symtable.insert($2, prev_declared_configuration_name_token);
  3906 	 library_element_symtable.insert($2, prev_declared_configuration_name_token);
  3905 	 variable_name_symtable.pop();
  3907 	 variable_name_symtable.pop();
  3906 	}
  3908 }
  3907 /* ERROR_CHECK_BEGIN */
  3909 /* ERROR_CHECK_BEGIN */
  3908 | CONFIGURATION error END_CONFIGURATION
  3910 | CONFIGURATION error END_CONFIGURATION
  3909 	{$$ = NULL;
  3911 	{$$ = NULL;
  3910 	 print_err_msg(current_filename, @2.first_line, "error in configuration declaration.");
  3912 	 print_err_msg(current_filename, @2.first_line, "error in configuration declaration.");
  3911 	 /* yychar */
  3913 	 /* yychar */
  4558  *       in order to remove a reduce/reduce conflict between reducing an
  4560  *       in order to remove a reduce/reduce conflict between reducing an
  4559  *       identifier to a variable or an enumerated_value.
  4561  *       identifier to a variable or an enumerated_value.
  4560  */
  4562  */
  4561 il_operand:
  4563 il_operand:
  4562   variable
  4564   variable
  4563 | enumerated_value_without_identifier
  4565 //| enumerated_value_without_identifier
       
  4566 | enumerated_value
  4564 | constant
  4567 | constant
  4565 ;
  4568 ;
  4566 
  4569 
  4567 
  4570 
  4568 il_operand_list:
  4571 il_operand_list:
  4935 /* NOTE: We use enumerated_value_without_identifier instead of enumerated_value
  4938 /* NOTE: We use enumerated_value_without_identifier instead of enumerated_value
  4936  *       in order to remove a reduce/reduce conflict between reducing an
  4939  *       in order to remove a reduce/reduce conflict between reducing an
  4937  *       identifier to a variable or an enumerated_value.
  4940  *       identifier to a variable or an enumerated_value.
  4938  *
  4941  *
  4939  *       This change follows the IEC specification. The specification seems to
  4942  *       This change follows the IEC specification. The specification seems to
  4940  *       imply (or is it explicit?) that in case the same identifier id used
  4943  *       imply (by introducing syntax that allows to unambiguosly reference an
       
  4944  *       enumerated value - enum_type#enum_value) that in case the same identifier is used
  4941  *       for a variable and an enumerated value, then the variable shall be
  4945  *       for a variable and an enumerated value, then the variable shall be
  4942  *       considered??????????????
  4946  *       considered.
  4943  */
  4947  */
  4944 primary_expression:
  4948 primary_expression:
  4945 /* constant */
  4949 /* constant */
  4946   non_negative_constant
  4950   non_negative_constant
  4947 | enumerated_value_without_identifier
  4951 //| enumerated_value_without_identifier
       
  4952 | enumerated_value
  4948 | variable
  4953 | variable
  4949 | '(' expression ')'
  4954 | '(' expression ')'
  4950 	{$$ = $2;}
  4955 	{$$ = $2;}
  4951 |  function_invocation
  4956 |  function_invocation
  4952 ;
  4957 ;
  4977  *       We will leave this out for now. No need to complicate the syntax
  4982  *       We will leave this out for now. No need to complicate the syntax
  4978  *       more than the specification does by contradicting itself, and
  4983  *       more than the specification does by contradicting itself, and
  4979  *       letting names clash!
  4984  *       letting names clash!
  4980  */
  4985  */
  4981 function_invocation:
  4986 function_invocation:
  4982 /*  function_name '(' param_assignment_list ')' */
  4987 /*  function_name '(' [param_assignment_list] ')' */
  4983   function_name_no_NOT_clashes '(' param_assignment_list ')'
  4988   function_name_no_NOT_clashes '(' param_assignment_formal_list ')'
       
  4989 	{$$ = new function_invocation_c($1, $3, locloc(@$));}
       
  4990 | function_name_no_NOT_clashes '(' param_assignment_nonformal_list ')'
  4984 	{$$ = new function_invocation_c($1, $3, locloc(@$));}
  4991 	{$$ = new function_invocation_c($1, $3, locloc(@$));}
  4985 ;
  4992 ;
  4986 
  4993 
  4987 
  4994 
  4988 /********************/
  4995 /********************/
  5043 
  5050 
  5044 
  5051 
  5045 fb_invocation:
  5052 fb_invocation:
  5046   prev_declared_fb_name '(' ')'
  5053   prev_declared_fb_name '(' ')'
  5047 	{$$ = new fb_invocation_c($1, NULL, locloc(@$));	}
  5054 	{$$ = new fb_invocation_c($1, NULL, locloc(@$));	}
  5048 | prev_declared_fb_name '(' param_assignment_list ')'
  5055 | prev_declared_fb_name '(' param_assignment_formal_list ')'
       
  5056 	{$$ = new fb_invocation_c($1, $3, locloc(@$));}
       
  5057 | prev_declared_fb_name '(' param_assignment_nonformal_list ')'
  5049 	{$$ = new fb_invocation_c($1, $3, locloc(@$));}
  5058 	{$$ = new fb_invocation_c($1, $3, locloc(@$));}
  5050 ;
  5059 ;
  5051 
  5060 
  5052 
  5061 
  5053 /* helper symbol for
  5062 /* helper symbol for
  5054  * - fb_invocation
  5063  * - fb_invocation
  5055  * - function_invocation
  5064  * - function_invocation
  5056  */
  5065  */
  5057 param_assignment_list:
  5066 param_assignment_formal_list:
  5058   param_assignment
  5067   param_assignment_formal
  5059 	{$$ = new param_assignment_list_c(locloc(@$)); $$->add_element($1);}
  5068 	{$$ = new param_assignment_list_c(locloc(@$)); $$->add_element($1);}
  5060 | param_assignment_list ',' param_assignment
  5069 | param_assignment_formal_list ',' param_assignment_formal
  5061 	{$$ = $1; $$->add_element($3);}
  5070 	{$$ = $1; $$->add_element($3);}
  5062 ;
  5071 ;
  5063 
  5072 
  5064 
  5073 /* helper symbol for
  5065 
  5074  * - fb_invocation
       
  5075  * - function_invocation
       
  5076  */
       
  5077 param_assignment_nonformal_list:
       
  5078   param_assignment_nonformal
       
  5079 	{$$ = new param_assignment_list_c(locloc(@$)); $$->add_element($1);}
       
  5080 | param_assignment_nonformal_list ',' param_assignment_nonformal
       
  5081 	{$$ = $1; $$->add_element($3);}
       
  5082 ;
       
  5083 
       
  5084 
       
  5085 /* NOTE: According to the IEC 61131-3 standard, there are two possible
       
  5086  *       syntaxes for calling function blocks within ST.
       
  5087  *       The formal method has the form:
       
  5088  *        fb ( invar := x, inoutvar := var1, outvar => var2);
       
  5089  *       The non-formal method has the form:
       
  5090  *        fb (x, var1, var2);
       
  5091  *       In the text of IEC 61131-3 (where the semantics are defined),
       
  5092  *       it is obvious that mixing the two syntaxes is considered incorrect.
       
  5093  *       The following should therefore be incorrect: 
       
  5094  *        fb ( invar := x, var1, var2);
       
  5095  *       However, according to the syntax definition, as defined in IEC 61131-3,
       
  5096  *       mixing the formal and non-formal methods of invocation is allowed.
       
  5097  *       We have two alternatives:
       
  5098  *        (a) implement the syntax here in iec.y according to the standard,
       
  5099  *            and leave it to the semantic analyser stage to find this error
       
  5100  *        (b) or implement the syntax in iec.y correctly, not allowing 
       
  5101  *            the mixing of formal and non-formal invocation syntaxes.
       
  5102  *       Considering that this is a syntax issue, and not semantic issue,
       
  5103  *       I (Mario) have decided to go with alternative (a).
       
  5104  *       In other words, in iec.y we do not follow the syntax as defined in
       
  5105  *       Annex B of the IEC 61131-3 standard, but rather implement
       
  5106  *       the syntax also taking into account the textual part of the standard too.
       
  5107  */
       
  5108 /*
  5066 param_assignment:
  5109 param_assignment:
  5067 /*  variable_name ASSIGN expression */
  5110   variable_name ASSIGN expression 
       
  5111 */
       
  5112 param_assignment_nonformal:
       
  5113   expression
       
  5114 ;
       
  5115 
       
  5116 
       
  5117 param_assignment_formal:
  5068   any_identifier ASSIGN expression
  5118   any_identifier ASSIGN expression
  5069 	{$$ = new input_variable_param_assignment_c($1, $3, locloc(@$));}
  5119 	{$$ = new input_variable_param_assignment_c($1, $3, locloc(@$));}
  5070 | expression
       
  5071 /*| variable_name SENDTO variable */
  5120 /*| variable_name SENDTO variable */
  5072 /*| any_identifier SENDTO variable */
  5121 /*| any_identifier SENDTO variable */
  5073 | sendto_identifier SENDTO variable
  5122 | sendto_identifier SENDTO variable
  5074 	{$$ = new output_variable_param_assignment_c(NULL,$1, $3, locloc(@$));}
  5123 	{$$ = new output_variable_param_assignment_c(NULL,$1, $3, locloc(@$));}
  5075 /*| variable_name SENDTO variable */
  5124 /*| NOT variable_name SENDTO variable */
  5076 /*| NOT any_identifier SENDTO variable*/
  5125 /*| NOT any_identifier SENDTO variable*/
  5077 | NOT sendto_identifier SENDTO variable
  5126 | NOT sendto_identifier SENDTO variable
  5078 	{$$ = new output_variable_param_assignment_c(new not_paramassign_c(locloc(@$)),$2, $4, locloc(@$));}
  5127 	{$$ = new output_variable_param_assignment_c(new not_paramassign_c(locloc(@$)),$2, $4, locloc(@$));}
  5079 ;
  5128 ;
  5080 
  5129 
  5451   /* now parse the input file... */
  5500   /* now parse the input file... */
  5452   yyin = in_file;
  5501   yyin = in_file;
  5453   yylineno = 1;
  5502   yylineno = 1;
  5454   allow_function_overloading = false;
  5503   allow_function_overloading = false;
  5455   current_filename = filename;
  5504   current_filename = filename;
  5456   if (yyparse() != 0)
  5505   {int res;
  5457     exit(EXIT_FAILURE);
  5506     if ((res = yyparse()) != 0) {
       
  5507       fprintf (stderr, "\nInternal error while parsing file - yyparse() returned %d. Bailing out!\n", res);
       
  5508       ERROR;
       
  5509     }
       
  5510   }
  5458 
  5511 
  5459   if (yynerrs > 0) {
  5512   if (yynerrs > 0) {
  5460     fprintf (stderr, "\nFound %d error(s). Bailing out!\n", yynerrs /* global variable */);
  5513     fprintf (stderr, "\nFound %d error(s). Bailing out!\n", yynerrs /* global variable */);
  5461     exit(EXIT_FAILURE);
  5514     exit(EXIT_FAILURE);
  5462   }
  5515   }