| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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). |
|
#3
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.