Reading a line (from a file) in Prolog

I wrote a lexer and parser in Prolog. He combines the string with his AST. This is part of the compiler / interpreter project I'm working on. Naturally, now I want to read a line from a file for its analysis. However, the predicates that I found for this are to read , and it only reads the atoms and predicates of Prolog, for example files with

hello.

I changed the double_quotes settings , but without success.

I want to read a file with something like this

let id = \x.x in id (S (S Z))

and then send this line to the parsing predicates.

+3
source share
1 answer

read_line_to_codes/2 read_line_to_codes/3. , stdin stdout:

read_lines([H|T]) :-
  read_line_to_codes(user_input, H), H \= end_of_file, read_lines(T).
read_lines([]).

write_lines([]).
write_lines([H|T]) :-
  writef("%s\n", [H]), write_lines(T).

main :-
  read_lines(X), write_lines(X).

( writef/2 .) read_stream_to_codes/2 read_stream_to_codes/3, . stdin :

main :-
  read_stream_to_codes(user_input, X), writef("%s", [X]).

, stdin. . readutil.

+8

Source: https://habr.com/ru/post/1724728/


All Articles