Ada Programming/Lexical elements
Character set
editThe character set used in Ada programs is composed of:
- Upper-case letters: A, ..., Z and lower-case letters: a, ..., z.
- Digits: 0, ..., 9.
- Special characters.
Take into account that in Ada 95 the letter range includes accented characters and other letters used in Western Europe languages, those belonging to the ISO Latin-1 character set, as ç, ñ, ð, etc.
In Ada 2005 the character set has been extended to the full Unicode set, so the identifiers and comments can be written in almost any language in the world.
Ada is a case-insensitive language, i. e. the upper-case set is equivalent to the lower-case set except in character string literals and character literals.
Lexical elements
editIn Ada we can find the following lexical elements:
- Identifiers
- Numeric Literals
- Character Literals
- String Literals
- Delimiters
- Comments
- Reserved Words
Example:
Temperature_In_Room := 25; -- Temperature to be preserved in the room.
This line contains 5 lexical elements:
- The identifier
Temperature_In_Room
. - The compound delimiter
:=
. - The number
25
. - The single delimiter
;
. - The comment
-- Temperature to be preserved in the room.
.
Identifiers
editDefinition in BNF:
identifier ::= letter { [ underscore ] letter | digit } letter ::= A | ... | Z | a | ... | z digit ::= 0 | ... | 9 underscore ::= _
From this definition we must exclude the keywords that are reserved words in the language and cannot be used as identifiers.
Examples:
The following words are legal Ada identifiers:
Time_Of_Day TimeOfDay El_Niño_Forecast Façade counter ALARM
The following ones are NOT legal Ada identifiers:
_Time_Of_Day 2nd_turn Start_ Access Price_In_$ General__Alarm
Exercise: could you give the reason for not being legal for each one of them?
Numbers
editThe numeric literals are composed of the following characters:
- digits
0 .. 9
- the decimal separator
.
, - the exponentiation sign
e
orE
, - the negative sign
-
(in exponents only) and - the underscore
_
.
The underscore is used as separator for improving legibility for humans, but it is ignored by the compiler. You can separate numbers following any rationale, e.g. decimal integers in groups of three digits, or binary integers in groups of eight digits.
For example, the real number such as 98.4 can be represented as: 9.84E1
,
98.4e0
, 984.0e-1
or 0.984E+2
, but not as 984e-1
.
For integer numbers, for example 1900, it could be written as 1900
, 19E2
, 190e+1
or 1_900E+0
.
A numeric literal could also be expressed in a base different to 10, by enclosing the number between #
characters, and preceding it by the base, which can be a number between 2 and 16. For example, 2#101#
is 1012, that is 510; a hexadecimal number with exponent is 16#B#E2
, that is 11 × 16² = 2,816.
Note that there are no negative literals; e.g. -1 is not a literal, rather it is the literal 1 preceded by the unary minus operator.
Character literals
editTheir type is Standard.Character, Wide_Character or Wide_Wide_Character. They are delimited by an apostrophe (').
Examples:
'A' 'n' '%'
String literals
editString literals are of type Standard.String, Wide_String or Wide_Wide_String. They are delimited by the quotation mark (").
Example:
"This is a string literal"
Delimiters
editSingle delimiters are one of the following special characters:
& ' ( ) * + , - . / : ; < = >
Compound delimiters are composed of two special characters, and they are the following ones:
=> .. ** := /= >= <= << >> <>
You can see a full reference of the delimiters in Ada Programming/Delimiters.
Comments
editComments in Ada start with two consecutive hyphens (--
) and end in the end of line.
-- This is a comment in a full line My_Savings := My_Savings * 10.0; -- This is a comment in a line after a sentence My_Savings := My_Savings * -- This is a comment inserted inside a sentence 1_000_000.0;
A comment can appear where an end of line can be inserted.
Reserved words
editReserved words are equivalent in upper-case and lower-case letters, although the typical style is the one from the Reference Manual, that is to write them in all lower-case letters.
In Ada some keywords have a different meaning depending on context. You can refer to Ada Programming/Keywords and the following pages for each keyword.
Ada Keywords | ||||
---|---|---|---|---|
abort
|
else
|
new
|
return
|
|
abs
|
elsif
|
not
|
reverse
|
|
abstract (Ada 95)
|
end
|
null
|
||
accept
|
entry
|
select
|
||
access
|
exception
|
of
|
separate
|
|
aliased (Ada 95)
|
exit
|
or
|
some (Ada 2012)
|
|
all
|
others
|
subtype
|
||
and
|
for
|
out
|
synchronized (Ada 2005)
|
|
array
|
function
|
overriding (Ada 2005)
|
||
at
|
tagged (Ada 95)
|
|||
generic
|
package
|
task
|
||
begin
|
goto
|
parallel (Ada 2022)
|
terminate
|
|
body
|
pragma
|
then
|
||
if
|
private
|
type
|
||
case
|
in
|
procedure
|
||
constant
|
interface (Ada 2005)
|
protected (Ada 95)
|
until (Ada 95)
|
|
is
|
use
|
|||
declare
|
raise
|
|||
delay
|
limited
|
range
|
when
|
|
delta
|
loop
|
record
|
while
|
|
digits
|
rem
|
with
|
||
do
|
mod
|
renames
|
||
requeue (Ada 95)
|
xor
|