<<

COSC 4400 – CONSTRUCTION SPRING 2017

EDSGER - DIJKSTRA'S GUARDED COMMANDS

This document is a rough description of a simple called EDSGER which is based on the Guarded Commands notation. Guarded Commands was developed by the scientist Edsger W. Dijkstra for use in program derivation and proof. We will be implementing interpreters and for EDSGER this semester.

In our implementation we will various simplifying assumptions. The types are integer, boolean, and arrays of integers or booleans. We will have user-defined functions (with multiple return values). Function definitions cannot be nested. Scope in functions is local then global (anything not local to a function). Variable names are a letter followed by an alphanumeric string. Numeric constants are decimal integers (unsigned).

The language has a number of unusual instructions, the semantics of which will be discussed in class. There is no provision for reading user input into a program.

Here are a couple sample programs:

{ compute the greatest common divisor of two numbers - written by mike slattery, feb 2000 }

{ Note comments are enclosed in braces {and can be nested} }

[ x:int, y:int ] { global variables } x, y <- 4, 6 { multiple assignment } display x,y { print out values } do x>y -> x <- x-y [] y>x -> y <- y-x od display x { recursive Fibonacci sequence }

[ n:int, i:int ] fun f:int << fib << n:int] {function declaration}

[ :int ] {local variable}

if n < 1 -> f <- 0 [] (n > 0) and (n <= 2) -> f <- 1 [] n > 2 -> f <- fib< x <- fib<

Here is a rough draft of a grammar for the language (this may need to be revised as we move through the semester). To help with Assignment #4, I've listed the suggested tree class to use for each non- terminal (just above each production).

{ Program } program -> decl funs body EOF

{ DeclList } decl -> [ decl_list ] | ε

{ DeclList containing entries of type VarDecl } decl_list -> ID : type | decl_list , ID : type

{ All the following are subclasses of Type } type -> int | int ( NUM .. NUM ) array declaration | bool | bool ( NUM .. NUM )

{ FunList } funs -> funs fundef | ε { FunDef } fundef -> fun opt_vlist << ID << opt_vlist ] decl body nuf

{ DeclList } opt_vlist -> decl_list | ε

{ StmtList } body -> stmt | body stmt

{ CompoundStmt } stmt -> simple | simple ; stmt

{ All the following are subclasses of Stmt } simple -> destlist <- exprlist | if glist fi | if expr then stmt fi | do glist od | skip do nothing | display exprlist | funcall function with no return value

{ DestList containing entries of type Expr (Id or ArrayElt) } destlist -> ID | ID ( expr ) array element | destlist , ID | destlist , ID ( expr )

{ Guardlist } glist -> gcommand | glist [] gcommand

{ GCommand } gcommand -> expr -> stmt

{ ExprList } exprlist -> expr | exprlist , expr

{ All the following are subclasses of Expr } expr -> expr + expr | expr - expr | expr * expr | expr / expr integer divide | - expr | ( expr ) | expr = expr | expr <> expr not equal | expr < expr | expr <= expr | expr > expr | expr >= expr | not expr | expr and expr | expr or expr | true | false | NUMBER | ID | ID ( expr ) array element | funcall

{ FunCall } funcall -> ID << exprlist ]