我似乎一直在努力編寫DCG來解析輸入文件。但它似乎應該很簡單?是否有任何提示或技巧來思考這個問題?創建文件輸入dcg的一般模式是什麼?
對於一個具體的例子,可以說我想解析一個fasta文件。 (https://en.wikipedia.org/wiki/FASTA_format)。我想閱讀每個描述和每個序列的後面跟蹤。
:- use_module(library(pio)).
:- use_module(library(dcg/basics)).
:- portray_text(true).
:- set_prolog_flag(double_quotes, codes).
:- set_prolog_flag(back_quotes,string).
fasta_file([]) -->[].
fasta_file([Section|Sections]) -->
fasta_section(Section),
fasta_file(Sections).
fasta_section(Section) -->
fasta_description(Description),
fasta_seq(Sequence),
{Section =.. [section,Description,Sequence]}.
fasta_description(Description) -->
">",
string(Description),
{no_gt(Description),
no_nl(Description)}.
fasta_seq([]) --> [].
fasta_seq(Seq) -->
nt([S]),
fasta_seq(Ss),
{S="X"->Seq =Ss;Seq=[S|Ss]}.
nt("A") --> "A".
nt("C") --> "C".
nt("G") --> "G".
nt("T") --> "T".
nt("X") --> "\n".
no_gt([]).
no_gt([E|Es]):-
dif([E],">"),
no_gt(Es).
no_nl([]).
no_nl([E|Es]):-
dif([E],"\n"),
no_nl(Es).
現在這顯然是錯誤的。我想該行爲是
?-phrase(fasta_section(S),">frog\nACGGGGTACG\n>duck\nACGTTAG").
S = section("frog","ACGGGGTACG");
S = section("duck","ACGTTAG");
false.
但是,如果我做了phrase(fasta_file(Sections),">frog\nACGGGGTACG\n>duck\nACGTTAG).
科統一使用節/ 2秒的列表,這是我想要的,但我現在的代碼似乎相當hacky-我是如何處理的換行符例如。
錯字:'S =「X」 - >'應該讀取'[S] =「X」 - >' – false
[This answer] (HTTP://計算器。com/a/12942551/772868)將會對你感興趣。 – false
用{{Section = section(Description,Sequence)}替換'{Section = .. [section,Description,Sequence]}'' – false