How is a integer created as a character code constant?

蹲街弑〆低调 提交于 2019-12-20 02:28:37

问题


I am building some test cases for integer/1 using SWI-Prolog.

ISO/IEC 13211-1 gives a BNF definition for integer and one of the alternatives for integer is for a character code constant.

I am able to create and test examples of all the other alternatives using integer/1 but for character code constant I cannot create a valid example. (see below)

How is an integer as a character code constant created that will return true using integer/1?


Answer

Thanks to @false.

integer(0'0).
true.

integer(0'9).
true.

integer(0'a).
true.

integer(0'\n).
true.

Usefulness

X is 0'\n.
X = 10.

X is 0b010101.
X = 21.

X is 0xFFF1.
X = 65521.

X is 0o7423.
X = 3859.

and thanks to j4n bur53 via link from @false

with SWI-Prolog other radix can be used besides 2,8, or 16.

X is 5'1234012340.
X = 3032220.

X is 32'123456789ABCDEFGHIJKLMNOPQRSTU.
X = 47525417447024678661670292427038339608998846.

What I tried

integer("0").
false.

integer('0').
false.

integer(`0`).
false.

integer("1").
false.

integer('1').
false.

integer(`1`).
false.

ISO

INTERNATIONAL STANDARD ISO/IEC 13211-1 First edition 1995-06-01
Information technology - Programming languages - Prolog
Part 1: General Core

INTERNATIONAL STANDARD ISO/IEC 13211-1:1995
TECHNICAL CORRIGENDUM 1
Published 2007-11-15

INTERNATIONAL STANDARD ISO/IEC 13211-1:1995
TECHNICAL CORRIGENDUM 2
Published 2012-02-15

BNF for integer

integer token (* 6.4.4 *) =  
    integer constant (* 6.4.4 *)
  | character code constant (* 6.4.4 *)
  | binary constant (* 6.4.4 *)
  | octal constant (* 6.4.4 *)
  | hexadecimal constant (* 6.4.4 *) ;

BNF for character code constant

character code constant (* 6.4.4 *) =  
  "0" , single quote char (* 6.5.5 *), single quoted character (* 6.4.2.1 *) 

I suspect the BNF is wrong in ISO/IEC 13211-1 but checking the CORRIGENDUM shows no corrections.


Integer test cases

% <integer constant> examples

number(1). 
% true.

number(0).
% true.

number(01).
% true.

number(12345678901234567890123456789012345678901234567890).
% true.

% <character code constant> examples

% ???

% <binary constant> examples

number(0b0).  
% true.

number(0b10101010101010101010101010101010101010101010101010). 
% true.

integer(0b2).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0b2) .

% <octal constant> examples

integer(0o7). 
% true.

integer(0o1234567012345670123456701234567012345670123456701234567). 
% true.

integer(0o8).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0o8) . 

% <hexadecimal constant>

integer(0x0). 
% true.

integer(0xF). 
% true.

integer(0xf). 
% true.

integer(0x123456789ABCDEF012345670123456789ABCDEF012345670123456789ABCDEF). 
% true.

integer(0xG).
% ERROR: Syntax error: Illegal number
% ERROR: integer
% ERROR: ** here **
% ERROR: (0xG) . 

回答1:


This was answered by false in a comment.
Reposting here so that others can see an answer exist.

integer(0'0).
true.

integer(0'9).
true.

integer(0'a).
true.

integer(0'\n).
true.


来源:https://stackoverflow.com/questions/41637402/how-is-a-integer-created-as-a-character-code-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!