Difference between revisions of "Language Reference/Lexical Elements"

From wiki.visual-prolog.com
m (Example Template)
Line 106: Line 106:


Notice that <vp>div</vp> and <vp>mod</vp> are also reserved words, but these are categorized as operators.
Notice that <vp>div</vp> and <vp>mod</vp> are also reserved words, but these are categorized as operators.
<vp>guards</vp> and <vp>monitor</vp> are not used in the language, but are reserved for future usage.


==== Punctuation Marks ====
==== Punctuation Marks ====

Revision as of 12:23, 16 June 2010

The Visual Prolog Compiler is applied to a source file. This source file may include other source files, which are (conceptually) inserted into the original source file to constitute one compilation unit. The compilation of a compilation unit is done in two conceptual steps:

  • first the input is transformed into a sequence of tokens;
  • and then these tokens are syntactically analyzed and transformed into executable code.

The lexical analysis of the program will break the compilation unit CompilationUnit into a list of input elements InputElement

CompilationUnit:
   InputElement-list
InputElement:
   Comment
   WhiteSpace
   Token

Only tokens are significant to the subsequent syntax analysis.

Comments

A Visual Prolog comment is written in one of the following ways:

  • The /* (slash, asterisk) characters, followed by any sequence of characters (including new lines), terminated by the */ (asterisk, slash) characters. These comments can be multi-lined. They can also be nested.
  • The % (percent sign) character, followed by any sequence of characters. Comments that begin with character % (percent sign) continue until the end of the line. Therefore, they are commonly called single-line comments.

Notice the following comment example:

/* Begin of Comment1
   % Nested Comment2 */ This mark does not close a multi-line comment because it is inside a single-line comment
   This is the real termination of Comment1 */

Whitespace

WhiteSpace:
   Space
   Tab
   NewLine

Here Space is a space character, Tab is a tabulation character and NewLine is a new line character.

Tokens

Token:
   Identifier
   Keyword
   Punctuator
   Operator
   Literal

Identifiers

Identifier:
   LowercaseIdentifier
   UppercaseIdentifier
   AnonymousIdentifier
   Ellipsis

A LowercaseIdentifier is a sequence of letters, digits, and underscores that starts with a small letter. An UppercaseIdentifier is a sequence of letters, digits, and underscores that starts either with a capital letter or with an underscore.

AnonymousIdentifier :    _
Ellipsis :
   ...

Keywords

Keywords are divided into major and minor keywords, this division is only cosmetic however, there is no formal difference between major and minor keywords. In the sequel we will however use different coloring for them.

Keyword :
   MajorKeyword
   MinorKeyword
MajorKeyword : one of
   class clauses constants constructors
   delegate domains
   end
   facts
   goal guards
   implement inherits interface
   monitor
   namespace
   open
   predicates
   properties
   resolve
   supports
MinorKeyword : one of
   align and anyflow as 
   bitsize
   catch
   determ digits div do
   else elseif  erroneous externally
   failure finally foreach from
   if
   language
   mod multi
   nondeterm
   or
   procedure
   quot
   rem
   single
   then to try

All keywords except as and language are reserved words.

Notice that div and mod are also reserved words, but these are categorized as operators.

Punctuation Marks

Punctuation marks in Visual Prolog have syntactic and semantic meaning to the compiler, but do not specify by themselves an operation that yields a value. Some punctuation marks, either alone or in combinations, can also be Visual Prolog operators.

Punctuation marks are:

PunctuationMarks: one of
   ;    !    ,    .    #    [    ]    |    (    )   :-    :    ::

Operators

Operators specify an evaluation to be performed on involved operands.

Operators: one of
      +    -    /    *    ^    =  div mod quot rem
   <    >    <>    ><    <=    >=    :=

All operators are binary, but - and + also exist as unary operators.

div and mod are reserved words.

Literals

Literals fall into following categories: integer, character, floating-point, string, binary and list.

Literal:
   IntegerLiteral
   RealLiteral
   CharacterLiteral
   StringLiteral
   BinaryLiteral
   ListLiteral
   CompoundDomainLiteral

Integral Literals

IntegerLiteral:
   UnaryPlus-opt    DecimalDigit-list
   UnaryMinus-opt   DecimalDigit-list
   UnaryPlus-opt    OctalPrefix OctalDigit-list
   UnaryMinus-opt   OctalPrefix OctalDigit-list
   UnaryPlus-opt    HexadecimalPrefix HexadecimalDigit-list
   UnaryMinus-opt   HexadecimalPrefix HexadecimalDigit-list
 
UnaryPlus:
   +
 
UnaryMinus:
   -
 
OctalPrefix:
   0o
 
OctalDigit: one of
   0 1 2 3 4 5 6 7
 
DecimalDigit: one of
   0 1 2 3 4 5 6 7 8 9
 
HexadecimalPrefix:
   0x
 
HexadecimalDigit: one of
   0 1 2 3 4 5 6 7 8 9 A a B b C c D d E e F f

An integral literal can belong to integer or unsigned domains and it should not exceed maximum and minimum integer or unsigned values.

Real Literal

RealLiteral:
   UnaryMinus-opt DecimalDigit-list FractionOfFloat-opt Exponent-opt
 
FractionOfFloat:
   . DecimalDigit-list
 
Exponent:
   ExponentSymbol ExponentSign-opt DecimalDigit-list
 
ExponentSymbol: one of
   e E
 
ExponentSign: one of
   - +

A floating literal should not exceed maximum and minimum real values.

Character Literals

CharacterLiteral:
   ' <CharacterValue> '

CharacterValue can be any printable character or an escape sequence:

  • \\ representing \
  • \t representing Tab character
  • \n representing New Line character
  • \r representing carriage return
  • \' representing single quote
  • \" representing double quote
  • \uXXXX, here XXXX should be exactly four HexadecimalDigit's representing the Unicode character corresponding to the digits.

String Literals

StringLiteral:
   StringLiteralPart-list
StringLiteralPart:
   @" <AnyCharacter>-list-opt "
   " <CharacterValue>-list-opt "

A string literal consists of one or more StringLiteralPart's, which are concatenated.

StringLiteralPart's starting with @ does not use escape sequences, except for the:

  • "" representing double quote.

Whereas StringLiteralPart's without @ uses the following escape sequences:

  • \\ representing \
  • \t representing Tab character
  • \n representing New Line character
  • \r representing carriage return
  • \" representing double quote
  • \uXXXX, here XXXX should be exactly four HexadecimalDigit's representing the Unicode character corresponding to the digits.

Binary Literals

BinaryLiteral:
   $[ ElementValue-comma-sep-list-opt ]
ElementValue:
   IntegerLiteral

ElementValue should be any integral arithmetic expression (for example, constant), which should be calculated while compilation-time and be in the range from 0 till 255.

List Literals

All elements in a list literal must belong to the same domain (or to compatible domains). This domain can be any built-in or user defined domain, for example, it can be integral, character, binary, compound domain, etc.

ListLiteral:
   [ SimpleLiteral-comma-sep-list-opt ]
   [ SimpleLiteral-comma-sep-list | ListLiteral ]

Here SimpleLiteral can be:

SimpleLiteral:
   IntegerLiteral
   RealLiteral
   CharacterLiteral
   StringLiteral
   BinaryLiteral
   ListLiteral
   CompoundDomainLiteral
Example These are all valid list literals:
[]                    % empty list
[1,2,3]               % list of integers
["abc", "defg"]       % list of strings
Example This is invalid because the elements in a list must all have same type:
[1,"abc"]             % this is INVALID list

Compound Domain Literals

Terms of user defined compound domains can be treated as literals if all their arguments are literals.