Next: , Previous: , Up: Run Intro   [Contents][Index]


3.5 Syntax Errors

Syntax errors are detected during reading1. Each clause, directive or, in general, any term read in by the built-in predicate read/1 that fails to comply with syntax requirements is displayed on the standard error stream as soon as it is read, along with its position in the input stream and a mark indicating the point in the string of symbols where the parser has failed to continue analysis, e.g.:

| memb(X, X$L).
! Syntax error
! , or ) expected in arguments
! in line 5
! memb ( X , X 
! <<here>>
! $ L ) . 

if ‘$’ has not been declared as an infix operator.

Note that any comments in the faulty line are not displayed with the error message. If you are in doubt about which clause was wrong, then you can use consult/12 to load the code and then use the listing/1 predicate to list all the clauses that were successfully read in, e.g.:

| ?- listing(memb/2).

Please note: The built-in predicates read/[1,2] normally raise an exception on syntax errors (see ref-ere). The behavior is controlled by the Prolog flag syntax_errors.


Footnotes

(1)

The SICStus Prolog IDE (see SPIDER) will show syntax errors and many other programming errors directly in the editor, while the code is written, without loading the code.

(2)

By default, e.g. when using [myprog], code is compiled. Compiled code can not be listed with listing/1. Using consult([myprog]) ensures that the code is interpreted, making it available for listing with listing/1.


Send feedback on this subject.