syntax for prolog

Upload: rockbrentwood

Post on 03-Jun-2018

212 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Syntax for Prolog

    1/4

    Full Prolog SyntaxThe fundamental syntactic construct in Prolog is the term. At the topmost level, itself, the terms areinterpreted as sentences in the manner described below. There is a limited ability to define or redefine thesyntax of terms, these facilities, therefore, are also available for defining the syntax of sentences.

    Each term comprises a sequence of tokens; each token, in turn, being treated as a single symbol that is

    spelled out in 1, 2 or more characters. The tokens include those for variables, constants, functorsas wellas bracketsand punctuation. The tokencomposition of a term is described in further detail below. Thespellings of the tokens are also described in further detail below.

    When a sequence of tokens is read in, it must be terminated by the full-stop token. Wherever theconcatenation of two or more tokens would produce the spelling of another token, the tokens need to beseparated by a layout-text token in order to prevent the misreading. The layout-text tokens may bearbitrarily interspersed in a program and serve no other function than to separate other tokens.

    SentencesS a:S Sentence labeled by module nameS L A sentence in list formS H[:-B] Clause (if :-Bis absent,Hmust not be otherwise interpretable as a sentence)

    S :-B CommandS ?-B QueryS H->B Grammar rule

    H a:H Module labelH T Goal; Tmay not be an Xor have any of ,;:\+->as its main operator.

    B a:B Module labelB B->B;B ConditionalB B->B Elseless conditionalB \+BB B;B DisjunctionB B,B Sequencing

    B

    T Goal; Tmay not be an Xor have any of ,;:\+->as its main operator.

    In a grammar rule, instances ofHor Bof the formsLor Sare interpreted respectively as a list of terminalsor as a literal terminal; instances ofBof the forms {B}or !are interpreted as conditions; instances ofHorB of the forms Tare interpreted as non-terminals. In H or B, instances of Tmay not have the form of avariable X, or contain any of the operators ,;\+->as their main operator.

    TermsAs mentioned previously, when a sequence of tokens comprising a term is read in, it must end in a full-stop. The syntax is: T1200full-stop. There are a set of precedence levels, ranging from 0 to 1200, for terms.This facility is provided to allow certain operators and punctuation to bind more tightly than others, and itis available to enable one to partially define or redefine the syntax of terms.

    Tn+1 Tn 0 n< 1200Tn fxnTn-1 except for number.Tn fynTnTn Tn-1xfxnTn-1Tn Tn-1xfynTnTn TnyfxnTn-1Tn Tn-1xfnTn TnyfnT1000 T999,T1000 The comma operator is xfy1000.

  • 8/12/2019 Syntax for Prolog

    2/4

    T0 a[(T999(,T999)*)] Function or constant.

    T0 (T1200)T0 {T1200}T0 L ListT0 S StringT0 N Numeric constantsT0 X Variable

    L [[T999(,T999)*[|T999]] ]

    (The Prolog syntax apparently doesnt allow two or more T999to precede the separator |.)

    N [+|-]NumeralN (+|-) (inf| nan)

    All the tokens fxn, fyn, xfxn, xfyn, yfxn, xfn, yfnare instances of awhich have been specially declared asoperators of the respective precedence and associativity. The operators of the forms * f, *f* and f* areprefix, infix and postfix. Those of the forms yf? are left-associative and those of the form ?fyare right-associative.

    The comma operator , is fixed in the syntax above as of type xfy1000. A term T1000 of the form A,B is

    interpreted in the standard syntax as ,(A,B); while the terms T0 of the form {A}and (A)are interpreted,respectively, as {}(A)andA.

    In order to disambiguate between an operator followed by a parenthesis versus a function-call, arequirement is imposed that (must immediately follow the aof the function in a function call, and (mustbe separated from fxnor fynin an operator application.

    Also, in case of ambiguity, the interpretation of an instance of aas a prefix operator, fxnor fyn, whereverpossible, wins out; and the interpretation of an instance of xfn or yfn as xfxn, xfyn or yfxn, whereverpossible, wins out.

    TokensThe standard character set for SICStus Prolog is ISO 8859/1, but EUC (Extended UNIX Code) is supported

    as an alternative. The environment variable SP_CTYPE determines which applies.

    The character categories used below are defined as follows in the two standardsClass ISO 8859/1 Minimal SetLayout 0-32,127,159 TAB, LFD, SPCSmall 97-122,223-246,248-255 a-zCapital 65-90,192-214,216-222 A-ZDigit 48-57 0-9Symbol 35,36,38,42,43,45-47,58,60-64,92,94,96,126,160-191,215,247 +-*/\^=`~;.?@#$&Solo 33,59 !;Punctuation 37,40,41,44,91,93,123-125 %(),[]{|}Quote 34,39 " 'Underline 95 _

    The EUC differs from the ISO 8859/1 in that all the characters 128-255 are counted as Small, whichchanges the meanings of the characters whose codes are listed above as italicized, as well as the meaningsof characters 128-158, which are not listed above.

    Token aToken NumeralToken XToken S

  • 8/12/2019 Syntax for Prolog

    3/4

    Token PunctuationToken LayoutItem+Token full-stop

    a 'C*'a Small (Capital | Small | Digit | Underline)*a Symbol+a Soloa [LayoutItem*]a {LayoutItem*}

    Neither the full-stopnor any symbol sequence starting in/*counts as an a.

    Numeral Digit+Numeral Digit+'(Capital | Small | Digit)*Numeral 0'CNumeral Digit+.Digit+[[e|E] [+|-] Digit+]

    In the base-radix notation R'N, the digits comprising N must be of value no larger than R, with a,b, andA,B, both being treated as being of values 10,11,. The base R must in the range [2,36]. Note that 3 is

    identified as a numeral, whereas (3) as an application of the operator to the numeral 3.

    X (_ | Capital) (Capital | Small | Digit | Underline)*

    S " C*"

    C Char, other than\C \Esc

    In a, a 'must be duplicated. In S, "must be duplicated.

    LayoutItem LayoutLayoutItem /*Char**/

    LayoutItem

    %Char*

    LFD

    The character sequences in the /* and % comments main not contain the respective ending sequences */ orLFD.

    full-stop .

    A full stop must be separated from subsequent tokens by LayoutItems.

    Char Any character, including Layout, Small, Capital, Digit, Underline, Symbol, Solo, Punctuation andQuote.

    Esc a|b|t|n|v|f|r|e|d

    Esc

    x(Capital | Small | Digit)

    2

    Esc Digit [Digit] [Digit]Esc ^(?| Capital | Small)Esc cLayout*Esc LayoutEsc Char

    The single-character escapes are, respectively, the alarm, backspace, horizontal tab, newline, vertical tab,form feed, carriage return, escape and delete (codes 7-13, 27 and 127). The escapes starting in x arehexadecimal escapes; with the following characters ranging from 0-9, A-F and a-f. The escapes starting in a

  • 8/12/2019 Syntax for Prolog

    4/4

    digit are octal escapes, and the digits must all be in the range 0-7. The escapes starting in ^ are control-sequences, with ^? standing for delete (127) and the corresponding codes for the other controls being takenas the code of the character following the ^, modulo 32. Escape sequences involving layout are ignored(and that starting in cis taken with the longest layout sequence that occurs following it), and the escapesequence of any other character is taken to stand for the character, itself.

    The escape sequence feature can be turned off to assure compatibility with older Prolog programs.