Parsing from standard input
Asked Answered
P

1

7

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)?

Proust answered 19/12, 2012 at 21:23 Comment(0)
S
3

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.

Silures answered 19/12, 2012 at 22:4 Comment(2)
I thought I had read this page carefully enough... This answers my standard input question indeed. Still don't know if I can use DCG for parsing standard input.Proust
yes, use a loop like repeat, read_line_to_codes(user_input,L), phrase(your_grammar,L).Silures

© 2022 - 2024 — McMap. All rights reserved.