Next: , Previous: , Up: ref-ere-err   [Contents][Index]


4.15.4.11 Syntax Errors

A syntax error occurs when data are read from some external source but have an improper format or cannot be processed for some other reason. This category mainly applies to read/1 and its variants.

The SICStus_Error term associated with a syntax error is

syntax_error(Goal, Position, Message, Left, Right)

where Goal is the goal in question, Position identifies the position in the stream where reading started, and Message describes the error. Left and right are lists of tokens before and after the error, respectively.

Note that the Position is where reading started, not where the error is.

read/1 does two things. First, it reads a sequence of characters from the current input stream up to and including a clause terminator, or the end of file marker, whichever comes first. Then it attempts to parse the sequence of characters as a Prolog term. If the parse is unsuccessful, then a syntax error occurs. Thus, in the case of syntax errors, read/1 disobeys the normal rule that predicates should detect and report errors before they perform any side effects, because the side effect of reading the characters has been done.

A syntax error does not necessarily cause an exception to be thrown. For I/O predicates, but not for number_chars/2 and number_codes/2, The behavior can be controlled via the syntax_errors Prolog flag (see ref-lps-flg), or via the syntax_errors/1 option to read_term/[2,3]. The following values are possible:

quiet

When a syntax error is detected, nothing is printed, and read/1 just quietly fails.

dec10

This provides compatibility with other Prologs: when a syntax error is detected, a syntax error message is issued with print_message/2, and the read is repeated. This is the default.

fail

This provides compatibility with other Prologs. When a syntax error is detected, a syntax error message is printed on user_error, and the read then fails.

error

When a syntax error is detected, an exception is thrown.



Send feedback on this subject.