如何修复SWI-Prolog代码中的Syntax error: operator expected错误
Prolog代码语法错误修复方案
第一个错误(第165行)
问题出在retract(at(you,maze(2)),这一行,括号未正确闭合,Prolog无法解析后续的assert和!,触发"Operator expected"错误。
修正后的代码:
/*does nothing if you aren't there*/ key:- at(you,key), write('you have found the key'), retract(at(you,maze(2))), % 补全闭合括号 assert(at(you,key)), !. /*If you are at the same place as the magic pants*/ magic_pants:- at(magic_pants,Loc), at(you,Loc), write('Magic pants?! I wonder what these do!'), !.
第二个错误(第291行)
问题出在go谓词的前几行,多个谓词调用的括号未闭合:
retract(at(you, maze(2)),缺少闭合括号move(at(you, treasure),缺少闭合括号move(at(you, mountaintop),缺少闭合括号
未闭合的括号导致Prolog解析到main时语法混乱,触发错误。
修正后的代码:
go :- retractall(at(_,_)), /* clean up from previous runs */ retract(at(you, maze(2))), % 补全闭合括号 move(at(you, treasure)), % 补全闭合括号 move(at(you, mountaintop)), % 补全闭合括号 assert(at(you,valley)), assert(at(ogre,maze(3))), assert(at(key,maze(2))), assert(at(gate_pants,meadow)), assert(at(treasure,mountaintop)), write('This is an adventure game.\n'), write('Legal moves are left, right, forward, or backward.\n'), write('To pick up items type take(x).\n'), write('To drop items drop(x).\n'), write('End each move with a period.\n\n'), report, main.
内容的提问来源于stack exchange,提问作者Gene Holt
相关产品推荐
相关产品推荐

