Literals and Constants

A literal is a constant that represents a given value of certain data type.

LiteralValue ::= PrimitiveLiteral | StructuredLiteral

PrimitiveLiteral ::= intLiteral | realLiteral | stringLiteral | EnumLiteral

Literals of Primitive Data Types

The formats of literals of integer, real, bool and string types are given in Section 2.2.2.

A literal value of an enumerate data type is a pair of the enumerate type name and an enumerate value of the type separated by ":" .

EnumLiteral ::=TypeName ":" EnumValue

For example, Gender:Male is a literal of the enumerate type Gender.

Literals of Structured Data Types

StructuredLiteral ::=TypeName ":" StructuredLitValue

StructuredLitValue ::=RecordLiteral | ListLiteral

A literal of a structured data type starts with the type name, followed by a ":" and then the literal value. Literal values of structured data types are constructed from primitive literals.

Literal Values of Record Types

RecordLiteral ::=

"{" FieldName ":" FieldValue=Literal

{ "," FieldName ":" FieldValue=Literal } "}"

In a record literal value, the identifier as a FieldName must be a field name of the record data type. The data type of a literal values of a field must be the data type of the corresponding field.

The FieldName-LiteralValue pairs in a record type literal can occur in any order regardless of the order that fields occur in the record type declaration.

For example, the following are some valid literal values for the Person1 record type.

Person1: {name:"Hong Zhu"}

Person1: {name:"Hong Zhu", day:9, month:2, year:1961}

Person1: {name:"Hong Zhu", month:2, day:9}

Person1: {name:"Hong Zhu", year:1961}

A field name must not occur more than once in a literal. But, a field can be omitted from the list of FieldName-LiteralValue pairs. In this case, the field of the record is undefined, and it is equivalent to write fieldName:null.

For example, the following literal is equivalent to the last literal in the above example.

Person1: { name:"Hong Zhu", day:null, month:null, year:1961}

Note that, for a field x of string type, x = "" is different from x = null. In the former, the field x is defined and has a value of empty string; while in the latter, field x is not defined and has no value at all.

The following is an example of valid literal value of the Person2 record type, which contains a field whose type is a structured type.

Person2: {

name:"Hong Zhu",

DoB: Date:{

day:9,

month:2,

year:1961

}

}

Literals of List Types

ListLiteral ::=

"[" [ ElementValue=Literal { "," ElementValue=Literal } ] "]"

The sequence of element literal values can be of any length, including 0, which means the list is empty. The following are some examples of valid literal values of the IntList data type.

IntList: [] //the empty list of integers.

IntList: [0] //a list of 1 integer value.

IntList: [1, 2, 3, 4, 5] //a list of 5 integer values.

The following is an example of list type declarations that contain structured values as elements. Some examples of its literal values are also shown.

//type definition for a list of dates:

type DateList = List{Date}

//Example 1 of literal value – a list of 1 date:

DateList: [

Date:{day:9, month:2, year:1961}

]

//Example 2 of literal value – a list of 2 dates:

DateList: [

Date:{day:9, month:2, year:1961},

Date:{day:3, month:8, year:1959}

]

Constant Declarations

Each constant declaration defines an identifier to denote a constant value. Constant declarations are in the following forms.

ConstDef ::= ⟨CONST⟩ ConstName=Identifier "=" LiteralValue ";"

The type of the constant is the same as the literal value.

For example, the following declares an identifier that denotes a constant value of the Person4 data type.

const HongZhu = Person4: {

name:"Hong Zhu",

sex:"Male",

DoB: Date: {day:9, month:2, year:1961}

}