Compiler Construction (in Python)

CompilerConstruction

Learning <insert any subject here> has never been easier! Massive Open Online Courses are a real thing, and while we can debate how high we are at this point on the hype curve, the world is a better place because we do have access to a new powerful set of tools for learning. Even if less Massive and less Open, tools such as iTunesU can offer a lot of content from the best universities in the world. Being the happy user of a Mac (and an iPad) I did recently attend “virtually” a fast paced class taught by nothing less than prof. Paul Hegarty of Stanford School of Engineering:  ” Developing iOSX8 applications in Swift“.  The experience turned out to be so positive (high quality video and audio, great presenter, challenging but not impossible coding exercises), that shortly I was looking for more …

Now you might remember from my previous postings [1][2][3] that one of my all time favorite subjects at the university was: Programming Languages and their Compilers. This is likely because I had great teachers, as I had the luck to study with  prof. Hmeljak  and prof. Sipala at the University of Trieste. We followed the path traced by prof Niklaus Wirth, as I believe most European Universities do. We started with the Pascal language, continuing with Modula (II) and on to Oberon following a trajectory from a simple procedural (Algol-like) language to a more structured one, to a most modern object oriented. My first experience writing an actual compiler for such languages was  based on Wirth’s book “Algorithms + Data Structures”  where, in few chapters, the author manages to present a complete compiler for a subset of Pascal (called PL/0) written in the same language. Unfortunately since I was following an Electronic Engineering rather than a pure Computer Science program, my exposure to the subject remained limited to this one example. At the time I re-wrote the code in C (Borland Turbo-C that was) and attempted to modify the grammar to fit my own personal preferences as I am sure most CS students have done (or dreamed of).

Fast forward a few decades (oops), and here I am looking on iTunesU at the class: “Introduction to Compiler Construction”, presented by prof. Christoph Kirsch, of the University of Salzburg. He teaches using the “Wirth method” and develops an Oberon compiler (based on Oberon-0) as Wirth’s last work: Project Oberon – The Design of an Operating System and Compiler.  This was music to my ears!

Although Prof. Kirsch was suggesting to his students to use C/C++ to develop the compiler,  I could not help but feel the urge to spice things up a bit and follow the project with a small twist: I used Python to develop the simplified Oberon compiler!

Here is a [link] to the Github repository containing my new implementation of the simplified Oberon-0 compiler. I learned a lot (about Python and about compiler design) while doing the porting and I will report in a future post more about my experience and my plans for its future evolution… stay tuned!

 

This entry was posted in Languages, Python, Tools and tagged , , , , . Bookmark the permalink.