Skip to content

einaregilsson/While-Language

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table of Contents

A While language compiler for the .NET platform

What is the While language?

The While programming language is introduced in the book Principles of Program Analysis by Nielson, Nielson and Hankin. It is a simple imperative language with integer variables and integer and boolean expressions. For branching it has an if-else statement and for looping it has a while statement. Other statements it has is an assignment to assign integer expressions to variables, read and write statements to read input from user and print to screen, and a skip statement that has no effect. The language also contains procedures that can take 0-n parameters by value and 0 or 1 result parameter. All parameters are integers.

Why a compiler for a toy language?

My masters project at DTU involves writing compilers for the .NET platform and since I had never written a compiler before I decided to start with this simple language. This language is also used in the Program Analysis course that is taught at DTU by the authors of the book, and I thought it might be useful for students in the course to be able to try compiling their programs on a real platform, and see what difference their optimizations make in the final compiled executable. Another benefit is that the compiled programs can be debugged using a free .NET graphical debugger. As for the name, compilers are often named with the rule "abbreviation of language name + c" so for C# we have csc.exe, for Visual Basic we have vbc.exe. Using that rule I came up with the (rather unfortunate) name wc.exe.

Download the compiler v1.0

License

The source is licensed under the GPL v2.0. If that is problematic for you for some reason, contact me and we can work something out.

Command line options

The compiler is invoked as "`wc.exe options filename`". The possible options are:

/? or /help            Print a help message

/out:<filename>        Specify the name of the compiled executable

/debug                 Include debug information in compiled file

/coursesyntax          Use the modified syntax used in the Program Analysis course at DTU.

/plugins:<p1>`[,p2...]`  Load the given plugins during compilation.

The input file can also be specified as "STDIN", in which case the compiler reads the source from the standard input stream. This can be useful for example if you have some kind of preprocessor that takes the source file, analyzes it and spits out an optimized version. Then you could run the whole thing as something like:

Course Syntax (the `/coursesyntax` switch)

By default the compiler compiles While exactly as it is presented in the Principles of Program Analysis book. It can compile the examples from the book without modifications. This means that variables are not declared explicitly, and that () are used to group together multiple statements in if-else and while blocks. However, when I took the Program Analysis course at DTU and implemented some static analysis for it, we were given a slighly modified specification of the While language. In this spec variables had to be declared before use, and could only be declared inside begin-end blocks, and instead of using () in if and while statements, they ended with fi and od respectively. When the compiler is run with the /coursesyntax switch it will use this syntax and give errors on undeclared variables and if without fi and while ... do without od.

Debugging the While language

To debug While applications you should first download the Microsoft .NET Framework SDK v2.0. If you have Visual Studio 2005 or 2008 you might already have it installed. When it has installed, you can search for the file dbgclr.exe that should be somewhere under a GuiDebug folder. Start the debugger, go to the Debug menu, and select Program to Debug. Then locate your compiled .exe file and press OK. Press F10 to start debugging. If your source file is in the same folder as your executable it will load automatically, otherwise the debugger will prompt you for its location. Note that for this to work you must have compiled your program with the /debug switch.

Plugin Architecture

The compiler supports plugins to alter the syntax tree before compilation. Plugin writers simply create classes that implement the While.ICompilerPlugin interface, put their .dll's in the plugins subfolder under the folder where the compiler is, and can then run their plugins with the --plugins switch of the compiler. Loaded plugins get passed a copy of the abstract syntax tree and are free to modify it any way that they see fit. Plugins can even add their own nodes to it, since each node in the tree knows how to compile itself. There is also a handy Visitor class that plugins can inherit from, where they can override methods for the types of nodes they want to process, so they do not have to write the tree traversing code themselves. A simple plugin to fold constant expressions comes with the compiler and can be seen at https://github.com/einaregilsson/While-Language/blob/master/plugins/FoldConstantExpressions.cs.

While Abstract Syntax

Below is a description of the While syntax, mostly taken from the book, with the addition that concrete operators are shown and bitwise operations have been added.


Syntactic categories

a = Arithmetic expressions
b = Boolean expressions
x,y = Variables
n = Numerals
p = Procedures
S = Statements
D = Declarations
P = Programs
opa = Aerithmetic operators (+ - / * %)
opb = Boolean operators (and or xor)
opr = Relational operators (== != `<` `>` `<=` `>=`)
opbt = Bitwise operators (`<<` `>>` & | ^)


Abstract syntax

a ::= x | n | a1 opa a2 | a1 opbt a2
b ::= true | false | not b | b1 opb 2 | a1 opr a2
S ::= x := a | skip | S1;S2 | if b then S1 else S2 | while b do S |
write a | write b | read x | call p(x, y)
D ::= proc p(val x, res y) is S end
P ::= S | begin D ; S end

When using the `/coursesyntax` these changes are made to the abstract syntax:

V = Variable Declaration
V ::= var x | V;V
S ::= ... | begin V;S end | if b then S1 else S2 fi | while b do S od

About

A .NET compiler for the While Language, a programming language used in Program Analysis courses

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages