[comp.sys.transputer] Syntax of occam 2

mdp@lion.inmos.co.uk (Michael Poole) (08/03/90)

A few weeks ago there was a request on the net for a syntax of occam2
which includes a root symbol.

This reminded me that I had produced such a syntax for the 5th Occam
User Group meeting at Loughborough in 1986, before we started to have
published proceedings. The syntax I produced then was squeezed on to a
single page to prove an assertion that Iann Barron had made.

The syntax below reflects the features added to the language since that
date and has been expanded to 2 pages to make it more readable.
The meta-language is that defined in British Standard 6154, and should
be reasonably obvious to anyone not already familiar with it.
The syntax is derived from the published versions in the occam2
reference manual and in Pountain and May, and reflects the language
accepted by current INMOS product occam compilers and configurers.

I should also like to commend readers to the description of the syntax
in Chapter 15 of "Programming in Occam 2" by Jones and Goldsmith
(Prentice Hall 1988); this description gives a concise and precise
statement of the semantic qualifications to the syntax.

I am not a syntax theorist and would welcome any comments from such
people on this syntax. I believe the most important feature of my
formulation is the inclusion of the terminal symbols i,o and n
which an appropriately designed lexer could create from the
indentation structure of the source text. Especial care is required
to handle properly the indentation within anonymous functions (VALOFs)
which are in the language but not in current implementations.

Any bugs found or other comments on this syntax would be welcomed.

Michael Poole
Manager of the Occam Development Programme
INMOS, Bristol.




----------------------------------------------------------------------------
                    SYNTAX OF OCCAM2 [BS 6154]

     (* process *)
     P = W ("SKIP"|
            "STOP"|
            E {"," E} ":=" Y|                        (* assignment *)
            E "!" X["::" X]{";" X["::" X]}|          (* output *)
            I|                                       (* input *)
            N "(" [(E|X) {"," (E|X)}] ")"|           (* proc instance *)
            C|                                       (* conditional *)
            "CASE" X {i (X {"," X}|"ELSE") i P o o}| (* case *)
            A|                                       (* alternation *)
            "WHILE" X i P o|                         (* while loop *)
            (["PRI"] "PAR"|"SEQ")(R i P o|{i P o})  (*parallel or sequence*)
           );

     (* input *)
     I = E "?" (J{";" J}|
                "AFTER" X|
                "CASE" ({i W N {";" J} i P o o}|
                             N {";" J})
               );

     (* input item *)
     J = E["::" E];

     (* conditional *)
     C = "IF" (R i W (X i P o|C) o|
                {i W (X i P o|C) o});

     (* alternation *)
     A = ["PRI"] "ALT" (R i W (G i P o|A) o|
                         {i W (G i P o|A) o});

     (* guard *)
     G = (X "&" ("SKIP"|I))|I;

     (* replicator *)
     R = N "=" X "FOR" X;

     (* primitive type *)
     T = "BOOL"|"BYTE"|
         "INT"|"INT16"|"INT32"|"INT64"|
         "REAL32"|"REAL64";
         "CHAN" "OF" (N|Q|"ANY")|
         "PORT" "OF" K|
         "TIMER";

     (* type *)
     K = {"[" X "]"} T;

     (* specifier *)
     S = {"[" [X] "]"} T;

     (* formal *)
     F = ["VAL"] S N{"," N};

     (* simple protocol *)
     Q = K | T ["::" "[]" T];


     (* specification *)
     D = K N{"," N}|
         "VAL" [S] N "IS" X|
         "VAL" S N "RETYPES" X|
         [S] N "IS" E|
         S N |"RETYPES" E|
         T {"," T} "FUNCTION" N "(" F {"," F} ")" ("IS" E {"," E}|i V o)|
         "PROC" N "(" F {"," F} ")" i P o|
         "PROTOCOL" N ("IS" Q {";" Q}|
                       i "CASE" {i N {";" Q} o} o)
         "PLACE" N ("AT" X|"IN" "WORKSPACE"|"IN" "VECSPACE");

     (* specification sequence *)
     W = {D ":" n};

     (* expression *)
     X = ("MOSTPOS"|"MOSTNEG")T|
         (T ["ROUND"|"TRUNC"]|"MINUS"|"-"|"~"|"NOT"|"SIZE") O|
         O ("+"|"*"|"/\"|"\/"|"><"|"PLUS"|"MINUS"|"TIMES"|
            "-"|"/"|"\"|"="|">"|"<"|"<>"|"<="|">="|
            "AFTER"|"<<"|">>") O |
         O {("AND"|"OR") O};

     (* expression list *)
     Y = X {"," X}|                             (* explicit list *)
         "(" V n ")"|                           (* results of valof *)
         N "(" X {"," X} ")";                   (* results of function *)

     (* table *)
     L = ("[" X {"," X} "]"|"[" E "FROM" X "FOR" X "]"|N|Z) {"[" X "]"};

     (* element *)
     E = ("[" E "FROM" X "FOR" X "]"|N|Z) {"[" X "]"};

     (* operand *)
     O = "(" (X|V n) ")"|            (* parenthesised expression or valof *)
         E|                          (* element *)
         L|                          (* table *)
         "TRUE"|
         "FALSE"|
         "'" H "'"|                        (* quoted character constant *)
         ["#"] U ["(" T ")"]|                       (* integer constant *)
         U "." U ["E" ("+"|"-") U]"(" T ")"|        (* real constant *)
         N "(" [X {"," X}] ")";                     (* function call *)

     (* valof *)
     V = W "VALOF" i P n "RESULT" Y o;

     (* program - ROOT SYMBOL OF SYNTAX*)
     M = W|P|"PLACED" "PAR" (R "PROCESSOR" X i P o|
                            {"PROCESSOR" X i P o} );

     (* N is a name       Z is a string
        H is a character  U is a digit sequence (with Hex digits if required)
        i is an indented new line (treat specially in valof)
        o is an outdented new line (treat specially after valof)
        n (n = o i;) is a new line at the same indent level *)

------------------- END OF SYNTAX OF OCCAM2 ---------------------------