To illustrate my system, I shall present a specification based on the little language dealt with in Kamin's first chapter. The syntax is follows:
goal = {instruction | function_definition} function_definition = `function' identifier `(' identifier {`,' identifier} `)' {instruction} `end' instruction = `if' expression `then' {instruction} `else' {instruction} `end' | `while' expression `do' {instruction} `end' | `print' expression | `return' expression | identifier `:=' expression expression = simple_expression [operator simple_expression] simple_expression = integer | identifier | `(' expression `)' | identifier `(' {expression} `)' operator = `+' | `-' | `/' | `*' | `=' | `<' | `>' identifier = (Up_let | Low_let) {Up_let | Low_let | Digit | `_' } integer = Digit{Digit} Tokens = identifier | integer Ignorable = Eol | ` '
To add semantics to this language, one considers the abstract syntax tree (AST) as an expression to be evaluated, rather than as a structure to be translated into another language.
For example, the source program:
is seen as syntactic sugar for the expression:a := 1 a := a + 2 print 3 + a
which is, of course, simply the AST.[`:='(new_identifier(`a'), new_integer(`1')), `:='(new_identifier(`a'), `+'(new_identifier(`a'), new_integer(`2')), `print'(`+'(new_integer(`3'), new_identifier(`a')))]
To understand what will happen when the expression is evaluated, one must refer to the definitions of classes Integer and Identifier (and any classes referred to by their methods). These classes, which can be written in an ordinary Object-Oriented programming language, thus constitute the semantics of the language being implemented.
In practice, students are not concerned with the mechanics of the process which turns the BNF+Class definitions into a compiler or interpreter. (I.e. translating the AST into executable code, or interpreting it directly.)
Some of the code for classes Identifier and Integer is presented in figures 1 and 2, respectively. It is written in Slim[11], a language developed by the author and closely related to SETL[8].
[IMAGE ]
Figure 1: Partial class definition for Identifiers
[IMAGE ]
Figure 2: Partial class definition for Integers
Prof Herman Venter