Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #110896

Re: Creating a calculator

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Christopher Reimer <christopher_reimer@icloud.com>
Newsgroups comp.lang.python
Subject Re: Creating a calculator
Date Fri, 01 Jul 2016 06:19:00 -0700
Lines 55
Message-ID <mailman.157.1467379145.2358.python-list@python.org> (permalink)
References <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <lf5oa6hhn9x.fsf@ling.helsinki.fi> <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> <mailman.156.1467375975.2358.python-list@python.org> <lf5poqxmsqc.fsf@ling.helsinki.fi> <81839B88-3F2B-4E28-9E59-F4B1AAAF058D@icloud.com>
Mime-Version 1.0 (1.0)
Content-Type text/plain; charset=us-ascii
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de p2ft7RNWEPLKn0l3EOVAeQWIXCELljtCQl/MyYImG6Og==
Return-Path <christopher_reimer@icloud.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'broken': 0.03; 'interpreter.': 0.07; 'trailing': 0.07; 'cc:addr:python-list': 0.09; 'expected.': 0.09; 'interpreter,': 0.09; 'unpack': 0.09; 'syntax': 0.13; 'ignore': 0.14; 'interpreter': 0.15; 'crashes': 0.16; 'element.': 0.16; 'keyword,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'spurious': 0.16; 'try/except': 0.16; 'valid.': 0.16; 'written.': 0.16; 'wrote:': 0.16; 'later': 0.16; 'element': 0.18; 'integer': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'strip': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'component': 0.23; 'ignored.': 0.23; 'split': 0.23; 'this:': 0.23; 'written': 0.24; "i've": 0.25; "doesn't": 0.26; 'chris': 0.26; 'least': 0.27; 'received:17': 0.27; 'end,': 0.29; 'spaces': 0.29; 'convert': 0.29; 'raise': 0.29; 'print': 0.30; 'code': 0.30; 'normally': 0.30; 'received:10.0.0': 0.32; 'statement': 0.32; 'run': 0.33; 'source': 0.33; 'third': 0.33; 'quotes': 0.33; 'correctly': 0.34; 'file': 0.34; 'received:10.0': 0.34; 'list': 0.34; 'could': 0.35; 'but': 0.36; 'needed': 0.36; 'basic': 0.36; 'keyword': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; "won't": 0.38; 'several': 0.38; 'end': 0.39; 'test': 0.39; 'still': 0.40; 'header :MIME-version:1': 0.60; 'enclosed': 0.63; 'strange': 0.63; 'more': 0.63; 'different': 0.63; 'jul': 0.72; '1991': 0.84; 'dialect': 0.84; 'header:In-reply-to:1': 0.84
X-Proofpoint-Virus-Version vendor=fsecure engine=2.50.10432:,, definitions=2016-07-01_04:,, signatures=0
X-Proofpoint-Spam-Details rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=0 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1607010134
X-Mailer iPhone Mail (13F69)
In-reply-to <lf5poqxmsqc.fsf@ling.helsinki.fi>
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1467379142; bh=nZeXWbOQjkNIma1EvPlm5J6lPCcyWd9t/1TZRiDZKss=; h=Content-type:MIME-version:Subject:From:Date:Message-id:To; b=j4tdjKF/nJXy9ly5tZXTKv60jwRPAXSI45TyzyV6t3sdYq6Xy5fZ6s+vHo/Bji5CD hK6rN+wc8s7SJtRVusv4WUuF0gyZe3gKSBRmtOLfk5LxZUZ+8qyzC8uAa+44t8fhfM e/XTrnLxUBEWGHUVUATouik1O6fdMOZefadW/LghqPATMvHPTv7N+dUytYbTtn8rb3 eaVUif1G0w/k7EnxD9zoTezvbVZss22ZrvDz/kxcr2ZyTVR9sDXyB63udGNZLgf8KI RKaMoAp07/P28KpV/xd+te65PkEyZKm/EXiPqUBDvYi7bi37tQxvajQe4oDSFpiw9u ilZQqZ3bDWdIw==
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <81839B88-3F2B-4E28-9E59-F4B1AAAF058D@icloud.com>
X-Mailman-Original-References <a0ed0591-dcbb-4ad2-b69a-58feef50153a@googlegroups.com> <nl4t1n$qgp$2@dont-email.me> <lf5oa6hhn9x.fsf@ling.helsinki.fi> <0E77E5BC-E8CC-4951-B520-37BF2E596D3A@icloud.com> <mailman.156.1467375975.2358.python-list@python.org> <lf5poqxmsqc.fsf@ling.helsinki.fi>
Xref csiph.com comp.lang.python:110896

