📄 e5
字号:
.NHLanguage Theory.PPThe basic structure of the language isnot a particularly original one.Equations are pictured as a set of ``boxes,''pieced together in various ways.For example, something with a subscript isjust a box followed by another box moved downwardand shrunkby an appropriate amount.A fraction is just a box centered above another box,at the right altitude,with a line of correct length drawn between them..PPThe grammar for the language is shown below.For purposes of exposition, we have collapsedsome productions. In the original grammar, thereare about 70 productions, but many of theseare simple ones used only to guaranteethat some keyword is recognized early enough in the parsing process.Symbols incapital lettersare terminal symbols; lower casesymbols are non-terminals, i.e., syntactic categories.The vertical bar \(bv indicates an alternative;the brackets [ ] indicate optional material.A.UC TEXTis a string of non-blank characters orany string inside double quotes;the other terminal symbols represent literal occurrencesof the corresponding keyword..P1.ce 0.ta .3i.ps 9.ne 17.in 1eqn : box | eqn box.sp 5pbox : text | { eqn } | box OVER box | SQRT box | box SUB box | box SUP box | [ L | C | R ]PILE { list } | LEFT text eqn [ RIGHT text ] | box [ FROM box ] [ TO box ] | SIZE text box | [ROMAN | BOLD | ITALIC] box | box [HAT | BAR | DOT | DOTDOT | TILDE] | DEFINE text text.sp 5plist : eqn | list ABOVE eqn.sp 5ptext : TEXT.ps 10.in 0.P2.PPThe 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:.P1.ce 0 eqn : box | eqn box box : text | { eqn }.P2Anywhere a single character could be used,.ulanylegal construction can be used..PPClearly, our grammar is highly ambiguous.What, for instance, do we do with the input.P1a over b over c ?.P2Is it.P1{a over b} over c .P2or is it.P1a over {b over c} ?.P2.PPTo answer questions like this, the grammaris supplemented with a small set of rules that describe the precedence and associativityof operators.In particular, we specify (more or less arbitrarily)that.uloverassociates to the left,so the first alternative above is the one chosen.On the other hand, .ulsuband.ulsupbind to the right,because this is closer to standard mathematical practice.That is, we assume $x sup a sup b$ is $x sup {(a sup b )}$,not $(x sup a ) sup b$..PPThe precedence rules resolve the ambiguity in a construction like.P1a sup 2 over b.P2We define.ulsupto have a higher precedence than.ulover,so this construction is parsed as$a sup 2 over b$ instead of $a sup {2 over b}$..PPNaturally, a user can alwaysforce a particular parsingby placing braces around expressions..PPThe 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 benormally used for resolving ambiguity.Instead the supplemental information aboutprecedence and associativity (also small enough to be understood)provides the compiler-compilerwith the information it needsto make a fast, deterministic parser forthe specific language we want.When the language is supplemented by the disambiguating rules,it is in fact.UC LR(1)and thus easy to parse[5]..PPThe output code is generated as the input is scanned.Any time a productionof the grammar is recognized,(potentially) some.UC TROFFcommands are output.For example, when the lexical analyzer reports that it has found a.UC TEXT(i.e., a string of contiguous characters),we have recognized the production:.P1text : TEXT.P2The translation of this is simple.We generate a local name for the string,then hand the name and the string to.UC TROFF,and let.UC TROFFperform the storage management.All we save is the name of the string,its height, and its baseline..PPAs another example,the translation associated with the production.P1box : box OVER box.P2is:.P1.ce 0.in 1.ne 14Width of output box = slightly more than largest input widthHeight of output box = slightly more than sum of input heightsBase of output box = slightly more than height of bottom input boxString describing output box = move down; 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..in 0.P2Most of the other productions have equally simple semantic actions.Picturing the output as a set of properly placed boxesmakes the right sequence of positioning commandsquite obvious.The main difficulty is in finding the right numbers to usefor esthetically pleasing positioning..PPWith a grammar, it is usually clear how to extend the language.For instance, one of our userssuggested a.UC TENSORoperator, to make constructions like.EQ~ sub size 7 m sup size 7 l{bold T from n to k} sub size 7 i sup size 7 j.ENGrammatically, this is easy:it is sufficient to add a production like.P1box : TENSOR { list }.P2Semantically, we need only juggle the boxes to the right places.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -