2013-05-09 178 views
0

我找不出这个代码中有什么问题。请帮助我。我得到这个错误:PROLOG:“语法错误:操作员期望”

ERROR: c:/users/ahmed/downloads/test.pl:2:4: Syntax error: Operator expected 
% c:/Users/Ahmed/Downloads/test.pl compiled 0.00 sec, 21 clauses 

下面是代码:

domains 
disease,indication = symbol 
Patient,name = string 

predicates 
hypothesis(string,disease) 
symptom(name,indication) 
response(char) 
go 

clauses 
go :- 
    write("What is the patient's name? "), 
    readln(Patient), 
    hypothesis(Patient,Disease), 
    write(Patient,"probably has ",Disease,"."),nl. 

go :- 
    write("Sorry, I don't seem to be able to"),nl, 
    write("diagnose the disease."),nl. 

symptom(Patient,fever) :- 
    write("Does ",Patient," have a fever (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,rash) :- 
    write("Does ",Patient," have a rash (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,headache) :- 
    write("Does ",Patient," have a headache (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,runny_nose) :- 
    write("Does ",Patient," have a runny_nose (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,conjunctivitis) :- 
    write("Does ",Patient," have a conjunctivitis (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,cough) :- 
    write("Does ",Patient," have a cough (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,body_ache) :- 
    write("Does ",Patient," have a body_ache (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,chills) :- 
    write("Does ",Patient," have a chills (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,sore_throat) :- 
    write("Does ",Patient," have a sore_throat (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,sneezing) :- 
    write("Does ",Patient," have a sneezing (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

symptom(Patient,swollen_glands) :- 
    write("Does ",Patient," have a swollen_glands (y/n) ?"), 
    response(Reply), 
    Reply='y'. 

hypothesis(Patient,measles) :- 
    symptom(Patient,fever), 
    symptom(Patient,cough), 
    symptom(Patient,conjunctivitis), 
    symptom(Patient,runny_nose), 
    symptom(Patient,rash). 

hypothesis(Patient,german_measles) :- 
    symptom(Patient,fever), 
    symptom(Patient,headache), 
    symptom(Patient,runny_nose), 
    symptom(Patient,rash). 

hypothesis(Patient,flu) :- 
    symptom(Patient,fever), 
    symptom(Patient,headache), 
    symptom(Patient,body_ache), 
    symptom(Patient,conjunctivitis), 
    symptom(Patient,chills), 
    symptom(Patient,sore_throat), 
    symptom(Patient,runny_nose), 
    symptom(Patient,cough). 

hypothesis(Patient,common_cold) :- 
    symptom(Patient,headache), 
    symptom(Patient,sneezing), 
    symptom(Patient,sore_throat), 
    symptom(Patient,runny_nose), 
    symptom(Patient,chills). 

hypothesis(Patient,mumps) :- 
    symptom(Patient,fever), 
    symptom(Patient,swollen_glands). 

hypothesis(Patient,chicken_pox) :- 
    symptom(Patient,fever), 
    symptom(Patient,chills), 
    symptom(Patient,body_ache), 
    symptom(Patient,rash). 

hypothesis(Patient,measles) :- 
    symptom(Patient,cough), 
    symptom(Patient,sneezing), 
    symptom(Patient,runny_nose). 

response(Reply) :- 
    readchar(Reply), 
    write(Reply),nl. 

回答

3

您正在使用SWI-Prolog的,随后爱丁堡语法。代码看起来像Visual Prolog的代码,它遵循不同的语法规则。如果您想在SWI-Prolog下运行它,则需要将代码移植到Edingburgh语法。否则,您可以安装Visual Prolog并在其中运行代码。

+0

同意。我建议删除文件顶部的前11行(通过“子句”),看看有什么结果。 – 2013-05-09 23:57:55

+0

试图删除前11行。得到这个错误 错误:c:/users/ahmed/downloads/test.pl:2: 在子句体内完全停止?无法重新定义,/ 2 %c:/Users/Ahmed/Downloads/test.pl编译0.00秒,21条款 – 2013-05-10 00:07:59

+0

@AhmedFouda:看起来你不太了解prolog。如果您需要使用Visual Prolog源代码,那么下载Visual Prolog会更好 - 它可以免费供个人使用。见http://www.visual-prolog.com/vip6/product/default.htm – liori 2013-05-10 00:14:46

1

能够运行在SWI-Prolog的代码,这样你可以更换开始行(直到go :- ...):

:- redefine_system_predicate(write(_)). 
:- redefine_system_predicate(readln(_)). 

write(S) :- 
    is_list(S) -> format('~s', [S]) ; format('~w', [S]). 
write(A,B,C,D) :- 
    maplist(write, [A,B,C,D]). 
write(A,B,C) :- 
    maplist(write, [A,B,C]). 
readchar(S) :- 
    get(C), atom_codes(S, [C]). 
readln(A) :- 
    system:readln(L), atomic_list_concat(L,' ',A). 

go :- 
    ... 

那些 '适配器' 谓词

?- go. 
What is the patient's name? Carlo 
Does Carlo have a fever (y/n) ?y 
y 
Does Carlo have a cough (y/n) ?|: y 
y 
Does Carlo have a conjunctivitis (y/n) ?|: y 
y 
Does Carlo have a runny_nose (y/n) ?|: y 
y 
Does Carlo have a rash (y/n) ?|: y 
y 
Carloprobably has measles. 
true . 

现在该知道什么是麻疹。去维基百科... :)

相关问题