Difference between revisions of "Language Reference/Lexical Elements"

From wiki.visual-prolog.com
(Operators)
 
(11 intermediate revisions by 2 users not shown)
Line 68: Line 68:
<vipbnf><Keyword> :
<vipbnf><Keyword> :
   <MajorKeyword>
   <MajorKeyword>
   <MinorKeyword></vipbnf>
   <MinorKeyword>
</vipbnf>


<vipbnf><MajorKeyword> : one of
<vipbnf><MajorKeyword> : one of
Line 75: Line 76:
   end
   end
   facts
   facts
   goal guard
   goal
   implement inherits interface
   implement inherits interface
   monitor
   monitor
Line 83: Line 84:
   properties
   properties
   resolve
   resolve
   supports</vipbnf>
   supports
  where
</vipbnf>


<vipbnf><MinorKeyword> : one of
<vipbnf><MinorKeyword> : one of
Line 92: Line 95:
   else elseif  erroneous externally
   else elseif  erroneous externally
   failure finally foreach from
   failure finally foreach from
   if
  guard
   if in
   language
   language
   mod multi
   mod multi
   nondeterm
   nondeterm
   or
   otherwise or orelse
   procedure
   procedure
   quot
   quot
   rem
   rem
   single
   single
   then to try</vipbnf>
   then to try
</vipbnf>


All keywords except <vp>as</vp> and <vp>language</vp> are reserved words.
All keywords except <vp>as</vp> and <vp>language</vp> are reserved words.
Line 130: Line 135:
   /    *  div mod quot rem
   /    *  div mod quot rem
   +    -
   +    -
   =    <    >    <>    ><    <=    >=    :=
   =    <    >    <>    ><    <=    >=    :=   ==
  ~~  ++    **    ^^    >>    <<
</vipbnf>
</vipbnf>


All operators are {{lang2|Terms|Operators|binary}}, but <vp>-</vp> and <vp>+</vp> also exist as {{lang2|Terms|Operators|unary}} operators.
* <vp>~~</vp> is a  {{lang2|Terms|Operators|unary}} operator.
* <vp>-</vp> and <vp>+</vp> are both {{lang2|Terms|Operators|binary}} and {{lang2|Terms|Operators|unary}} operators.
* all other operators are {{lang2|Terms|Operators|binary}}.


<vp>div</vp>, <vp>mod</vp>, <vp>quot</vp> and <vp>rem</vp> are reserved words.
<vp>div</vp>, <vp>mod</vp>, <vp>quot</vp> and <vp>rem</vp> are reserved words.
Line 146: Line 154:
   <CharacterLiteral>
   <CharacterLiteral>
   <StringLiteral>
   <StringLiteral>
   <BinaryLiteral>
   <BinaryLiteral></vipbnf>
  <ListLiteral>
  <CompoundDomainLiteral></vipbnf>


==== Integral Literals ====
==== Integral Literals ====
Line 182: Line 188:


An integral literal can belong to {{lang2|Built-in_entities|integer|integer}} or {{lang2|Built-in_entities|unsigned|unsigned}} domains and it should not exceed maximum and minimum {{lang2|Built-in_entities|integer|integer}} or {{lang2|Built-in_entities|unsigned|unsigned}} values.
An integral literal can belong to {{lang2|Built-in_entities|integer|integer}} or {{lang2|Built-in_entities|unsigned|unsigned}} domains and it should not exceed maximum and minimum {{lang2|Built-in_entities|integer|integer}} or {{lang2|Built-in_entities|unsigned|unsigned}} values.
{{Example|Here are some examples of integral literals
<vip>
100
-100
0o77 % Octal
-0o17
0xFF % Hexadecimal
-0x5A
</vip>
}}


==== Real Literal ====
==== Real Literal ====
Line 200: Line 219:
   - +</vipbnf>
   - +</vipbnf>


A floating literal should not exceed maximum and minimum real values.
A real literal should not exceed maximum and minimum real values. Notice that any integral literal can also be used as a real value.
 
{{Example|Some real literals
<vip>
0.5
.5
1.23e-12
-10.25
</vip>
}}


==== Character Literals ====
==== Character Literals ====
Line 216: Line 244:
*<vp>\"</vp> representing double quote
*<vp>\"</vp> representing double quote
*<vp>\u</vp><vpbnf><XXXX></vpbnf>, here <vpbnf><XXXX></vpbnf> should be exactly four <vpbnf><HexadecimalDigit></vpbnf>'s representing the Unicode character corresponding to the digits.
*<vp>\u</vp><vpbnf><XXXX></vpbnf>, here <vpbnf><XXXX></vpbnf> should be exactly four <vpbnf><HexadecimalDigit></vpbnf>'s representing the Unicode character corresponding to the digits.
{{Example|Some character literals
<vip>
'a'
'\t' % TAB character
</vip>
}}


==== String Literals ====
==== String Literals ====
Line 223: Line 258:


<vipbnf><StringLiteralPart>:
<vipbnf><StringLiteralPart>:
   @" <AnyCharacter>-list-opt "
   ' <CharacterValue>-list-opt '
   " <CharacterValue>-list-opt "</vipbnf>
   " <CharacterValue>-list-opt "
  @<AtOpenChar> <AnyCharacter>-list-opt <AtCloseChar></vipbnf>


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


<vpbnf><StringLiteralPart></vpbnf>'s starting with <vp>@</vp> does not use escape sequences, except for the:
The first two forms (the ' and " forms) uses escape sequences to express certain characters
* <vp>""</vp> representing double quote.


Whereas <vpbnf><StringLiteralPart></vpbnf>'s without <vp>@</vp> uses the following escape sequences:
*<vp>\\</vp> representing <vp>\</vp>
*<vp>\\</vp> representing <vp>\</vp>
*<vp>\t</vp> representing Tab character
*<vp>\t</vp> representing Tab character
Line 237: Line 271:
*<vp>\r</vp> representing carriage return
*<vp>\r</vp> representing carriage return
*<vp>\</vp><vp>"</vp> representing double quote
*<vp>\</vp><vp>"</vp> representing double quote
*<vp>\</vp><vp>'</vp> representing single quote
*<vp>\u</vp><vpbnf><XXXX></vpbnf>, here <vpbnf><XXXX></vpbnf> should be exactly four <vpbnf><HexadecimalDigit></vpbnf>'s representing the Unicode character corresponding to the digits.
*<vp>\u</vp><vpbnf><XXXX></vpbnf>, here <vpbnf><XXXX></vpbnf> should be exactly four <vpbnf><HexadecimalDigit></vpbnf>'s representing the Unicode character corresponding to the digits.
In single quoted strings it is optional to escape double quotes, and likewise it is optional to escape single quotes in double quoted strings.
Single quoted strings must contain at least two characters otherwise they will be assumed to be a character literal.
{{Example|Some string literals
<vip>
"string\nnext line"
'string'
</vip>
}}
The @-literals can be used to avoid obscuring the string literals with escape characters.  The literals starts with @ followed by some non-letter  character <vpbnf><AtOpenChar></vpbnf>.  And it terminates when the close character <vpbnf><AtCloseChar></vpbnf> is met.  For most characters the close character is the same as the opening character, but for diverse paranthesis charactes the close character is the corresponding opposite paranthesis.
{|border="2" cellspacing="0" cellpadding="5" rules="all" style="border:solid 1px #AAAAAA; border-collapse:collapse; empty-cells:show; text-align: center;"
|-
| Open
| Close
| Open
| Close
|-
| @(
| )
| @)
| (
|-
| @[
| ]
| @]
| [
|-
| @{
| }
| @}
| {
|-
| @<
| >
| @>
| <
|}
For all non-paranthesis opening character the close character is the same as the open character, for example @" is closed by ".
For all @-strings it is the case the twice the closing character does not close the string, but means one occurence of the closing character in the string.
{{Example|
This example uses @[ as opening and ] as closing, inside the string literal both " and ' can be used unescaped:
[[Image:AtString.png]]
}}


==== Binary Literals ====
==== Binary Literals ====
Line 248: Line 333:
<vpbnf><ElementValue></vpbnf> should be any integral arithmetic expression (for example, constant), which should be calculated while compilation-time and be in the range from <vp>0</vp> till <vp>255.</vp>
<vpbnf><ElementValue></vpbnf> should be any integral arithmetic expression (for example, constant), which should be calculated while compilation-time and be in the range from <vp>0</vp> till <vp>255.</vp>


==== List Literals ====
{{Example|A binary literal
 
All elements in a list literal must belong to the same domain (or to compatible domains). This domain can be any {{lang2|Built-in_entities|Domains|built-in}} or {{lang|Domains|user defined domain}}, for example, it can be {{lang2|Lexical_Elements|Integral_Literals|integral}}, {{lang2|Lexical_Elements|Character_Literals|character}}, {{lang2|Lexical_Elements|Binary_Literals|binary}}, {{lang2|Lexical_Elements|Compound_Domain_Literals|compound domain}}, etc.
 
<vipbnf><ListLiteral>:
  [ <SimpleLiteral>-comma-sep-list-opt ]
  [ <SimpleLiteral>-comma-sep-list | <ListLiteral> ]</vipbnf>
 
Here <vpbnf><SimpleLiteral></vpbnf> can be:
 
<vipbnf><SimpleLiteral>:
  <IntegerLiteral>
  <RealLiteral>
  <CharacterLiteral>
  <StringLiteral>
  <BinaryLiteral>
  <ListLiteral>
  <CompoundDomainLiteral></vipbnf>
 
{{Example| These are all valid list literals:
<vip>
<vip>
[]                    % empty list
$[12, 34, 12, 0x22]
[1,2,3]               % list of integers
</vip>
["abc", "defg"]      % list of strings</vip>
}}
}}
{{Example| This is invalid because the elements in a list must all have same type:
<vip>
[1,"abc"]            % this is INVALID list</vip>
}}
==== Compound Domain Literals ====
Terms of {{lang2|Domains|Compound_Domains|user defined compound}} domains can be treated as literals if all their arguments are literals.

Latest revision as of 16:02, 26 February 2019

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 */ Comment 1 is not terminated (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
   implement inherits interface
   monitor
   namespace
   open
   predicates
   properties
   resolve
   supports
   where
MinorKeyword : one of
   align and anyflow as 
   bitsize
   catch
   determ digits div do
   else elseif  erroneous externally
   failure finally foreach from
   guard
   if in 
   language
   mod multi
   nondeterm
   otherwise or orelse
   procedure
   quot
   rem
   single
   then to try

All keywords except as and language are reserved words.

end is always combined with another key word:

end class
end implement
end interface
end if
end foreach
end try

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
   +    -
   =    <    >    <>    ><    <=    >=    :=    ==
   ~~   ++    **    ^^    >>    <<

div, mod, quot and rem are reserved words.

Literals

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

Literal:
   IntegerLiteral
   RealLiteral
   CharacterLiteral
   StringLiteral
   BinaryLiteral

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.

Example Here are some examples of integral literals
100
-100
 
0o77 % Octal
-0o17
 
0xFF % Hexadecimal
-0x5A

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 real literal should not exceed maximum and minimum real values. Notice that any integral literal can also be used as a real value.

Example Some real literals
0.5
.5
1.23e-12
-10.25

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.
Example Some character literals
'a'
'\t' % TAB character

String Literals

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

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

The first two forms (the ' and " forms) uses escape sequences to express certain characters

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

In single quoted strings it is optional to escape double quotes, and likewise it is optional to escape single quotes in double quoted strings.

Single quoted strings must contain at least two characters otherwise they will be assumed to be a character literal.

Example Some string literals
"string\nnext line"
'string'

The @-literals can be used to avoid obscuring the string literals with escape characters. The literals starts with @ followed by some non-letter character AtOpenChar. And it terminates when the close character AtCloseChar is met. For most characters the close character is the same as the opening character, but for diverse paranthesis charactes the close character is the corresponding opposite paranthesis.

Open Close Open Close
@( ) @) (
@[ ] @] [
@{ } @} {
@< > @> <

For all non-paranthesis opening character the close character is the same as the open character, for example @" is closed by ".

For all @-strings it is the case the twice the closing character does not close the string, but means one occurence of the closing character in the string.

Example

This example uses @[ as opening and ] as closing, inside the string literal both " and ' can be used unescaped: AtString.png

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.

Example A binary literal
$[12, 34, 12, 0x22]