Compile Pseudocode. But Why? [Updated]

Pseudocode is meant to be a way to quickly sketch or communicate how a coding problem could be solved. It’s not a real programming language, but usually resembles the language one is targeting.

Well, I’ve been teaching a class based on a book that takes the approach of learning the fundamentals of programming by focusing on pseudocode. It’s a mixed bag. Yes, a lot of complexity is avoided so students can focus on the logic rather than syntax, but there is no way to test it. It all feels abstract and theoretical.

I tried doing examples in class to demonstrate, but many students need that experience to make it stick. Long story short, I created a web page that gives them something to experiment with. It’s based on the pseudocode outlined in the textbook, Extended Prelude to Programming. (I should note that I’m in no way affiliated with the author or publisher of the book.)

Basically, I take what’s typed into a text input and convert it via php into javascript that runs on the page. Much simpler than writing a real compiler, but it’s a bit of a parsing job (and very incomplete.) Still, it should prove very helpful to some students. Especially if they have access to it from the beginning of the class.

It’s located here.

Here’s the PHP script zipped up and ready to host: pseudo.php

If you use it, I make no promises that it won’t blow up your server computer, crash an operating system or cause your browser to hang (in fact, an infinite loop may do just that) but it works for me. 🙂

(I updated the links and added a downloadable script on March 16th, 2013)

Tags: , , , , , ,

12 Responses to “Compile Pseudocode. But Why? [Updated]”

  1. Seth says:

    That is no kidding! I am taking a class on logic and design and the concept of learning pseudocode without the ability to try things seriously interferes with learning this stuff for me. I will have to say the point of the class is to familiarize students with the IPO, flowcharting with Visual Logic and pseudocode as a whole, not so much to perfect our ability to use it, though we are being graded as though we know what we are doing, which I don’t.

  2. aaron says:

    Sadly and a bit ironically the site only works with a very specific “pseudo code” described in that book. Would be interesting to make it as general as possible and see if more people could use it. Hmm…

  3. Scott says:

    Aaron, I teach this same class (same textbook) and wondered if you would be willing to share your code as I think it would be great for my students to use. Then I just have to find someplace to host it… I’ve actually been working on doing a pseudocode interpreter based on the work in Ronald Mak’s Writing Compilers and Interpreters but finding the time to make a progress is hard to do! Thanks!

  4. aaron says:

    Scott, you may just use the web site if you’d like for now. Not sure if traffic would be a problem or not. I’d be happy to send you the php script. Actually it might be easier if I just include it, updated in this post.

    The link has changed, too. There is a more up to date one at http://buzzabit.com/learn

    The nastiest bug in the script is with for loops. If you don’t put a space between for and the first parenthesis it won’t work! “for (” not “for(“

  5. Scott says:

    Super–thanks Aaron! We can see if it will work. I’ve only got 24 in the class this term which starts Monday. I have been trying to use Python for the class but combined with the difference in the textbook makes it a little confusing for the students. I don’t like Raptor which comes with the text but have used Progranimate (similar graphic flowchart programming) but it really gets cumbersome to create programs after the first few chapters. I hope that we get a textbook that uses a real language one day 🙂

  6. aaron says:

    The post is updated with a downloadable script and an updated link. If you have success with it, please improve it! There are a few problems. One I’ll probably fix soon is that the book introduces Print as a command which writes without starting a newLine. I had already made my own (WritePart) before the latest edition added it. It would be good to use what the book uses. 🙂

    I also just noticed by accident that you can access the characters in a String by simply using square brackets. That was a fun class once we realized that worked (it works that way in Javascript I didn’t put anything in the code to support it).

  7. Scott says:

    Thanks for sharing Aaron! If I can find anything I can help on, I’ll be sure to pass it back to you! That’s pretty cool that the character access for a string worked out without even having to do anything about it.

    I appreciate your help and will let you know how things work out with the class and if I ever find time to work on the Java-based interpreter I’ll let you know. Although, I think your solution of doing it on a server may be better since it doesn’t require any additional software installation by the student–just the ability to access the site with a browser.

  8. aaron says:

    No problem. I’m glad someone else gets to benefit from it. 🙂

    As far as online or not, really both would be better. I occasionally have some students who don’t have internet at home and would like to use it. Doesn’t happen often though. Some students told me they run it on their smart phone. I haven’t tried but it would need scripted dialog windows enabled.

  9. aaron says:

    One more thing. I got curious if I could make some sort of a bundled download to run this without an internet connection and I found a way! There me be a better solution, but if you look up “Viewer for PHP” http://viewerforphp.sourceforge.net/ there is an old program that basically bundles up an apache server and you can just swap out their index.php with this script and zip it up and distribute. I might try this next time a student has a need. It’s also kind of handy for testing.

    • Scott says:

      The bundled viewer is a neat idea. I already installed a LAMP VirtualBox appliance to play with things and it works great there (but would be a lot of work for a student just to try out some code!)

      I’ve also put it it up on a free hosting account I have but have run into problems with quotes being backslashed automatically. I think it is due to MagicQuotes but I’ll have to look into it some more…

  10. Scott says:

    Aaron, I just wanted to let you know that I did get the script running on the free hosting service and that students have been able to access it fine. I started to look at a couple of things but first had to get used to php and javascript since I don’t really use them much. Anyway, I may have found the problem with for() vs for (). I noticed this also affected if() and while() if no spaces where put between the keyword and the parenthesis. On the line where you create a new string that contains everything after the token, you have $c + 1 as one of the parameters for the substr() function. I think this was to get rid of the space that would normally be after the keyword but if there is no space, the open parenthesis was being removed. Since you use the trim function a couple of lines later on $lineAfter, I just changed $c+1 to c$. So even if there is a space at the beginning, it gets removed by the trim but if there is a parenthesis, it gets left in place. So far I haven’t run into any problems testing locally it so I may go ahead and put it on my hosted server the class is using and give it the real test!

Leave a Reply

You must be logged in to post a comment.