A Comparative Study of Formalisms for Programming Language
Definition.
A thesis presented in partial fulfilment of tho requirsments for the degree of Master. of Science in
Computer Science at Massey University.
Ian Joseph Thompson
ii
Abstract
This sludy looks Rt a number of methods for defining the f u 11 syntax and s efl12.n ti cs of computer pro gr arnrni n g languages. The syntax, especially the nature of context-dependent conditions in it, is first oxamined, then some oxtensions of context-free grammars are compared to see to what cxt8nt they can encompass the full contGxt
-conditions of typicc1l programminy lnnguages. It is found that several syntax extensions are i11adequate in this r egard, and that tho ability to cnlculoto complicated functions and conditions , and to eventually delete the val ues of such functions, is needed. This ability may be obtained either by allowing unrestricted rules and rncta -vari ablos in the phrase- structure, or by associating roathematical functionc either with individual production
r ules or with the whole contoxt--froe structure, to trL,ns -f orm it into an ' c.ibstrocl syntax ' .
Since the forrn of o definition of a progr2.r,1rnir19 J.,rngt1ar;e semantics de pen cJ s critical .l y on how one conceives II n: can i 11 y 11 , f ive mo.in tyf)es of semantics are considered: these are
I\ c know led genre n ts
In the preparation of this thesis, I wish
particularly to thank Lloyd Thomas and
Professor Tate for many valuable discussions,
suggestions 1 and criticisms, and also to
thank Bob Doran for help in his obtaining
i mportant source materials.
Table of Contents
Acknowledgem8nts
1 The probl ems of programming l anguage definition.
2 Syntax
2 .1 Introduction
2.2 Context-dependent conditions 2.3 Extended grammars
2.3.1 Context-sensitive grammars
2. 3.2 Scattered-context grammars 2.3.3 Programmed grammars
2.4 Grammars with associated c ontext-dependent conditions
2.4 .1 Property grammars
2.4. 2 Grammars using inherited and derived
iii
1
3
3
4
7
7
9
12
14
14
attributes, Production Systems 17
2.4.3 Conditions defined by functions on the
whole context-free syntactic structure 20 2.4.3.1 The Vienna Definition method 21
2.4.3.1 Objects and selectors and·
operations in VDL 21
2.4.3.2 Constructing the abstract syntax
in VOL 24
2.4.3.2 Linked-Forest Manipulation Sys tems 26
2.5 van Wijngaarden grammars 29
2. 5.2.1 An historical digression
2.5. 2.2 Context-dependencies defined by
van Llijngaarden grammars
2.6 Conclusion
3 Semantics
3.1 Introduction
3. 2 ~atural semantics
3.2.1 Natural l anguage semantics
30 31 36 36 37 37 3.2.2 Semantics by compilers 39
3. 3 P,r oposi tional semantics 40
3. 4 .r unctional semantics 44
3. 4. 1 The l ambda cal culus 45
3 • 5 S e f! i an t i c s b as o d on s t r i n g r e w r i t i n g r u l o s 4 8
3. 5.1 Preprocessing semantics 48
3.5. 2 Data-processing semantics 51
3.6 Semantics with structures and computations
on structures 54
3 • 6 • 1 I n f o r rn :q t.i. o 11 s t r u c t u 1· e s 3.'1.2 Sornentic 01etaJ.angu2ges
3.6.2.1 The Vienna Definition Language
3. G. 2.2 The
OAS
I
S
/1
definition ofP
L/
I
f O r [ [ U\/ /'. r~ S J
3. G. 2. 3 The Algal 60 method
3 • G • 2 • I~ L i n l<G d- ~-or e s t r:i an i p u l a t i on S y s t ems as semanti c met al anguoges
3. 6. 3 Hypothetical machines
3.G.3.1 Machine structures
3.7 Conclusion
References
Bibliography
Appendices
A Concrete syntax of
ALEX:
a context-free54 57 57 59 60 62
, 6G (;7
71
73
79
83
covering grammar 83
8 Abstract represent ation of .the concrete
syntax in
VDL
85C Abstract syntax for the
VDL
definition ofA
LEX
88E Definition of ALEX syntax by a Production
System
F Definition of ALEX syntax by ~ van Llijngaarden grammar
G Definition of ALEX syntax by a Linked-Forest
Manipulation System
H f'iachine states for the VDL definition of
ALEX semantics
I Interpretation instructions for tho VOL
94
97
1 02
106
definition 107
J Abstract syntax for the BAS IS definition of
ALEX 112
K Machine states for the BASIS definition 113 L Interpretation program for the BASIS dofinition 114
M Abstract syntax tree-structures for the LFMS
definition of ALEX 120
1 The problems of proaramming lanquage definition
Defining a computing language is generally done in
two stnges :
1) ~ntax : defining as rigorously as possible the set
of all possible programs of the language,
together with their formal structures and
substructures.
1
2)
semantics : associating wi th each such program itsmeaning, so that the effects of executing the progr am with its data are as rigo rous-ly defined as possible.
Details of definitions of these two stages will
be discussed in the following two chapters (although
t he exact dem2rcation between the stages has varied for different people; I shall discuss t his further
in section
2.2)
.
The aim is to be able to define a significantly l ar ge language,including both st ages of definition,
8nd to this end there are several criteria for comparing the different systems examined l ater.
1) Scope of t he definition method
Is it applicable to all features of all progr amming
languages, or are there some features that can be
encompassed either not at all, only with great
difficulty, or at the cost of breaking up a neat system?
2) Elegance
A general aim is for a definition as readable , concise,
and 'trahsparent' as possible. A readable definitJon
should be understandable even with only a short initiation into the details of the formalism ; it should not be written in a wholly foreign language. A definition should al.so ·transparentl y follow the
language being defined; this means that small changes in
disti nguish between the method and its appl ication
to specific l anguages: a very simpl e method wil l
general ly lead t o a very compl i cated definition.
3 ) Rioour
Syntax definitions should dofine, ideall y, al l
and only the proqr;-ims in the .lant_;uage, and assign
correct formal structures to val id programs. They
should avoid ovorlapping, incompatible, ambj guous,
and/or missing specifications. Si mi larly wit h
semantics. Note, however, t hat i t i s occasionaJ Jy
desi reab le to leavo certain parts of~ standar d
2
de fin j_ t .i on either corn p let e 1 y op on , or to de J. i berat e 1 y
give only a pnrt i ol definition of t hem. r or example,
the de ta .i l [; of re c1 l a r i t h m Ei ti c, beyond certain
basic conditions, may be postponed beyond the
standard defi nition; and i n any case the effect of
merging p;:,ralJ.eJ. operations should i ntentionally be
left undetermined.
4 ) Fur rn a J. is i.' t i on
The for111al jsts ' idec:il is th2t a ciefinition shouJ d
S8Y everything thnt can be sc1id about c1Jl programs
i n the lc111guc1ge, a11d i n such a manner that mechan
-i cal ~;b:1ternents r.,rn be rnarle 8hOut the proqri:lm wj thout
either us.inq hum,:,n understand.ing at this point, or
rL1nnin9 i t on a computer with specif ic data. Such
statements, for exnmple, could concern the mechan
-ioal design of impJ.oment~tjons , or the mechanical
proofs of correctness , equival ence, etc., of
programs i n t he language.
Chapter 2 looks at lhe synlactic; and chapter 3 the
semant i c, components of definitions of progr amming l anguages ,
and in the appendi ces I have used those methods which ar e
sufficiently powerful for the definition of "ALEX". ALEX i s
the name which hencefort h I give to a certai n subset of Algal
60; i t does not include arrays ,. for-loops , conditional
expressions , or designational expressi ons , but i t does
incl ude mi~ed- mode arithmetic, procedures , functions,
call-by-name and call-by-value parameters, 11goto11 and 11if11