Parse numeric litterals (Integer and Real numbers)
BjAlvestad opened this issue · comments
Ref. section 11.3 of SCLV4_e
Examples of valid formats for decimal digit strings in literals:
1000
1_120_200
666_999_400_311
Integer literals
Integer literals can be assigned to BOOL
, BYTE
, INT
, DINT
, WORD
, DWORD
(depending on their length).
Integer literals can also be specified in binary, octal or hexadecimal with the prefixes 2#
, 8#
and 16#
Real number literals
This is nubers with decimal places, and can only be assigned to the REAL
data type.
Note that eksponent is denoted with e
followed by +
, -
or just a number directly.
E.g. 3.0E+10
, 3.0E10
, 3e+10
, 3E10
, 0.3E+11
, 0.3e11
, 30.0E+9
, 30e9
are all representing 3*10^10.
Summary of various alternatives
// Integer literals
NUMBER1:= 10 ;
NUMBER2:= 2#1010 ;
NUMBER3:= 16#1A2B ;
// Real number literals
NUMBER4:= -3.4 ;
NUMBER5:= 4e2 ;
NUMBER6:= 40_123E10;
Suggestion to try out
Add terminal factors for digits:
- Decimal digit string as a
terminal fragment DECIMAL_DIGIT /([0-9](_[0-9])?)+/
- Binary digit string as
terminal fragment BINARY_DIGIT /2#([0-1](_[0-1)?)+/
- Octal digit string as
terminal fragment OCTAL_DIGIT /8#([0-7](_[0-7)?)+/
- Hex digit string as
terminal fragment HEX_DIGIT /16#([0-9a-fA-F](_[0-9a-fA-F)?)+/
Add terminal factors for exponent;
- Exponent
terminal fragment EXPONENT /[eE][+-]?DECIMAL_DIGIT/
And terminal for Integer literal and Real number:
- Integer literal
terminal INTEGER /[+-]?DECIMAL_DIGIT|BINARY_DIGIT|OCTAL_DIGIT|HEX_DIGIT/
- Real number literal
terminal REAL /[+-]?((DECIMAL_DIGIT\\.DECIMAL_DIGIT)|(DECIMAL_DIGIT(\\.DECIMAL_DIGIT)?EXPONENT))/
Landed on
terminal fragment BINARY_DIGIT: /2#([0-1](_[0-1])?)+/;
terminal fragment OCTAL_DIGIT: /8#([0-7](_[0-7])?)+/;
terminal fragment HEX_DIGIT: /16#([0-9a-fA-F](_[0-9a-fA-F])?)+/;
terminal fragment DECIMAL_DIGIT: /([0-9](_[0-9])?)+/;
terminal fragment EXPONENT: /[eE][+-]?/DECIMAL_DIGIT;
terminal REAL: /[+-]?/((DECIMAL_DIGIT('.'DECIMAL_DIGIT)?EXPONENT)|(DECIMAL_DIGIT'.'DECIMAL_DIGIT));
terminal INTEGER: /[+-]?/BINARY_DIGIT|OCTAL_DIGIT|HEX_DIGIT|DECIMAL_DIGIT;