Chapter 4 |
The revised syntax |
|
The revised syntax is an alternative syntax for OCaml. Its purposes
are 1/ fix some problems of the normal syntax (unclosed constructions
sometimes introducing ambiguities, constructors arity, end of top
level phrases and structure items, etc) 2/ avoid unjustified double
constructions (":="
vs ``<-
'', ``fun'' vs ``function'',
``begin..end'' vs parentheses) or concepts (types and types
declarations) 3/ bring some ideas (lists, types). In a word, propose a
syntax which be more logical, simpler, more consistent and easier to
parse and to pretty print.
The revised syntax, being few used, is less constrained by the
history than the normal one, and can try to answer the question: ``how
things should be done'' instead of ``how to remain compatible with old
versions''.
Other motivations are: 1/ show that syntax is just a ``shell'' of the
language: you can change it without modifying the background 2/
experiment right to the end the ability of Camlp4 of doing syntax
extensions.
It is a syntax of the complete language, therefore it can be used for
all OCaml programs: by the way, Camlp4 is itself completely written in
that syntax. Notice that it is not a constraint: it is always possible
to convert from and to the normal syntax, using the pretty print
facilities of Camlp4.
Remark: syntax in programming languages is much a question of personal
taste. This syntax represents mine, with some ideas taken here and
there. Some choices may seem arbitrary (other solutions are possible),
but I tried to keep some consistency, and without being too far from
the normal syntax: I guess that it is possible to understand a program
written in revised syntax even without having read this chapter.
Most of the constructions in revised syntax are therefore the same
than in the normal syntax. This chapter presents only the differences,
and the motivations of them.
The quotations for OCaml syntax trees, which we shall see in next
chapter, use the revised syntax.
To compile the file foo.ml
written in revised syntax, use:
$ ocamlc -pp camlp4r foo.ml
To use the revised syntax in the toplevel, do:
$ ocaml
#load "camlp4r.cma";;
- In revised syntax, simple semicolons end the items of structures,
signatures and objects. These semicolons are mandatory. The
double semicolon is no more a token. There is no ambiguity with the
sequence, which has a special construction (see further).
- The declaration of a global variable is introduced by the keywo