The basic structure of the language is
not a particularly original one.
Equations are pictured as a set of ``boxes,''
pieced together in various ways.
For example, something with a subscript is
just a box followed by another box moved downward
by an appropriate amount.
A fraction is just a box centered above another box,
with a line of correct length drawn between them.
The grammar for the language is shown below.
For purposes of exposition, we have collapsed
some productions. In the original grammar, there
are about 70 productions, but many of these
are simple ones used only to guarantee
that some keyword is recognized early enough in the parsing process.
symbols are non-terminals, i.e., syntactic categories.
The vertical bar \(bv indicates an alternative;
the brackets [ ] indicate optional material.
is a string of non-blank characters or
any string inside double quotes;
the other terminal symbols represent literal occurrences
of the corresponding keyword.
| box SUB box | box SUP box
| [ L | C | R ]PILE { list }
| LEFT text eqn [ RIGHT text ]
| box [ FROM box ] [ TO box ]
| [ROMAN | BOLD | ITALIC] box
| box [HAT | BAR | DOT | DOTDOT | TILDE]
list : eqn | list ABOVE eqn
The grammar makes it obvious why there are few exceptions.
For example, the observation that something can be replaced by a more complicated something
in braces is implicit in the productions:
Anywhere a single character could be used,
legal construction can be used.
Clearly, our grammar is highly ambiguous.
What, for instance, do we do with the input
To answer questions like this, the grammar
is supplemented with a small set of rules that describe the precedence
In particular, we specify (more or less arbitrarily)
so the first alternative above is the one chosen.
because this is closer to standard mathematical practice.
That is, we assume $x sup a sup b$ is $x sup {(a sup b )}$,
The precedence rules resolve the ambiguity in a construction like
to have a higher precedence than
so this construction is parsed as
$a sup 2 over b$ instead of $a sup {2 over b}$.
Naturally, a user can always
force a particular parsing
by placing braces around expressions.
The ambiguous grammar approach seems to be quite useful.
The grammar we use is small enough to be easily understood,
for it contains none of the productions that would be
normally used for resolving ambiguity.
Instead the supplemental information about
precedence and associativity (also small enough to be understood)
provides the compiler-compiler
with the information it needs
to make a fast, deterministic parser for
the specific language we want.
When the language is supplemented by the disambiguating rules,
and thus easy to parse[5].
The output code is generated as the input is scanned.
of the grammar is recognized,
For example, when the lexical analyzer
reports that it has found a
(i.e., a string of contiguous characters),
we have recognized the production:
The translation of this is simple.
We generate a local name for the string,
then hand the name and the string to
perform the storage management.
All we save is the name of the string,
its height, and its baseline.
the translation associated with the production
slightly more than largest input width
slightly more than sum of input heights
slightly more than height of bottom input box
String describing output box =
move right enough to center bottom box;
draw bottom box (i.e., copy string for bottom box);
move up; move left enough to center top box;
draw top box (i.e., copy string for top box);
move down and left; draw line full width;
return to proper base line.
Most of the other productions have
equally simple semantic actions.
Picturing the output as a set of properly placed boxes
makes the right sequence of positioning commands
The main difficulty is in finding the right numbers to use
for esthetically pleasing positioning.
With a grammar, it is usually clear how to extend the language.
For instance, one of our users
operator, to make constructions like
~ sub size 7 m sup size 7 l
{bold T from n to k} sub size 7 i sup size 7 j
Grammatically, this is easy:
it is sufficient to add a production like
Semantically, we need only juggle the boxes to the right places.