LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2011, 08:13 PM   #1
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Parsing S-Expressions in Python


I've been loosely following this:

http://norvig.com/lispy.html

And I have a problem: the parsing function throws an array out of bounds exception. I thought that maybe I'm doing it wrong, so I copy and pasted the code from the page, and still the same error!

I'm using Python 3 (not 2), if it matters.
 
Old 02-01-2011, 10:12 PM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
I've looked at the page you link to - it's quite interesting. I had a look at the lis.py module and noticed it's definitely python2 and not python3 compliant having print statements and calling the now removed operator.div method of the operator module. I fixed the print statements running the 2to3 utility on the code and replaced the operator.div with operator.truediv calls manually. This meant I could now import it and run repl() to get the prompt. I also tested some examples of program(), tokenize() and parse() which worked too. This example, "(count (quote the) (quote (the more the merrier the bigger the better)))", of using the prompt to evaluate statements failed though so I left it at that. The example works however in python2.

This is undoubtedly a python3 incompatibility and I suspect your problems are the same. Possibly try your tests in python2 ?

Last edited by bgeddy; 02-01-2011 at 10:14 PM.
 
Old 02-02-2011, 08:00 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443

Original Poster
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Turns out it was a dumb mistake. I wrote this:

Code:
return s.replace('(',' ( ').replace('(',' ( ').split()
instead of this:

Code:
return s.replace('(',' ( ').replace(')',' ) ').split()
in the tokenizing function.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: Parsing data in chunks/sections? Neruocomp Programming 3 11-06-2010 04:06 AM
LXer: Parsing object-oriented expressions with Dijkstra's shunting yard algorithm LXer Syndicated Linux News 0 10-04-2010 04:20 PM
parsing a version number with regular expressions? neunon Linux - General 5 11-14-2009 08:01 AM
Parsing a Grub config file: Python vharishankar Programming 8 03-03-2006 10:24 AM
Regular Expressions in Python indian Programming 7 09-14-2005 11:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration