Hello,
I’m using: SWI-Prolog version 8.2.4 on Win10
I want to parse a sequence of numbers (floats and integers) and words (some are keywords, some just identifiers), separated by whitespace, like " -4 2.1 add"
I use this dcg code
eval(List, DataStack, DataStack2) :-
phrase(words(DataStack, DataStack2), List, _).
words(DS, DS3) --> ws,
word(W),
ws, !,
{ format("Current Word ~w ~n", [W]),
( is_keyword(W) ->
execute(W, DS, DS2)
;
% if word no keyword put it onto data stack
append([W], DS, DS2)
)
},
words(DS2, DS3).
words(DS, DS) --> [].
word(N) --> number(N) , { format("Number ~w ~n", [N]) }.
% longest possible match
word(W) --> identifier(CS), { length(CS, N),
N > 1,
atom_chars(W, CS),
format("Word ~w ~n", [W])}.
% Wenn es eine Adresse ist lege sie auf den Datastack
word(W) --> identifier(CS), { [C] = CS,
atom_string(W, C),
is_adress(W)}.
% parse Identifier
identifier([C|S]) --> character(C),
characters(S).
characters([C|S]) --> character(C),
characters(S).
characters([]) --> [].
character(C) --> [C], {char_type(C, csym)}.
character(C) --> [C], {char_code(C, 42)}.
ws --> [C], {char_type(C, white)}, ws.
ws --> [].
The problem is, only positive intergers will be detected correctly, negative numbers and floats don’t work? Where is my fault? I don’t see it, according documentation number//1 should work. BTW, it is some kind of simple Forth interpreter.
Cheers and thanks
Hans