Show key headers only | View raw


> On Jul 1, 2016, at 5:46 AM, Jussi Piitulainen <jussi.piitulainen@helsinki.fi> wrote:
> 
> Christopher Reimer writes:
> 
>> For my BASIC interpreter, each line of BASIC is broken this way into
>> tokens.
>> 
>> line_number, keyword, *expression = line.split(' ', 2)
>> 
>> For a line like 10 PRINT "HELLO, WORLD!", this works as expected.
>> 
>> For a line like 20 END, which doesn't have a third element for
>> expression, an empty list is assigned.
>> 
>> By using * to unpack the split line, my program no longer crashes and
>> no try/except block is needed to work around the crash. A later line
>> of code will test the expression, ignore if empty or run regex if
>> full.
> 
> Yes.
> 
> Consider line.split(None, 2) or line.split(maxsplit=2). You may still
> need to strip the third component, but at least it will *be* the third
> component even if the user happens to type extra spaces like this:
> 
> 10   PRINT  "Hello, world."
> 
> And you won't get a spurious third component if the user types this:
> 
> 20 END   
> 
> (There's trailing spaces.)

This is a BASIC interpreter. Any extra spaces for line_number or keyword will raise an incomprehensible syntax error, as the line number must convert to an integer and the keyword much match the KEYWORDS list. As for trailing spaces for expression, regex will keep them whole if enclosed in double quotes or convert them to a list element. Depending on the keyword grammar, extra spaces are likely to be ignored.

A C interpreter would be more difficult as the same line of code could be written several different ways and be valid.

FILE *source
FILE * source
FILE* source
FILE*source

The first line is how I normally see this statement written. The last line I found in a 1991 book with a strange dialect of C that I've never seen before and doesn't always compile correctly without modification. Writing a C interpreter is not on my to do list. 

Chris R.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Creating a calculator Elizabeth Weiss <cake240@gmail.com> - 2016-06-30 20:08 -0700
  Re: Creating a calculator Michael Torrie <torriem@gmail.com> - 2016-06-30 21:38 -0600
  Re: Creating a calculator DFS <nospam@dfs.com> - 2016-06-30 23:57 -0400
  Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 00:54 -0400
    Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 09:42 +0300
      Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 05:25 -0700
        Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 15:46 +0300
          Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 06:19 -0700
            Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 16:35 +0300
        Re: Creating a calculator Steven D'Aprano <steve@pearwood.info> - 2016-07-01 23:52 +1000
          Re: Creating a calculator alister <alister.ware@ntlworld.com> - 2016-07-01 14:21 +0000
          Re: Creating a calculator Christopher Reimer <christopher_reimer@icloud.com> - 2016-07-01 07:15 -0700
          Re: Creating a calculator Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-07-01 20:44 +0300
          Re: Creating a calculator Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2016-07-04 12:32 +0200
      Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 20:16 -0400
    Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-01 11:34 +0200
      Re: Creating a calculator Chris Warrick <kwpolska@gmail.com> - 2016-07-01 13:39 +0200
        Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-01 15:03 +0200
      Re: Creating a calculator DFS <nospam@dfs.com> - 2016-07-01 20:16 -0400
        Re: Creating a calculator pdorange@pas-de-pub-merci.mac.com (Pierre-Alain Dorange) - 2016-07-04 11:50 +0200
        Re: Creating a calculator BartC <bc@freeuk.com> - 2016-07-06 01:53 +0100
          Re: Creating a calculator Quivis <quivis@domain.invalid> - 2016-07-07 22:00 +0000
  Re: Creating a calculator Chris Warrick <kwpolska@gmail.com> - 2016-07-01 10:57 +0200

csiph-web