Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
Malte Schmitz 92766f7957 Add readme před 9 roky
src Add documentation of Interpreter před 9 roky
.gitignore Switch to maven and Java 1.6 před 9 roky
LICENSE Initial commit před 9 roky
README.md Add readme před 9 roky
compile.sh Add shell scripts před 9 roky
mult.whl Add main method před 9 roky
pom.xml Move docco to compile phase před 9 roky
run.sh Add shell scripts před 9 roky

README.md

Parser and Interpreter for While Programs

Exemplary Java implementation of a recursive decent parser, an abstract syntax tree (AST) data structure and a structural recursive interpreter over this data structure.

The purpose of this code is to demonstrate the concepts of syntax and semantics. For this purpose a little and very artificial but nevertheless Turing-complete programming language was defined.

The example code in the file mult.whl multiplies two integers using addition:

a := 2;
b := 4;
r := 0;
while (a) {
    r := r + b ;
    a := a - 1
}

Literate Programming Documentation

The code is documented in a literate programming style using Atlassian Docco

Have a look a the Parser and the Interpreter where the formal syntax and semantics are defined, too.

Building

The maven project can be build with

mvn compile

which compiles the Java code and automatically generates the docco documentation in target/docco.

Without maven you can compile the Java code manually as follows:

mkdir -p target/classes
javac -d target/classes src/main/java/*.java src/main/java/**/*.java

Running

You can the example code in mult.whl with

java -cp target/classes Main mult.whl

The expected output is

{a=0, b=4, r=8}

Exercise

In order to understand how this little application works, I suggest trying to extend is. For example you could try to implement the ++ operator which increments a variable. This has to be done in three steps

  1. Add a class Increment in the program package with the attribute identifier of type Identifier storing the name of the variable that will be incremented.

  2. Update the Parser by extending the Stmt rule

Stmt = ... | Id "++"
  1. Update the Interpreter by extending the sem function either by rewriting the increment operator as an assignment
sem(x "++", v) = sem(x ":=" x "+" 1, v)

or by directly applying the incrementation

sem(x "++", v) = v.update(x, v(x) + 1)