如何修复Prolog语法错误:操作员预期错误?

时间:2014-03-06 02:29:00

标签: swi-prolog visual-prolog

PROLOG: “Syntax error: Operator expected”
ERROR: c:/users/zulfekarali/Desktop/KBS/kbs.pl:2:3: Syntax error: Operator expected
% c:/Users/ZulfekarAli/Desktop/KBS/kbs.pl compiled 0.00 sec, 27 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.

2 个答案:

答案 0 :(得分:1)

domains
    disease,indication = symbol
    Patient,name = string

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

我认为这段代码应该使用%符号进行评论,以便修复语法错误。

在这里,作为另一个问题,您可能希望在谓词的最后剪切(!),否则Prolog因为回溯将在第一个成功时尝试第二个go

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

答案 1 :(得分:1)

由于多种原因,您提供的代码是无效的SWI-Prolog代码。

1

在第一次出现go:-之前已经观察到未评论的自由文本。

2

谓词write/[3,4]在SWI-Prolog中不存在。

3

readln/1中存在谓词readln。即使此模块是自动加载的,最好使用文件顶部的声明:- use_module(library(readln)).来显示依赖项。

更重要的是,readln/1会返回列表,但hypothesis/2symptom/2似乎会预期非列表输入。

4

readln/1 atoms 的元素与write/[3,4]的其他参数 strings 之间存在不匹配。