<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Passing Curiosity: Posts tagged parsing</title>
    <link href="https://passingcuriosity.com/tags/parsing/parsing.xml" rel="self" />
    <link href="https://passingcuriosity.com" />
    <id>https://passingcuriosity.com/tags/parsing/parsing.xml</id>
    <author>
        <name>Thomas Sutton</name>
        
        <email>me@thomas-sutton.id.au</email>
        
    </author>
    <updated>2006-04-04T00:00:00Z</updated>
    <entry>
    <title>Order of Operations</title>
    <link href="https://passingcuriosity.com/2006/order-of-operations/" />
    <id>https://passingcuriosity.com/2006/order-of-operations/</id>
    <published>2006-04-04T00:00:00Z</published>
    <updated>2006-04-04T00:00:00Z</updated>
    <summary type="html"><![CDATA[<p>A recent experience reminded me of some posts on <a href="http://mathandtext.blogspot.com/">Math and Text</a>, a
blog on mathematics education. <a href="http://mathandtext.blogspot.com/2005/12/so-long-aunt-sally.html">So Long, Aunt Sally!</a> discusses
mnemonic devices for the order of operations (and reasons that such are
a Bad Thing) and <a href="http://mathandtext.blogspot.com/2005/05/order-of-operations.html">Order of Operations</a> motivates the standard order
of operations in terms of error reduction (performing those operations
which will most effect the magnitude of the result first will reduce our
error in the event that we’ve misread a number).</p>
<p>Both of these posts interest me as I think that they both contain within
them interesting insights to mathematics as seen by most of us (the
non-mathematicians). The standard mathematical notation (that is, infix
operators interpreted according to the standard order of operations) has
a long history and is very much embedded with our understanding of
maths. Unfortunately it is also rather poorly suited to expressing
complex sentences: it requires that we interpret sentences with respect
to a specific order of operations (exponentiation,
multiplication/division, addition/subtraction) and use grouping
operators (parentheses, brackets and the like).</p>
<p>This shortcoming is, however, addressed in a class of expression
languages called prefix and postfix languages. The difference between an
infix language (like the standard mathematical notation), a prefix
language and a postfix language is suggested by their names. An
<strong>in</strong>fix language situates operators in-between their arguments, a
<strong>pre</strong>fix language writes operators before their arguments and a in
<strong>post</strong>fix language the operators follow their arguments.</p>
<p>Where there is an ambiguity in infix languages as to which operators
ought to be evaluated first (Left-to-right? Inward? Outward? According
to precedence?), sentences of pre- and post-fix languages have only one
possible interpretation. In these languages operations are performed in
the order that they are written and we can express every sentence
without using grouping operators (like brackets).</p>
<p>In an infix language, the sentence <code>1+2*3</code> is ambiguous and can be
parsed in two different ways: <code>(1+2)*3</code> and <code>1+(2*3)</code>. Without a system
of operator precedence, there is no way in which we can determine which
of these two interpretations is “correct.” In a pre- or post-fix
language however, these two different interpretations are actually
written differently. In postfix form, they can be written as <code>1 2 + 3 *</code>
and <code>1 2 3 * +</code> respectively.</p>
<figure>
<img src="/files/2006/parsing.0.jpg" alt="Parse trees for the sentence “1+2*3”." />
<figcaption aria-hidden="true">Parse trees for the sentence “1+2*3”.</figcaption>
</figure>
<p>As they don’t need an operator precedence scheme to determine which
possible interpretation, these languages are much easier to parse and
evaluate. This makes them ideal for use in constrained environment like
calculators and printers. It is no surprise then, that many printers use
the language Postscript to describe documents to be printed (with
operators like draw-line and change colour, in addition to add and
multiply) and some calculators use a programming language called RPN
(though newer calculators also provide more complete languages).</p>
<p>The algorithm for evaluating an expression is postfix form is:</p>
<ol type="1">
<li><p>Read a token;</p></li>
<li><p>If it’s a value push it onto the stack;</p></li>
<li><p>If it is an operator:</p>
<ol type="1">
<li>Pop the appropriate number of arguments off the stack;</li>
<li>Perform the operation on those arguments; and</li>
<li>Push the result of the operation onto the stack</li>
</ol></li>
<li><p>Repeat until there is no more input and the answer will be the
[single] item remaining on the stack.</p></li>
</ol>]]></summary>
</entry>

</feed>
