stage1_2/iec_bison.yy
changeset 643 1cc0e1ca2aad
parent 616 0c7663fdc8d3
child 667 bd1360f29f15
equal deleted inserted replaced
641:5681f600ac18 643:1cc0e1ca2aad
   431 
   431 
   432 /*********************/
   432 /*********************/
   433 /* B 1.2 - Constants */
   433 /* B 1.2 - Constants */
   434 /*********************/
   434 /*********************/
   435 %type <leaf>	constant
   435 %type <leaf>	constant
   436 %type <leaf>	non_negative_constant
   436 %type <leaf>	non_int_or_real_constant
   437 
   437 
   438 /******************************/
   438 /******************************/
   439 /* B 1.2.1 - Numeric Literals */
   439 /* B 1.2.1 - Numeric Literals */
   440 /******************************/
   440 /******************************/
   441 /* Done totally within flex...
   441 /* Done totally within flex...
  1295 // %type  <leaf>	multiply_operator
  1295 // %type  <leaf>	multiply_operator
  1296 %type  <leaf>	power_expression
  1296 %type  <leaf>	power_expression
  1297 %type  <leaf>	unary_expression
  1297 %type  <leaf>	unary_expression
  1298 // %type  <leaf>	unary_operator
  1298 // %type  <leaf>	unary_operator
  1299 %type  <leaf>	primary_expression
  1299 %type  <leaf>	primary_expression
  1300 %type  <leaf>	non_negative_primary_expression
  1300 %type  <leaf>	non_int_or_real_primary_expression
  1301 /* intermediate helper symbol for primary_expression */
  1301 /* intermediate helper symbol for primary_expression */
  1302 %type  <leaf>	function_invocation
  1302 %type  <leaf>	function_invocation
  1303 
  1303 
  1304 // %token AND
  1304 // %token AND
  1305 // %token XOR
  1305 // %token XOR
  1726 
  1726 
  1727 /*********************/
  1727 /*********************/
  1728 /* B 1.2 - Constants */
  1728 /* B 1.2 - Constants */
  1729 /*********************/
  1729 /*********************/
  1730 constant:
  1730 constant:
  1731   numeric_literal
  1731   character_string
  1732 | character_string
       
  1733 | time_literal
  1732 | time_literal
  1734 | bit_string_literal
  1733 | bit_string_literal
  1735 | boolean_literal
  1734 | boolean_literal
       
  1735 | numeric_literal  
       
  1736 /* NOTE: Our definition of numeric_literal is diferent than the one in the standard.
       
  1737  *       We will now add what is missing in our definition of numeric literal, so our
       
  1738  *       definition of constant matches what the definition of constant in the standard.
       
  1739  */
  1736 /* NOTE: in order to remove reduce/reduce conflicts,
  1740 /* NOTE: in order to remove reduce/reduce conflicts,
  1737  * [between -9.5 being parsed as 
  1741  * [between -9.5 being parsed as 
  1738  *     (i)   a signed real, 
  1742  *     (i)   a signed real, 
  1739  *     (ii)  or as a real preceded by the '-' operator
  1743  *     (ii)  or as a real preceded by the '-' operator
  1740  *  ]
  1744  *  ]
  1741  *  we need to define a variant of the constant construct
  1745  *  we need to define a variant of the constant construct
  1742  *  where any constant is never preceded by the '-' character.
  1746  *  where any real or integer constant is always preceded by 
  1743  * In order to do this, we have borugh the signed_real 
  1747  *  a sign (i.e. the '-' or '+' characters).
  1744  * directly into the definition of the constant construct
  1748  *  (For more info, see comment in the construct non_int_or_real_primary_expression)
  1745  * (so we can define another non_negative_constant
  1749  *
  1746  * construct that does not include it!)
  1750  * For the above reason, our definition of the numeric_literal construct
  1747  */
  1751  * is missing the integer and real constrcuts (when not preceded by a sign)
  1748 | signed_real
  1752  * so we add then here explicitly!
  1749 /* NOTE: in order to remove reduce/reduce conflicts,
  1753  */
  1750  * unsigned_integer, signed_integer, binary_integer, octal_integer
  1754 | real
  1751  * and hex_integer have been integrated directly into
  1755 | integer
  1752  * the constants construct, instead of belonging to
       
  1753  * both the bit_string_literal or integer_literal
       
  1754  * construct.
       
  1755  */
       
  1756 /* NOTE: unsigned_integer, although used in some
  1756 /* NOTE: unsigned_integer, although used in some
  1757  * rules, is not defined in the spec!
  1757  * rules, is not defined in the spec!
  1758  * We therefore replaced unsigned_integer as integer
  1758  * We therefore replaced unsigned_integer as integer
  1759  */
  1759  */
  1760 /*| integer {} */  /* i.e. an unsigned_integer */ /* NOTE: already included as a signed integer! */
  1760 ;
  1761 | signed_integer
  1761 
  1762 | binary_integer
  1762 
  1763 | octal_integer
  1763 
  1764 | hex_integer
  1764 
  1765 ;
  1765 non_int_or_real_constant:
  1766 
  1766   character_string
  1767 
  1767 | time_literal
       
  1768 | bit_string_literal
       
  1769 | boolean_literal
       
  1770 | numeric_literal  
       
  1771 /* NOTE: Our definition of numeric_literal is diferent than the one in the standard.
       
  1772  *       It is missing the integer and real when not prefixed by a sign 
       
  1773  *       (i.e. -54, +42 is included in numerical_literal,
       
  1774  *        but   54,  42 is not parsed as a numeric_literal!!)
       
  1775  */
  1768 /* NOTE: in order to remove reduce/reduce conflicts,
  1776 /* NOTE: in order to remove reduce/reduce conflicts,
  1769  * [between -9.5 being parsed as 
  1777  * [between -9.5 being parsed as 
  1770  *     (i)   a signed real, 
  1778  *     (i)   a signed real, 
  1771  *     (ii)  or as a real preceded by the '-' operator
  1779  *     (ii)  or as a real preceded by the '-' operator
  1772  *  ]
  1780  *  ]
       
  1781  * [and a similar situation for integers!]
  1773  *  we need to define a variant of the constant construct
  1782  *  we need to define a variant of the constant construct
  1774  *  where any constant is never preceded by the '-' character.
  1783  *  where any real or integer constant is always preceded by 
  1775  * In order to do this, we have borugh the signed_real 
  1784  *  a sign (i.e. the '-' or '+' characters).
  1776  * directly into the definition of the constant construct
  1785  *
  1777  * (so we can define another non_negative_constant
  1786  * For the above reason, our definition of the numeric_literal construct
  1778  * construct that does not include it!)
  1787  * is missing the integer and real constrcuts (when not preceded by a sign)
  1779  */
  1788  */
  1780 non_negative_constant:
       
  1781   numeric_literal
       
  1782 | character_string
       
  1783 | time_literal
       
  1784 | bit_string_literal
       
  1785 | boolean_literal
       
  1786 /* NOTE: in order to remove reduce/reduce conflicts,
       
  1787  * [between -9.5 being parsed as 
       
  1788  *     (i)   a signed real, 
       
  1789  *     (ii)  or as a real preceded by the '-' operator
       
  1790  *  ]
       
  1791  *  we need to define a variant of the constant construct
       
  1792  *  where any constant is never preceded by the '-' character.
       
  1793  * In order to do this, we have borugh the signed_real 
       
  1794  * directly into the definition of the constant construct
       
  1795  * (so we can define another non_negative_constant
       
  1796  * construct that does not include it!)
       
  1797  */
       
  1798 /* | signed_real */
       
  1799 | real /* an unsigned real */
       
  1800 /* NOTE: in order to remove reduce/reduce conflicts,
       
  1801  * unsigned_integer, signed_integer, binary_integer, octal_integer
       
  1802  * and hex_integer have been integrated directly into
       
  1803  * the constants construct, instead of belonging to
       
  1804  * both the bit_string_literal or integer_literal
       
  1805  * construct.
       
  1806  */
       
  1807 /* NOTE: unsigned_integer, although used in some
       
  1808  * rules, is not defined in the spec!
       
  1809  * We therefore replaced unsigned_integer as integer
       
  1810  */
       
  1811 | integer  /* i.e. an unsigned_integer */
       
  1812 /* | signed_integer */
       
  1813 | binary_integer
       
  1814 | octal_integer
       
  1815 | hex_integer
       
  1816 ;
  1789 ;
  1817 
  1790 
  1818 
  1791 
  1819 /******************************/
  1792 /******************************/
  1820 /* B 1.2.1 - Numeric Literals */
  1793 /* B 1.2.1 - Numeric Literals */
  1912 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1885 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1913 | integer_type_name '#' octal_integer
  1886 | integer_type_name '#' octal_integer
  1914 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1887 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1915 | integer_type_name '#' hex_integer
  1888 | integer_type_name '#' hex_integer
  1916 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1889 	{$$ = new integer_literal_c($1, $3, locloc(@$));}
  1917 /* NOTE: see note in the definition of constant for reason
  1890 | binary_integer
  1918  * why signed_integer, binary_integer, octal_integer
  1891 | octal_integer
  1919  * and hex_integer are missing here!
  1892 | hex_integer
  1920  */
  1893 //|signed_integer  /* We expand the construct signed_integer here, so we can remove one of its constituents */
       
  1894 //|  integer       /* REMOVED! see note in the definition of constant for reason why integer is missing here! */
       
  1895 | '+' integer   {$$ = $2;}
       
  1896 | '-' integer	{$$ = new neg_integer_c($2, locloc(@$));}
  1921 /* ERROR_CHECK_BEGIN */
  1897 /* ERROR_CHECK_BEGIN */
  1922 | integer_type_name signed_integer
  1898 | integer_type_name signed_integer
  1923 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between integer type name and value in integer literal."); yynerrs++;}
  1899 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between integer type name and value in integer literal."); yynerrs++;}
  1924 | integer_type_name binary_integer
  1900 | integer_type_name binary_integer
  1925 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between integer type name and value in integer literal."); yynerrs++;}
  1901 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between integer type name and value in integer literal."); yynerrs++;}
  1934 	 yyerrok;
  1910 	 yyerrok;
  1935 	}
  1911 	}
  1936 /* ERROR_CHECK_END */
  1912 /* ERROR_CHECK_END */
  1937 ;
  1913 ;
  1938 
  1914 
       
  1915 /* NOTE: this construct is used in the definition of integer_literal. However, in order to remove
       
  1916  *       a reduce/reduce conflict (see NOTE in definition of constant for reason why)
       
  1917  *       it is not used directly, but rather its expansion is copied there.
       
  1918  *
       
  1919  *       If for some reason you need to change the definition of signed_integer, don't forget
       
  1920  *       to change its expansion in integer_literal too!
       
  1921 */
  1939 signed_integer:
  1922 signed_integer:
  1940   integer
  1923   integer
  1941 | '+' integer   {$$ = $2;}
  1924 | '+' integer   {$$ = $2;}
  1942 | '-' integer	{$$ = new neg_integer_c($2, locloc(@$));}
  1925 | '-' integer	{$$ = new neg_integer_c($2, locloc(@$));}
  1943 ;
  1926 ;
  1944 
  1927 
  1945 
  1928 
  1946 real_literal:
  1929 real_literal:
  1947 /* NOTE: see note in the definition of constant for reason
  1930 // signed_real /* We expand the construct signed_integer here, so we can remove one of its constituents */
  1948  * why signed_real is missing here!
  1931 // real        /* REMOVED! see note in the definition of constant for reason why real is missing here! */
  1949  */
  1932   '+' real	{$$ = $2;}
  1950 /*  signed_real */
  1933 | '-' real	{$$ = new neg_real_c($2, locloc(@2));}
  1951   real_type_name '#' signed_real
  1934 | real_type_name '#' signed_real
  1952 	{$$ = new real_literal_c($1, $3, locloc(@$));}
  1935 	{$$ = new real_literal_c($1, $3, locloc(@$));}
  1953 /* ERROR_CHECK_BEGIN */
  1936 /* ERROR_CHECK_BEGIN */
  1954 | real_type_name signed_real
  1937 | real_type_name signed_real
  1955 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between real type name and value in real literal."); yynerrs++;}
  1938 	{$$ = NULL; print_err_msg(locl(@1), locf(@2), "'#' missing between real type name and value in real literal."); yynerrs++;}
  1956 | real_type_name '#' error
  1939 | real_type_name '#' error
  1960 	 yyerrok;
  1943 	 yyerrok;
  1961 	}
  1944 	}
  1962 /* ERROR_CHECK_END */
  1945 /* ERROR_CHECK_END */
  1963 ;
  1946 ;
  1964 
  1947 
  1965 
  1948 /* NOTE: this construct is used in the definition of real_literal. However, in order to remove
       
  1949  *       a reduce/reduce conflict (see NOTE in definition of constant for reason why)
       
  1950  *       it is not used directly, but rather its expansion is copied there.
       
  1951  *
       
  1952  *       If for some reason you need to change the definition of signed_real, don't forget
       
  1953  *       to change its expansion in real_literal too!
       
  1954 */
  1966 signed_real:
  1955 signed_real:
  1967   real
  1956   real
  1968 | '+' real	{$$ = $2;}
  1957 | '+' real	{$$ = $2;}
  1969 | '-' real	{$$ = new neg_real_c($2, locloc(@2));}
  1958 | '-' real	{$$ = new neg_real_c($2, locloc(@2));}
  1970 ;
  1959 ;
  1971 
       
  1972 
  1960 
  1973 
  1961 
  1974 bit_string_literal:
  1962 bit_string_literal:
  1975   bit_string_type_name '#' integer  /* i.e. unsigned_integer */
  1963   bit_string_type_name '#' integer  /* i.e. unsigned_integer */
  1976 	{$$ = new bit_string_literal_c($1, $3, locloc(@$));}
  1964 	{$$ = new bit_string_literal_c($1, $3, locloc(@$));}
  7195 /* ERROR_CHECK_END */
  7183 /* ERROR_CHECK_END */
  7196 ;
  7184 ;
  7197 
  7185 
  7198 
  7186 
  7199 unary_expression:
  7187 unary_expression:
  7200   non_negative_primary_expression
  7188   primary_expression
  7201 | '-' non_negative_primary_expression
  7189 | '-' non_int_or_real_primary_expression
  7202 	{$$ = new neg_expression_c($2, locloc(@$));}
  7190 	{$$ = new neg_expression_c($2, locloc(@$));}
  7203 | NOT primary_expression
  7191 | NOT primary_expression
  7204 	{$$ = new not_expression_c($2, locloc(@$));}
  7192 	{$$ = new not_expression_c($2, locloc(@$));}
  7205 /* ERROR_CHECK_BEGIN */
  7193 /* ERROR_CHECK_BEGIN */
  7206 | '-' error
  7194 | '-' error
  7232  *       OR
  7220  *       OR
  7233  *       expression<-unary_expression<-constant<-integer
  7221  *       expression<-unary_expression<-constant<-integer
  7234  *       (i.e. the constant 9, preceded by a unary negation)
  7222  *       (i.e. the constant 9, preceded by a unary negation)
  7235  *
  7223  *
  7236  *       To remove the conflict, we only allow constants without
  7224  *       To remove the conflict, we only allow constants without
  7237  *       a preceding '-' to be used in primary_expression
  7225  *       integer or reals that are not preceded by a sign 
       
  7226  *       (i.e. a '-' or '+' character) to be used in primary_expression
  7238  *       (i.e. as a parameter to the unary negation operator)
  7227  *       (i.e. as a parameter to the unary negation operator)
       
  7228  *
       
  7229  *       e.g.  '-42', '+54', '42', '54' are all allowed in primary expression
       
  7230  *       according to the standard. However, we will allow only '-42' and '+54'
       
  7231  *       to be used as an argument to the negation operator ('-').
       
  7232  */
       
  7233 /* NOTE: Notice that the standard considers the following syntax correct:
       
  7234  *         VAR intv: INT; END_VAR
       
  7235  *         intv :=      42;         <----- OK
       
  7236  *         intv :=     -42;         <----- OK
       
  7237  *         intv :=     +42;         <----- OK
       
  7238  *         intv :=    --42;         <----- OK!!
       
  7239  *         intv :=    -+42;         <----- OK!!
       
  7240  *         intv :=  -(--42);        <----- OK!!
       
  7241  *         intv :=  -(-+42);        <----- OK!!
       
  7242  *         intv :=-(-(--42));       <----- OK!!
       
  7243  *         intv :=-(-(-+42));       <----- OK!!
       
  7244  *     but does NOT allow the following syntax:
       
  7245  *         VAR intv: INT; END_VAR
       
  7246  *         intv :=   ---42;       <----- ERROR!!
       
  7247  *         intv :=   --+42;       <----- ERROR!!
       
  7248  *         intv :=  ----42;       <----- ERROR!!
       
  7249  *         intv :=  ---+42;       <----- ERROR!!
       
  7250  *
       
  7251  *    Although strange, we follow the standard to the letter, and do exactly
       
  7252  *    as stated above!!
  7239  */
  7253  */
  7240 /* NOTE: We use enumerated_value_without_identifier instead of enumerated_value
  7254 /* NOTE: We use enumerated_value_without_identifier instead of enumerated_value
  7241  *       in order to remove a reduce/reduce conflict between reducing an
  7255  *       in order to remove a reduce/reduce conflict between reducing an
  7242  *       identifier to a variable or an enumerated_value.
  7256  *       identifier to a variable or an enumerated_value.
  7243  *
  7257  *
  7245  *       imply (by introducing syntax that allows to unambiguosly reference an
  7259  *       imply (by introducing syntax that allows to unambiguosly reference an
  7246  *       enumerated value - enum_type#enum_value) that in case the same identifier is used
  7260  *       enumerated value - enum_type#enum_value) that in case the same identifier is used
  7247  *       for a variable and an enumerated value, then the variable shall be
  7261  *       for a variable and an enumerated value, then the variable shall be
  7248  *       considered.
  7262  *       considered.
  7249  */
  7263  */
  7250 non_negative_primary_expression:
  7264 non_int_or_real_primary_expression:
  7251   non_negative_constant
  7265   non_int_or_real_constant
  7252 //| enumerated_value_without_identifier
  7266 //| enumerated_value_without_identifier
  7253 | enumerated_value
  7267 | enumerated_value
  7254 | variable
  7268 | variable
  7255 | '(' expression ')'
  7269 | '(' expression ')'
  7256 	{$$ = $2;}
  7270 	{$$ = $2;}