B. Syntax Summary

1. Lexical units

The lexical units of Refal are divided into special signs, symbols, and variables. Blanks in any number, as well as line transfers, may appear between lexical units. A blank becomes a lexical unit when it appears inside a quoted string.

1.1 Special signs are:


There are also special system keywords: $ENTRY and $EXTERNAL (the latter can also be spelled $EXTERN or $EXTRN ). System keywords must be in capital letters.

1.2 Symbols are:



1.2.1 An identifier is a string of alphanumeric characters beginning with a capital letter. It must not be longer than 15 characters. Dash and underscore are also allowed in identifiers; they are equivalent. Upper and lower case letters inside identifiers are also equivalent.

1.2.2 Macrodigits are whole non-negative numbers. They are represented by strings of decimal digits. The value of the largest macrodigit is 232 - 1.

1.2.3 Real numbers, when unsigned, must start and end with a digit and include a period, a letter E , or both. The exact syntax of real numbers can be defined by the following BNF form (here and below, the alternatives are put in different lines):
real-number ::= unsigned-real 
                - unsigned-real 
                + unsigned-real 
unsigned-real ::= digits . digits 
                  digits . digits E digits 
                  digits E digits 
digits ::= decimal-digit 
           decimal-digit digits 


1.2.4 Characters are put in single or double quotes. A string of characters is enclosed in quotes as a whole; thus 'ab c' is a sequence of four character-symbols (the third symbol is a blank). A string of characters cannot be transfered between lines. A quote inside a string which is limited by the same kind of quotes is represented by a double quote. The following two strings:
  "Jimmy's Pizza" 
  'Jimmy''s Pizza'
represent the same Refal object. To avoid spurious quotes, separate quoted strings by blanks when one follows immediately after another. The size of a string is limited to 255 characters, so break up long strings.

1.3 A variable is a type indicator followed by a dot followed by an index. Type indicators are: They must always be in the lower case. An index is an identifier or a number. If it is a one-letter identifier or one-digit number, the dot can be omitted. If the dot is not omitted, the identifier can start with a lower-case letter. eX , e.X and e.x all stand for the same Refal object. t12 and eTree are not valid variables; a dot is required: t.12 , e.Tree .

1.4 Blanks, tabs and new-lines (known collectively as white space) are ignored by the Refal compiler when they appear between lexical elements. They often serve as separators -- preventing lexical units from merging into something else, as in the case of a sequence of identifiers or numbers. At the same time, lexical elements can follow each other without intervening white space as long as this does not interfere with lexical analysis. For instance, s1s2s3 is perfectly legitimate and equivalent to s1 s2 s3 , because exactly one character is expected after a type indicator if it is not immediately followed by a dot. However, s.1s.2s.3 is a syntax error.

2. Expressions

The syntax of Refal expressions in the BNF form is:

expression ::= empty 
               term expression 

term ::= symbol 
         variable  
         (expression)
         <f-name expression>

f-name ::= identifier 
empty ::= 

A pattern expression, or pattern, is an expression which includes no activation brackets. A ground expression is an expression which includes no variables.

3. Sentences and programs

A Refal program is a list of function definitions (f-definition's -- see below) and external function declarations (external-decl). Semicolons must be used to separate an external declaration from the following function definition; they may also separate function definitions.

program ::= f-definition 
	    f-definition  program 
	    f-definition ;  program 
            external-decl ;  program 
            program  external-decl ;

f-definition ::= f-name {  block }
		 $ENTRY  f-name {  block }

external-decl ::= $EXTERNAL  f-name-list 
                  $EXTERN  f-name-list 
                  $EXTRN  f-name-list 

f-name-list ::= f-name 
                f-name , f-name-list 

block ::= sentence 
          sentence ;
          sentence ;  block 

sentence ::= left-side  conditions  =   right-side 
	     left-side  conditions  ,  block-ending 

left-side ::= pattern 

conditions ::= empty 
               , arg : pattern conditions 

arg ::= expression 

right-side ::= expression 

block-ending ::= arg : { block }

4. Comments

Comments can be inserted into a program as defined above. They are ignored by the Refal compiler. There are two kinds of comments:

In line comments we usually refer to the following sentence(s); slashed comments refer to the preceding text.