user1812457
user1812457

Reputation:

Parsing from standard input

How do I parse the standard input (without buffering)? If I understand correctly, phrase/2 needs a list, and phrase_from_file/2 from library(pure_input) needs a file.

I solved my problem using normal predicates (not DCG) and using built-ins like get_char/2 and read_line_to_codes/2, but at the end the implementation looks suspiciously similar to the solution I would have written in C.

And if I can sneak a very much related question: what is standard input in SWI-Prolog? read_line_to_codes (library(readutil)) needs an input stream (unlike get/1, for example). I get it with the following predicate:

input_stream(Stream) :-
    current_stream(Object, read, Stream),
    integer(Object).

. . . which of course works, but feels a bit hacked. Is it possible to have more than one open input stream? How am I going to know which one is the standard input of the operating system (Linux in my case)?

Upvotes: 7

Views: 3370

Answers (1)

CapelliC
CapelliC

Reputation: 60014

I think you are looking for the proper naming of streams. Here a sample that could be useful:

?- read_line_to_codes(user_input,L).
|: a line
L = [97, 32, 108, 105, 110, 101].

The most detailed explanation page I found is here.

Upvotes: 3

Related Questions