Sophie

Sophie

distrib > Fedora > 15 > i386 > by-pkgid > 7ebd25ac536d248d499a3ce2acda963a > files > 4875

Macaulay2-1.3.1-8.fc15.i686.rpm

<?xml version="1.0" encoding="utf-8" ?>  <!-- for emacs: -*- coding: utf-8 -*- -->
<!-- Apache may like this line in the file .htaccess: AddCharset utf-8 .html -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 plus MathML 2.0 plus SVG 1.1//EN"	 "http://www.w3.org/2002/04/xhtml-math-svg/xhtml-math-svg-flat.dtd" >
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>parsing precedence, in detail</title>
<link rel="stylesheet" type="text/css" href="../../../../Macaulay2/Style/doc.css"/>
</head>
<body>
<table class="buttons">
  <tr>
    <td><div><a href="_part.html">next</a> | <a href="___Parenthesize.html">previous</a> | <a href="_part.html">forward</a> | <a href="___Parenthesize.html">backward</a> | up | <a href="index.html">top</a> | <a href="master.html">index</a> | <a href="toc.html">toc</a> | <a href="http://www.math.uiuc.edu/Macaulay2/">Macaulay2 web site</a></div>

    </td>
  </tr>
</table>
<hr/>
<div><h1>parsing precedence, in detail</h1>
<div>A newline ends a statement if it can, otherwise it acts like any white space.<table class="examples"><tr><td><pre>i1 : 2+
     3+
     4

o1 = 9</pre>
</td></tr>
</table>
<p/>
Parsing is determined by a triple of numbers attached to each token.  The following table (produced by the command <a href="_see__Parsing.html" title="display parsing precedence table for Macaulay2 operators">seeParsing</a>), displays each of these numbers.<table class="examples"><tr><td><pre>parsing     binary    unary                                                                                      
precedence  binding   binding                                       operators
            strength  strength

     2          2                                                {*end of file*}                                 

     4          4                                                {*end of cell*}                                 

     6                                                               )  ]  }                                     

     8          7                                                       ;                                        

    10         10        10                                             ,                                        

    12                   12                                    do  else  list  then                              

    14         13                                             ->  :=  &lt;-  =  =>  >>                              

    16                   16                                   from  in  of  to  when                             

    18         18        18                                             &lt;&lt;                                       

    20         19        20                                             |-                                       

    22         21                                                      ===>                                      

    22         21        22                                            &lt;===                                      

    24         23                                                      &lt;==>                                      

    26         25                                                      ==>                                       

    26         25        26                                            &lt;==                                       

    28         27                                                       or                                       

    30         29                                                      and                                       

    32                   32                                            not                                       

    34         33                                                !=  =!=  ==  ===                                

    34         33        34                                      &lt;  &lt;=  >  >=  ?                                 

    36         36                                                       ||                                       

    38         37                                                       :                                        

    40         40                                                       |                                        

    42         42                                                       ^^                                       

    44         44                                                       &amp;                                        

    46         46                                                    ..  ..&lt;                                     

    48         48                                                       ++                                       

    48         48        48                                            +  -                                      

    50         50                                                       **                                       

    52                    6                                             [                                        

    54         53                                                     \  \\                                      

    54         54                                                    %  /  //                                    

    54         54        54                                             *                                        

    56         55                                                       @                                        

    58                                                          {*...symbols...*}                                

    58                    6                                            (  {                                      

    58                   12     break  catch  continue  if  return  shield  step  throw  time  timing  try  while

    58                   16                                          for  new                                    

    58                   70                                   global  local  symbol                              

    58         57                                                     SPACE                                      

    60                                                                 (*)                                       

    62         62                                                       @@                                       

    64                                                              ^*  _*  ~                                    

    66         66                                              #?  .  .?  ^  ^**  _                              

    66         66        57                                             #                                        

    68                                                                  !                                        </pre>
</td></tr>
</table>
Here is the way these numbers work.  The parser maintains a number which called the current parsing level, or simply, the level.  The parser builds up an expression until it encounters an input token whose parsing precedence is less than or equal to the current level.  The tokens preceding the offending token are bundled into an expression appropriately and incorporated into the containing expression.<p/>
When an operator or token is encountered, its binding strength serves as the level for parsing the subsequent expression, unless the current level is higher, in which case it is used.<p/>
Consider a binary operator such as <tt>*</tt>.  The relationship between its binary binding strength and its parsing precedence turns out to determine whether <tt>a*b*c</tt> is parsed as <tt>(a*b)*c</tt> or as <tt>a*(b*c)</tt>.  When the parser encounters the second <tt>*</tt>, the current parsing level is equal to the binding strength of the first <tt>*</tt>.  If the binding strength is less than the precedence, then the second <tt>*</tt> becomes part of the right hand operand of the first <tt>*</tt>, and the expression is parsed as <tt>a*(b*c)</tt>.  Otherwise, the expression is parsed as <tt>(a*b)*c</tt>.<p/>
For unary operators, the unary binding strength is used instead of the binary binding strength to reset the current level.  The reason for having both numbers is that some operators can be either unary or binary, depending on the context.  A good example is <a href="__sh.html" title="length, or access to elements">#</a> which binds as tightly as <a href="_..html" title="access to elements whose key is a symbol">.</a> when used as an infix operator, and binds as loosely as adjacency or function application when used as a prefix operator.<p/>
To handle expressions like <tt>b c d</tt>, where there are no tokens present which can serve as a binary multiplication operator, after parsing <tt>b</tt>, the level will be set to 1 less than the precedence of an identifier, so that <tt>b c d</tt> will be parsed as <tt>b (c d)</tt>.<p/>
The comma and semicolon get special treatment: the empty expression can occur to the right of the comma or semicolon or to the left of the comma.<p>One of the most unusual aspects of the parsing precedence table above is that <tt>[</tt> is assigned a precedence several steps lower than the precedence of symbols and adjacency, and also lower than the precedence of <tt>/</tt>.  This was done so expressions like <tt>R/I[x]</tt> would be parsed according to mathematical custom, but it implies that expressions like <tt>f g [x]</tt> will be parsed in a surprising way, with <tt>f g</tt> being evaluated first, even if <tt>f</tt> and <tt>g</tt> are both functions.  Suitably placed parentheses can help, as illustrated in the next example.</p>
<table class="examples"><tr><td><pre>i2 : f = x -> (print x; print)

o2 = f

o2 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i3 : f f [1,2,3]
f
[1, 2, 3]</pre>
</td></tr>
<tr><td><pre>i4 : f f ([1,2,3])
[1, 2, 3]
print

o4 = print

o4 : FunctionClosure</pre>
</td></tr>
<tr><td><pre>i5 : f (f [1,2,3])
[1, 2, 3]
print

o5 = print

o5 : FunctionClosure</pre>
</td></tr>
</table>
</div>
</div>
</body>
</html>