Prolog - parse with DCG -


i want write dcg can deal text presented in notepad. i've read online tutorial writing dcg none of delt text free , involves strings,dates , integers. i'm not sure how start writing dcg (how represent line or date). help?

the 'trick' it's approach problem in declarative way, giving first more selective patterns. data format has apparently defined columnar structure, , using library(dcg/basics) handled:

:- [library(dcg/basics)].  row([date,key|numerics]) -->  date(date), separe, key(key), separe, numeric_pairs(numerics).  date(d/m/y) -->  integer(d), "/", integer(m), "/", integer(y).  key([f|ks]) -->  [f], {f \= 0' }, string(ks).  numeric_pairs([num:perc|nps]) -->  integer(num), separe, number(perc), "%", whites, !, numeric_pairs(nps). numeric_pairs([]) --> [].  separe --> white, whites. 

test:

?- atom_codes('02/18/2014  bats z  235122734   6.90%   109183482   10.50%  147587409   7.80%', cs), phrase(row(r), cs). cs = [48, 50, 47, 49, 56, 47, 50, 48, 49|...], r = [2/18/2014, [66, 65, 84, 83, 32, 90], 235122734:6.9, 109183482:10.5, 147587409:7.8]  

i must isn't easy debug. when prolog backtracks have no hint going wrong... there should specialized trace, guess...

to feed dcg, see library(pure_input), or - easier debug - fetch line @ time, read_line_to_codes/2

edit maybe hit use read_line_to_codes/2 bad one.

here complete scan of test data, using phrase_from_file/2 , subsequent selection of appropriate columns , sum (column required argument).

:- [library(dcg/basics)]. :- [library(pure_input)].  test(coltosum, tot) :-     phrase_from_file(row(rows), '/tmp/test.txt'),     maplist(get_col(coltosum), rows, cols),     sum_list(cols, tot).  get_col(coltosum, row, col) :-     nth1(coltosum, row, col:_).  row([[date,key|numerics]|rows]) -->  date(date), separe, key(key), separe, numeric_pairs(numerics), "\n",  row(rows). row(rows) -->  string(_), "\n",  row(rows). row([]) --> [].  date(d/m/y) -->  integer(d), "/", integer(m), "/", integer(y).  key([f|ks]) -->  [f], {f \= 0' }, string(ks).  numeric_pairs([num:perc|nps]) -->  integer(num), separe, number(perc), "%", whites, !, numeric_pairs(nps). numeric_pairs([]) --> [].  separe --> white, whites. 

that yields

?- test(3,x). x = 561877153  

if you're using windows, use "\r\n" line terminator...

hth


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -