Channa Dias Perera

Creating a compiler for a subset of Pascal in Rascal - Prologue

Motivation & Goals

In my Compiler Construction class at the University of Groningen, we made a compiler for a (very limited) subset of Pascal to a subset of C that is meant to simulate some assembly, which we left unoptimized. I thought it would be cool to extend this by:

  • Expanding upon the subset of Pascal
  • Targeting a lower level language
  • Performing optimizations on the generated code

Futhermore, in my Software Language Engineering class (same university), we learnt about a very cool meta-programming langauge called Rascal (more on this below). I thought it would be a fun project to implement the compiler in this language.

So what is the target language? Somewhat arbitrarily, I have chosen LLVM's IR. I thought it makes for a nice trade-off between portability and fun-factor (i.e: it's still relatively low level). Additionally, I don't believe there is any library in Rascal for this purpose, so perhaps I can make a nice contribution, in this regard.

The code for the repository can be found here.

MiniPascal

The subset of Pascal we will parse is called 'MiniPascal' or 'MiniPas'. It allows for:

  • Global function / procedure delcarations
  • Basic control flow (loops / conditional)
  • Integer / real types, as well as arrays of the two types

Indeed, this is quite limited! But it will suffice, I think, to show various interesting features. The full grammar and semantics will follow in later blog posts.

What is Rascal?

Rascal is a imperative / functional programming language intended to make it easy to create tools (such as compilers / type checkers / etc.) for Domain-Specific Languages. It comes with a lot of features that would usually be provided by a library and has very nice syntax.

I won't go over all the details here, as I intend to explain syntactic details / ideas as I write my posts. Some highlights however:

  • Built-in support for defining lexical syntax / grammars
  • Imperative-style functions
  • Haskell-style function definitions
  • A deep standard library
  • Easy traversal objects made of Abstract Data Types
  • Strong support for pattern-matching
The last one is really super, it makes it very easy to define transformations / conduct analysis on the data we create. If you would like to have a quick overview of the constructs / features in Rascal, please reference this cheat-sheet, made by the developers of Rascal. Of course, you can find the full documentation here. It is quite good!

If the last two points, are unclear, do not worry. It's a bit easier to demonstrate, than to describe succintly (for your humble author).

Acknowledgements

Thanks to the Compiler Construction team for defining MiniPas :)

Disclaimer

I'm merely a 4th year BSc student, so perhaps a lot of what I will be writing (both in terms of prose, as well as code) will be utter garbage. Please bear with me >:)

Certainly, I am no expert in Rascal, nor compiler construction. This is simply a blog post for my friends to read. If you have any corrections, please contact me, I'm very open for feedback.