next up previous index
Next: Syntax Differences between ECLiPSe Up: Syntax Previous: Notation   Index

Subsections

Formal definition of clause syntax

What follows is the specification of the syntax. The terminal symbols are written in UPPER CASE or as the character sequence they consist of.
program                 ::=     clause EOCL
                         |      clause EOCL program

clause                  ::=     head
                         |      head rulech goals
                         |      rulech goals

head                    ::=     term_h

goals                   ::=     term_g
                         |      goals , goals
                         |      goals ; goals
                         |      goals -> goals
                         |      goals -> goals ; body

term_h                  ::=     term_h(0)
                         |      term(1200)

term_g                  ::=     term_g(0)
                         |      term(1200)

term(0)                 ::=      VAR            /* not a term_h */
                         |       metaterm       /* not a term_h */
                         |       ATOM
                         |       structure
                         |       subscript
                         |       list
                         |       STRING         /* not a term_h nor a term_g */
                         |       number         /* not a term_h nor a term_g */
                         |       bterm

term(N)                 ::=     term(0)
                         |      prefix_expression(N)
                         |      infix_expression(N)
                         |      postfix_expression(N)

prefix_expression(N)    ::=     fx(N) term(N-1)
                         |      fy(N) term(N)

infix_expression(N)     ::=     term(N-1)       xfx(N)  term(N-1)
                         |      term(N)         yfx(N)  term(N-1)
                         |      term(N-1)       xfy(N)  term(N)

postfix_expression(N)   ::=     term(N-1)       xf(N)
                         |      term(N) yf(N)

metaterm                ::=     VAR { meta_attributes }

meta_attributes         ::=     attribute
                         |      attribute , meta_attributes

attribute               ::=     qualified_attribute
                         |      nonqualified_attribute

qualified_attribute     ::=     ATOM : nonqualified_attribute

nonqualified_attribute  ::=     term_a

structure               ::=     functor ( termlist )

subscript               ::=     structure list
                         |      VAR list

termlist                ::=      term_a
                         |       term_a , termlist

list                    ::=     [ listexpr ]
                         |      .(term_a, term_a)

listexpr                ::=     term_a
                         |      term_a | term_a
                         |      term_a , listexpr

term_a                  ::=     term(1200)
                                /* Note: it depends on syntax_options */

number                  ::=     INT
                         |      INTBAS
                         |      RAT
                         |      FLOAT
                         |      BREAL

bterm                   ::=     ( clause )
                         |      { clause }

functor                 ::=     ATOM                    /* arity > 0 */

rulech                  ::=     :-
                         |      ?-

Comments

Comments can be full line comments, that is enclosed by CM1-CM2 and CM2-CM1, or end of line comments, that is enclosed by CM and NL. They behave as separators.

Operators

In Prolog, the user is able to modify the syntax dynamically by explicitly declaring new operators. The builtin op/3 performs this task. As in Edinburgh Prolog, a lower precedence value means that the operator binds stronger (1 strongest, 1200 weakest).

Multiple definitions are handled in the following way:

When entering ECLiPSe, some operators are pre-defined (see Appendix B on page [*]). They may be redefined if desired.

Note that parentheses are used to build expressions with precedence zero and thus to override operator declarations. Quotes, on the other hand, are used to build atoms from characters with different or mixed character classes; they do not change the precedence of operators.

Operators are by default local to a module unless they are declared with global_op/3. The operator visible in a module is either the local one (if any) or the global one.

Ambiguity

The prolog syntax allows some ambiguity when parsing operators. To solve it, we could do a lookahead(N) or a backtracking. ECLiPSe uses a lookahead(1), which allows to solve the main cases of ambiguity.

If a prefix operator is followed by an infix operator the system decides which token is an atom and which is the operator by examining the precedence. The token with the higher precedence becomes the operator. If the precedences are the same, the associativity of the operators is examined. If the prefix operator is fy, it becomes the operator. If the infix operator is yfx or yfy it becomes the operator. Otherwise an error is raised.


next up previous index
Next: Syntax Differences between ECLiPSe Up: Syntax Previous: Notation   Index
Warwick Harvey
2004-08-07