• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

A.24.1 library(pure_input): Pure Input from files and streams
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • The SWI-Prolog library
        • library(pio): Pure I/O
          • library(pure_input): Pure Input from files and streams
            • phrase_from_file/2
            • phrase_from_file/3
            • phrase_from_stream/2
            • syntax_error/1
            • lazy_list_location/1
            • lazy_list_character_count/1
            • stream_to_lazy_list/2
    • Packages
Availability::- use_module(library(pure_input)).(can be autoloaded)
Source[nondet]phrase_from_file(:Grammar, +File)
Process the content of File using the DCG rule Grammar. The space usage of this mechanism depends on the length of the not committed part of Grammar. Committed parts of the temporary list are reclaimed by the garbage collector, while the list is extended on demand due to unification of the attributed tail variable. Below is an example that counts the number of times a string appears in a file. The library dcg/basics provides string/3 matching an arbitrary string and remainder/3 which matches the remainder of the input without parsing.
:- use_module(library(dcg/basics)).

file_contains(File, Pattern) :-
        phrase_from_file(match(Pattern), File).

match(Pattern) -->
        string(_),
        string(Pattern),
        remainder(_).

match_count(File, Pattern, Count) :-
        aggregate_all(count, file_contains(File, Pattern), Count).

This can be called as (note that the pattern must be a string (code list)):

?- match_count('pure_input.pl', `file`, Count).
ClioPatria (version V3.1.1-51-ga0b30a5)