home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / compiler / 2059 < prev    next >
Encoding:
Text File  |  1992-12-22  |  8.8 KB  |  164 lines

  1. Newsgroups: comp.compilers
  2. Path: sparky!uunet!world!iecc!compilers-sender
  3. From: Dave Gillespie <daveg@thymus.synaptics.com>
  4. Subject: Re: Extension Languages
  5. Reply-To: Dave Gillespie <daveg@thymus.synaptics.com>
  6. Organization: Compilers Central
  7. Date: Sat, 19 Dec 1992 01:52:26 GMT
  8. Approved: compilers@iecc.cambridge.ma.us
  9. Message-ID: <92-12-091@comp.compilers>
  10. References: <92-12-087@comp.compilers> <92-12-064@comp.compilers>
  11. Keywords: design
  12. Sender: compilers-sender@iecc.cambridge.ma.us
  13. Lines: 149
  14.  
  15.  
  16. Stavros Macrakis writes:
  17. > Any of these notations are "easy for the machine", although some of the
  18. > tables will be larger for infix notation.
  19.  
  20. and Peter Ludemann writes:
  21. > 20-30% of the CPU time is spent in scanning for the next non-blank ---
  22. > something a parser for any language needs to do (this scanner loop is in
  23. > tightly coded assembler, by the way).  As the standard method of handling
  24. > infix languages is to push things onto a stack, the cost of interpreting
  25. > an infix language should be the same as for prefix and postfix languages.
  26.  
  27. These are valid points, but they're not quite what I was trying to get at
  28. in my original message.  I guess I wasn't very clear about it.  Let's try
  29. to figure out what we really mean by the terms we're using.
  30.  
  31. "Post/pre/infix" can, as purely syntactic terms, be taken at two different
  32. levels.  There's the actual placement of the operator in an expression
  33. involving two operands, which is admittedly a fairly minor issue, but
  34. there's also the more general linguistic philosophy that tends to
  35. accompany the three methods.  I was trying to address the philosophies as
  36. well as the actual notations, since that's what the original poster seemed
  37. to be asking about.
  38.  
  39. If a language is designed so that all functions or operators take a fixed
  40. number of arguments (or more generally, a self-delimiting number of
  41. arguments), and if every function/operator always evaluates its arguments
  42. except when explicit quoting is used, then it's possible to use a postfix
  43. syntax which allows for extremely easy evaluation: Each argument pushes
  44. its value onto the stack, so that when the operator is seen the arguments
  45. are already sitting there in the proper order, ready to be taken off and
  46. used.  Forth and PostScript are good examples of such languages.
  47.  
  48. Some languages want to allow functions which take variable numbers of
  49. arguments (without requiring explicit argument counts or terminator
  50. arguments), or they want to provide implicit quoting of certain arguments
  51. (where, for example, Lisp's "(setq x y)" quotes the first argument so that
  52. it can operate on the variable itself rather than on the variable's
  53. current value).  Both of these can be accomplished by adding an explicit
  54. notation to delimit the argument lists, such as Lisp's parentheses.
  55. Experience seems to show that prefix notation works best with languages at
  56. this level, where pursuit of simplicity is still a major concern.  We tend
  57. to think of these languages when we refer to "prefix languages."
  58. (Traditional "f(x,y)" function calls without any operators, like
  59. Mathematica's FullForm, also fit this description.)
  60.  
  61. Both notations have the property that the natural intermediate form for
  62. storing a scanned-in program closely resembles the source form in
  63. structure.
  64.  
  65. "Prefix" languages make the fine structure more explicit, so they tend to
  66. be better suited when programs want to operate on other programs as data.
  67. In an embedded language like Emacs Lisp, this has the advantage that many
  68. of the interpreter's algorithms can double as primitives available to the
  69. user, and also that much of the interpreter can be written in its own
  70. language rather than the (usually less concise) implementation language.
  71. Since we like our embedded languages to be small and unobtrusive, this is
  72. a big win.  Also, since embedded languages have to cope with changing
  73. environments, the ability to operate on code is useful in its own right.
  74.  
  75. If simplicity of the interpreter isn't so great a concern, a design may
  76. opt for a more complex language that more closely matches the concepts
  77. human programmers like to work with.  This produces languages which tend
  78. to have a variety of infix, prefix, and postfix operators, plus various
  79. syntactic elements like commas, semicolons, braces, etc.  When we refer to
  80. an extension language being "infix," I think we're usually thinking about
  81. these sorts of languages.
  82.  
  83. The problem with "infix" languages is that because of their mixture of
  84. human-style implicit assumptions and redundant syntax, they tend to be
  85. unsuitable for direct use as intermediate languages, while also making the
  86. need for an intermediate language more important because the language is
  87. less tied to the constraints that make an implementation run fast.  So
  88. except for the simplest BASIC interpreters, most "infix" languages compile
  89. to an intermediate form that's closer to a prefix or postfix language in
  90. nature, even in so-called "interpretive" systems.  Stavros brings up Lisp
  91. M-expressions as an example, although I can't help noticing that the early
  92. Lispers gave up on M-expressions because S-expressions (that is, prefix
  93. notation) turned out to be easy enough to use directly.
  94.  
  95. So infix systems generally parse the input into an intermediate form that
  96. is fairly far removed from the source language that humans work with.  As
  97. long as only the compiler or interpreter ever see the intermediate form,
  98. this doesn't really matter.  But in a language like Lisp where the user
  99. works with the intermediate form directly, there is a lot to be gained
  100. from having the intermediate form "look" just like the source form.  It's
  101. a lot less confusing, as well as making the parser a whole lot simpler.
  102. Both Stavros and Peter point out that a more complicated parser doesn't
  103. necessarily have to be much slower than a simple parser, but it *does*
  104. tend to be a lot larger, and compactness of the interpreter is important
  105. for embedded languages.
  106.  
  107. Stavros continues:
  108. > 1) You seem to assume that postfix syntax translates into difficulty of
  109. >    accessing the operator.  But the syntax doesn't tell you anything about
  110. >    the internal representation.
  111.  
  112. But my assumption was based on the desire to have the internal
  113. representation be close to the syntax.  When I said postfix was slightly
  114. slower for Lisp-like languages, I meant that, given a direct mapping of
  115. syntactic order onto internal Lisp lists, postfix has a slight extra cost
  116. because the argument list must be scanned once to reach the operator
  117. before you can know how to evaluate the arguments.  It was only meant to
  118. be a minor point, though.  A system that stored argument vectors instead
  119. of lists, for example, would not have this problem.
  120.  
  121. I'm pretty confident that prefix is more natural than postfix, all else
  122. being the same, as least to English readers.  Because a Lisp operator can
  123. affect the way the arguments are interpreted, people reading left to right
  124. want to see the operator first.  That seems like a much more compelling
  125. argument against postfix, unless you're going to go the whole postfix
  126. route and have no parentheses to delimit the arguments.
  127.  
  128. > 2) Many machine-oriented languages are postfix, precisely because they are
  129. >    EASIER to interpret.  The problem comes only with non-strict operators
  130. >    like "if", which such languages avoid by using explicit gotos.
  131.  
  132. Or by quoting, as with PostScript's "/" and "{ }" notations.  Postfix is
  133. indeed great if its utter minimalism is enough for you, but if you want to
  134. write large programs, or do anything other than simply interpret a stored
  135. program, it gets clumsy.  (Even PostScript was designed to be written
  136. mainly by other programs, where the human-friendly structure goes in the C
  137. program that generates the PostScript rather than in the PostScript
  138. itself.)
  139.  
  140. >    I've also seen languages which are Lisp-like in essence, but with C-like
  141. >    parsers added on top to make them more palatable.  In other words, they
  142. >    have a parser which reads "a+b" into the list "(+ a b)".
  143.  
  144. I've even used this technique myself.  But it does complicate the parser,
  145. and I can understand that such creature comforts might not seem worthwhile
  146. in systems (like Emacs) which don't envision major applications being
  147. written in the extension language (my own crazed efforts notwithstanding
  148. ;-).
  149.  
  150. > Yes, there are Lisp-like languages which provide an infix syntax, or even
  151. > Lisps which happen to have an infix reader as well.  But they are not
  152. > Lisp-like because they represent the program as a tree!  After all, many
  153. > language processors represent the program as a tree at some point....
  154.  
  155. Right, they are Lisp-like because they represent the program as a *data
  156. structure of the language* which is a tree.  A true Lisp fanatic might
  157. argue that your last point merely shows that other languages must convert
  158. themselves to Lisp in order to get anything done!
  159.  
  160.                                 -- Dave
  161. -- 
  162. Send compilers articles to compilers@iecc.cambridge.ma.us or
  163. {ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.
  164.