Difference between revisions of "Language Reference/Lexical Elements"
m (→Keywords: vipbnf) |
(Operators) |
||
(8 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 83: | Line 84: | ||
properties | properties | ||
resolve | resolve | ||
supports</vipbnf> | supports | ||
where | |||
</vipbnf> | |||
<vipbnf><MinorKeyword> : one of | <vipbnf><MinorKeyword> : one of | ||
Line 97: | Line 100: | ||
mod multi | mod multi | ||
nondeterm | nondeterm | ||
or orelse | 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 131: | Line 135: | ||
/ * div mod quot rem | / * div mod quot rem | ||
+ - | + - | ||
= < > <> >< <= >= := | = < > <> >< <= >= := == | ||
~~ ++ ** ^^ >> << | |||
</vipbnf> | </vipbnf> | ||
* <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 147: | Line 154: | ||
<CharacterLiteral> | <CharacterLiteral> | ||
<StringLiteral> | <StringLiteral> | ||
<BinaryLiteral | <BinaryLiteral></vipbnf> | ||
==== Integral Literals ==== | ==== Integral Literals ==== | ||
Line 183: | 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 201: | Line 219: | ||
- +</vipbnf> | - +</vipbnf> | ||
A | 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 217: | 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 ==== | ||
<vipbnf><StringLiteral>: | <vipbnf><StringLiteral>: | ||
Line 239: | 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>\</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. | ||
Line 245: | Line 277: | ||
Single quoted strings must contain at least two characters otherwise they will be assumed to be a character literal. | 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. | 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. | ||
Line 294: | 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> | ||
{{Example|A binary literal | |||
<vip> | <vip> | ||
[ | $[12, 34, 12, 0x22] | ||
</vip> | |||
}} | }} | ||
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 + - = < > <> >< <= >= := == ~~ ++ ** ^^ >> <<
- ~~ is a unary operator.
- - and + are both binary and unary operators.
- all other operators are binary.
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.
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.
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.
'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.
"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.
This example uses @[ as opening and ] as closing, inside the string literal both " and ' can be used unescaped:
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.
$[12, 34, 12, 0x22]