Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Wow, I guess pyparsing is really on the map! Thanks Alex and John for stepping in on this question. You are both on the mark with your responses. But let me add a comment or two:</p> <ol> <li><p>If we suppress the opening and closing parenthesis symbols, and group the parenthesized expression using Group, pyparsing will a structured result that is closer to an AST. </p> <pre><code>from pyparsing import Literal,Word,ZeroOrMore,Forward,nums,oneOf,Group def Syntax(): op = oneOf( '+ - / *') lpar = Literal( '(' ).suppress() rpar = Literal( ')' ).suppress() num = Word(nums) expr = Forward() atom = num | Group( lpar + expr + rpar ) expr &lt;&lt; atom + ZeroOrMore( op + expr ) return expr if __name__ == "__main__": expr = Syntax() def test(s): results = expr.parseString( s ) print s,'-&gt;', results test( "(9 + 3)" ) test( "(9 + 3) * (4 / 5)" ) </code></pre> <p>Giving:</p> <pre><code>(9 + 3) -&gt; [['9', '+', '3']] (9 + 3) * (4 / 5) -&gt; [['9', '+', '3'], '*', ['4', '/', '5']] </code></pre> <p>Otherwise, pyparsing is just tokenizing, and you have to walk the list of parsed tokens to find the nested expressions.</p></li> <li><p>Since op is defined as just oneOf("+ - * /"), there is no precedence of operations. There are examples on the pyparsing wiki of the manual way to define this (fourFn.py), or the more recent approach using the operatorPrecedence helper (simpleArith.py). Again, this has pyparsing adding more value than just tokenizing.</p></li> </ol> <p>To the OP, please check out those examples, I think they will help move you forward on your project.</p> <p>-- Paul</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload