我似乎一直在努力编写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