Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.013 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'expressions': 0.07; 'parser': 0.07; 'rolling.': 0.09; 'terms,': 0.09; 'cc:addr:python- list': 0.11; 'python': 0.11; 'dice': 0.16; 'each)': 0.16; 'evaluating': 0.16; 'expression.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'written': 0.21; 'code,': 0.22; 'programming': 0.22; 'bonus': 0.22; 'cc:addr:python.org': 0.22; 'adds': 0.24; 'mathematical': 0.24; 'fairly': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; "d'aprano": 0.31; 'develops': 0.31; 'follows': 0.31; 'pascal': 0.31; 'steven': 0.31; 'allows': 0.31; 'languages': 0.32; 'received:google.com': 0.35; 'google': 0.35; 'add': 0.35; 'building': 0.35; 'similar': 0.36; 'behind': 0.37; 'e.g.': 0.38; 'sure': 0.39; 'expression': 0.60; 'deals': 0.65; 'limit': 0.70; 'yourself': 0.78; 'common,': 0.84; 'examples.': 0.84; 'rolls': 0.84; 'to:none': 0.92; 'fun!': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=WuDAQmByqYpsLhf9JSu0z9JykT8IqcSur+FHSWQP5pQ=; b=oHOuav9ABIwh+KP4iJeQcwP/9KmNyEHRBUqtyHqepkJ1bdxCDRRR26ygRrFjuV+cxV CGuZJSFc5PKTAa7WpQ9POP7W+6NYAGQ/uxNaLW/tAYSagNCdd23Yb6Bj3ZDTqf6D59I8 ZO5xFxc1Yjab3ZFwDNKPOfBV8tXO9Spx7PohagTldCj2+Cx2ADtbbWfvMLYShyTrX8vt 6KvazyZL+1uFjT/8sJB58J+ewPcIkAarJFk74T1mzTrL7ShDOLYYeoKwMEN9duZXMePC qSiMPvZ5bFsjEciuGzeGXoGZWSt6TKH5IgAonb4JuK6zDhfYNSNvcRGNJIJM9T9HzZ5B BgrA== MIME-Version: 1.0 X-Received: by 10.50.176.202 with SMTP id ck10mr21564708igc.2.1412182904075; Wed, 01 Oct 2014 10:01:44 -0700 (PDT) In-Reply-To: <542c2f9a$0$12975$c3e8da3$5496439d@news.astraweb.com> References: <4a7f3e7a-ba4d-405e-a078-937bf861fcd8@googlegroups.com> <542c2f9a$0$12975$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 2 Oct 2014 03:01:44 +1000 Subject: Re: how to parse standard algebraic notation From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1412182911 news.xs4all.nl 2895 [2001:888:2000:d::a6]:34461 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78558 On Thu, Oct 2, 2014 at 2:45 AM, Steven D'Aprano wrote: > Code for evaluating mathematical expressions are very common, if you google > for "expression parser" I am sure you will find many examples. Don't limit > yourself to Python code, you can learn from code written in other languages > too, e.g. I have a Pascal programming book that develops a parser for both > polynomials and arithmetic expression. Building expression evaluators is good fun! And then you can add specialty terms, like dice rolling. > roll 2d6 + d6 "Electric" + 12 "Strength" [ROLL] Rosuav rolls 2d6: 3, 6, totalling 9. [ROLL] Rosuav rolls d6: 1 ("Electric") [ROLL] Rosuav adds a bonus of 12 ("Strength") [ROLL] For 2d6 + d6 "Electric" + 12 "Strength", Rosuav totals: 22 The code behind that follows a fairly similar pattern to what you describe, only it deals with dice rolls (NdM -> roll N dice with M sides each) instead of powers of x. (And it allows comments/tags.) ChrisA