Reading in a PrologScript file in SWI-Prolog

This is a discussion on Reading in a PrologScript file in SWI-Prolog within the PROLOG forums in Programming Languages category; Hi, I am trying to read a Prolog file in SWI-Prolog in order to perform analysis on the file. My first thought was to use read_file_to_terms/3. Unfortunately, the majority of Prolog files I'm looking at start with a PrologScript shebang like: #!/usr/bin/pl -q -t main -f and read_file_to_terms/3 fails when this is included. My simplest idea would be to open the file, read the first line using read_line_to_codes/2, convert this line into an atom, establish whether this is equivalent to a shebang declaration and if not do an atom_to_term to convert this into a term then process the rest of ...

Go Back   Application Development Forum > Programming Languages > PROLOG

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 07-31-2008, 09:17 PM
Katie Duczmal
Guest
 
Default Reading in a PrologScript file in SWI-Prolog

Hi,

I am trying to read a Prolog file in SWI-Prolog in order to perform
analysis on the file. My first thought was to use read_file_to_terms/3.
Unfortunately, the majority of Prolog files I'm looking at start with
a PrologScript shebang like:

#!/usr/bin/pl -q -t main -f

and read_file_to_terms/3 fails when this is included.

My simplest idea would be to open the file, read the first line using
read_line_to_codes/2, convert this line into an atom, establish whether
this is equivalent to a shebang declaration and if not do an
atom_to_term to convert this into a term then process the rest of the
list using read_term.

Would this be the best way to do it or are there alternative ways of
handling this case in a more straightforward manner.

Thanks,
Katie
Reply With Quote
  #2  
Old 08-01-2008, 01:18 AM
phi500ac@yahoo.ca
Guest
 
Default Re: Reading in a PrologScript file in SWI-Prolog

On 31 jul, 22:17, Katie Duczmal <ducz...@itee.uq.edu.au> wrote:
> Hi,
>
> I am trying to read a Prolog file in SWI-Prolog in order to perform
> analysis on the file. My first thought was to use read_file_to_terms/3.
> Unfortunately, the majority of Prolog files I'm looking at start with
> a PrologScript shebang like:
>
> #!/usr/bin/pl -q -t main -f
>
> and read_file_to_terms/3 fails when this is included.
>
> My simplest idea would be to open the file, read the first line using
> read_line_to_codes/2, convert this line into an atom, establish whether
> this is equivalent to a shebang declaration and if not do an
> atom_to_term to convert this into a term then process the rest of the
> list using read_term.
>
> Would this be the best way to do it or are there alternative ways of
> handling this case in a more straightforward manner.
>
> Thanks,
> Katie


Hi, Katie.
I had a similar problem: I needed to read files containing French
text, in order to find words with accented letters and analyze them. I
solved the problem by writing a tokenizer to read codes from streams.
Here is a simplified version of my program:

separator(X) :- X<65/*A*/, X>32/*space*/, X \= 45.

tokeniz(_Str, Ch, []) :- member(Ch, " \n\t"), !.
tokeniz(Str, 44, []) :- !, unget_code(Str, 44).
tokeniz(_Str, 39, [39]) :- !.
tokeniz(_Str, Ch, []) :- separator(Ch), !.
tokeniz(_Str, Ch, []) :- Ch<0, !.
tokeniz(Str, Ch, [Ch|Rs]) :- get_lower(Str, NxtChr), tokeniz(Str,
NxtChr, Rs).

tokens(_Str, Ch, []) :- Ch<0, !.
tokens(Str, 44, [','|Rs]) :- !, get_lower(Str, NxtCh), tokens(Str,
NxtCh, Rs).
tokens(Str, Ch, Rs) :- separator(Ch), !, get_lower(Str, NxtCh),
tokens(Str, NxtCh, Rs).
tokens(Str, Ch, L) :- Ch =< 32, !, get_lower(Str, NxtCh),
tokens(Str, NxtCh, L).
tokens(Stre, Ch, [AW|L]) :- tokeniz(Stre, Ch, W), atom_codes(AW, W),
get_lower(Stre, NxtCh),
tokens(Stre, NxtCh, L).

accents("áéíóúâêô").

get_lower(Stre, X) :- get_code(Stre, C), to_lower(C, X).

to_lower(X, L) :- X >= 65, X =< 90, !, L is X+32.
to_lower(X, L) :- member(X, "ÁÉÍÓÚÂÊÔÃÕ"), !, L is X+32..
to_lower(X, X).
Reply With Quote
  #3  
Old 08-01-2008, 04:02 AM
Jan Wielemaker
Guest
 
Default Re: Reading in a PrologScript file in SWI-Prolog

On 2008-08-01, Katie Duczmal <duczmal@itee.uq.edu.au> wrote:
> Hi,
>
> I am trying to read a Prolog file in SWI-Prolog in order to perform
> analysis on the file. My first thought was to use read_file_to_terms/3.
> Unfortunately, the majority of Prolog files I'm looking at start with
> a PrologScript shebang like:
>
> #!/usr/bin/pl -q -t main -f
>
> and read_file_to_terms/3 fails when this is included.
>
> My simplest idea would be to open the file, read the first line using
> read_line_to_codes/2, convert this line into an atom, establish whether
> this is equivalent to a shebang declaration and if not do an
> atom_to_term to convert this into a term then process the rest of the
> list using read_term.
>
> Would this be the best way to do it or are there alternative ways of
> handling this case in a more straightforward manner.


See the library(prolog_source), which provides some centralised support
for reading Prolog source files, such as dealing with #!, take care of
operator descriptions and term-expansion. It isn't rocket science, but
it might do the job for you. If not, please help improving it.

Cheers --- Jan
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 03:55 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.