Back
PDF Version (113 KB)
Precedence
osurs@bluewin.ch Urs Oswald http://www.ursoswald.ch September 23, 2002
Why Are the Mediation Points Where They Are?
For MetaPost, `2/5' is a
numeric expression
. As a consequence, `2/5[P0, P1]' is equivalent to
`(2/5)[P0, P1]'. Furthermore, the figure above reveals that MetaPost reads `-2/5[P0, P1]' as
`-((2/5)[P0, P1])'. Therefore, `-2/5[P0, P1]' is equivalent to `2/5[P0, P1] rotated 180'. And
`-c[P0, P1]' is, for MetaPost, equivalent to '-(c[P0, P1])', which in turn is equivalent to
`c[P0, P1] rotated 180'.
Breaking Down the Input Into Tokens
An Experimental ApproachIn order to find out how MetaPost breaks down an input sequence like
In the case of the above input sequence, you will find that it is broken down into the tokens .
Three Kinds of TokensMetaPost knows three kinds of tokens:
What is or is not a numeric token is defined by the following set of syntactic rules:
In order to prove that `1.4142' is a numeric token , one first establishes, by rule (A), that each of `1',`4',`2' is a decimal digit . So by the first part of rule (B), `1' and `4' are also of expression type digit string . Using the second part of (B) thrice in a row, one proves that `4142' is a digit string . Finally, by the third part of rule (C) it follows that `1.4142' is a numeric token . The notation used here and afterwards to formulate syntactic rules for expressions were introduced about 1960 by John Backus and Peter Naur. Rule (A) determines that a decimal digit is either of 0,1,2,3,4,5,6,7,8,9, and nothing else. Similarly, rule (C) determines that an expression is a numeric token iff (if and only if) it is of one of the three following expression types: ` digit string ', `. digit string ', ` digit string . digit string '. How can one prove that `.4.1' is not a numeric token ? One has to prove that it is neither a digit string , nor of one of the two forms `. digit string ' or ` digit string . digit string '. As a digit string consists of decimal digits only, neither of `.4.1', `4.1', `.4' is a digit string . So `.4.1' has none of the three possible forms of a numeric token . For MetaPost, the input sequences `sqrt3a' and `sqrt 3a' are equivalent, as both are broken down into the tokens
,
and so are the input sequences `www.latex2html.org' and `www latex 2html org', both of which result
in the sequence
.
And both of `sind30cosd60+sind60cosd30' and `sind 30 cosd 60 + sind 60 cosd 30' yield the token
sequence
.
Numeric Expressions
The Complete Set of Syntactic RulesThe following diagram is taken from KNUTH[2], p. 211. It contains the complete and final set of syntactic rules for numeric expressions.
Some ConsequencesThe above set of rules implies:
By lines 8 and 9, a numeric token atom is either a numeric token or a numeric token not followed by `/ numeric token ' . So a numeric token atom is a numeric token or has one of the 9 forms
The figure below gives an overview of the subtypes of numeric expression . The classification of expressions is not absolute, but it depends on the context, as the following example shows. In the expression `.61803[2, 5]', the sequence `.61803' is a numeric token atom ; in the expression `.61803/3.14[2, 5]' it is not.
Two ExamplesEach one of the input sequences `sind4a', `sind 4 a', and `sind4.a' will result in the token sequence
.
(The `d' in `sind' refers to degree, as `sind' expects the argument to be measured in
degrees.) Let `a' be numeric.
By line 27 of the set of syntactic rules for numeric expressions, the first token,
,
is a
numeric operator
. The second token is a
numeric token atom not followed by + or - or a numeric token
.
It is therefore, by line 30, a
scalar multiplication operator
,
and, by line 28, a
numeric operator
. The third token is a
numeric variable
which
is a
numeric atom
by line 1 and a
numeric primary
by line 10. Therefore, the token
sequence has the form
numeric operator
numeric operator
numeric primary
.
The only way of reducing this sequence is by line 22:
numeric operator
numeric primary
yields
numeric primary
, which means that `sind4a' is equivalent with `sind(4a)'. Again by
use of line 22, we can finally reduce the sequence to
numeric primary
.
The input sequence `sind4a' (or, equivalently, `sind 4 a' or `sind 4a') has the form
sind
numeric token atom
numeric variable
,
and can therefore be converted by the syntax rules into
It may seem arbitrary that this time, we do not classify the token as a numeric token atom not followed by + or - or a numeric token , as we did in the previous example. Had we done so, we would have ended up with
numeric operator
numeric operator
times or over
numeric primary
,
a sequence which cannot be proven to be a
numeric expression
by the syntax rules.
While each numeric primary is also a numeric secondary , the reverse is not true. For the numeric secondary `2*3.1415', we can prove that it is not a numeric primary by proving that it is of neither of the possible expression types given in lines 10 through 23: It is neither an isolated numeric atom (10) nor a mediation expression (11), and interpreting the leading digit `2' as a numeric operator leads nowhere. It neither starts with a pair part nor a transform part , and it does not begin with any of the remaining operators length, ASCII, oct, hex, angle, turningnumber, totalweight, directiontime ...of.
Syntax of Pair Expressions
The complete set of syntactic rulesThe following diagram is taken from Donald E. Knuth, The METAFONTbook, p. 212:
Mediation PointsNow let's go back to the mediation expressions `2/5[P0, P1]' and `-2/5[P0, P1]'. (We suppose that a preliminary declaration pair P[] has been made.) The first is of the form
numeric token atom
[
pair variable
,
pair variable
].
So we can establish each of the forms
scalar multiplication operator
pair primary
and finally, by line 46, to
pair primary
.
What about `a/b[P0, P1]'? This sequence is of the form
numeric variable
/
numeric variable
[
pair expression
,
pair expression
]
and therefore
numeric atom
/
numeric atom
[
pair expression
,
pair expression
]
by line 1 of the syntax rules. There are no rules for reducing
numeric atom
/
numeric atom
.
So MetaPost tries by applying line 45 to
numeric atom
[
pair expression
,
pair expression
],
reducing this expression to
pair primary
. But then it gets stuck with
numeric atom
/
pair primary
,
as it cannot divide a
numeric atom
by a
pair primary
. The error message is:
! Not implemented: (known numeric)/(pair).
Bibliography
|