VitGryfny
VitGryfny

Reputation: 13

CLIPS - if then else function gives [CSTRCPSR1] err

Here is the error summary:

CLIPS> (load "C:/Users/labor/Desktop/Witek/projekt.CLP")
Defining defrule: R1 +j+j
Defining defrule: R2 +j+j
Defining defrule: R3 =j+j+j
Defining defrule: imie-if =j=j+j+j

[CSTRCPSR1] Expected the beginning of a construct.

And here is the code for my CLIPS program. Basically I want it to react different if the name and last name are different from Abraham Lincoln.

(defrule R1
(initial-fact)
=>
(printout t "Enter your name:" crlf)
(bind ?name (read))
(assert (name ?name)))

(defrule R2
(name ?name)
=>
(printout t "Enter your last name" crlf)
(bind ?lastnm (read))
(assert (lastnm ?lastnm)))

(defrule R3
(and(name ?name)(lastnm ?lastnm))
=>
(printout t "How old are you " ?name "?" crlf)
(bind ?age (read))
(assert (age ?age)))

(defrule name-if
(name ?name)(lastnm ?lastnm)(age ?age)
=>
(if(and(eq ?name Abraham)(eq ?lastnm Lincoln))
then (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old bro" crlf))
else (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old" crlf)))

I copied the if statement logic from some webpage and I am not quite sure what, in this case, 'eq' stands for... i'd appreciate if you could additionally explain the role of it.

Regards, W

Upvotes: 1

Views: 7706

Answers (1)

Gary Riley
Gary Riley

Reputation: 10757

You have an extra right parenthesis at the end of the then clause that is causing the issue. The Mac OS and Window CLIPS IDEs have a balance command that you can use to see if the parentheses are properly balanced within a construct. Just click inside a construct and apply the balance command several times until the entire construct is selected. If you place the cursor by the then keyword and balance, you'll see that the if statement is closed by the parenthesis at the end of the then clause and the else clause is left dangling.

enter image description here

The corrected rule is:

(defrule name-if
   (name ?name)
   (lastnm ?lastnm)
   (age ?age)
   =>
   (if (and (eq ?name Abraham)
            (eq ?lastnm Lincoln))
      then 
      (printout t "Hello" ?name " " ?lastnm ", you are " ?age " years old bro" crlf)
      else 
      (printout t "Hello " ?name " " ?lastnm ", you are " ?age " years old" crlf)))

The eq predicate is short for equals. Unlike the = predicate that expects numeric arguments, eq compares values of any type.

Upvotes: 1

Related Questions