Infrared simulation and processing on Nvidia platforms
While interpreter
1. WHILE interpreter
How to quiclky implement a flexible
OO interpreter
for an imperative programming language
(with code in Java)
Andrea Valente
aaue.dk/~av
2. Outline
1. The WHILE programming language:
• syntax and semantics
2. Demo and structure of the interpreter
• classes and attributes
• evaluation and step-by-step execution
3. The mapping
• map interpreter UML back to the grammar
• invert it -> map grammar (and info about steps) into UML!
4. What is missing? ... a parser
3. The WHILE language
It is possible to model a simple imperative programming language with only:
● Assignment - a := 3+2
● Sequence - statement1;statement2
● While loop – while a<b { statement }
(the if-then-else statement can be emulated using the while, so it is not fundamental)
Syntax
Stmt -> id := Expr | Stmt1;Stmt2 | while Cond Stmt
Expr -> id | number | Expr1 + Expr2 | …
C -> Expr1 < Expr2 | …
4. Semantics – an example
Example: a:=5; b:=0; while b<a b:=b+1
Execution is simulated using a set S, a store, that represent the memory.
Initial configuration: empty store, the whole program to execute
{} , a:=5; b:=0; while b<a b:=b+1
The first step is to evaluate the assignment a:=5, and add a to the store:
{ (a,5) } , b:=0; while b<a b:=b+1
Then we evaluate the assignment b:=0
{ (a,5) , (b,0) } , while b<a b:=b+1
5. To execute the while -> evaluate boolean condition using values in store:
● if it is false we stop,
● otherwise execute body, then try again to execute the whole while
In S={ (a,5) , (b,0) }, b<a is true, then we rewrite our program like this:
{ (a,5) , (b,0) } , b:=b+1; while b<a b:=b+1
Now evaluate assignment b:=b+1, and in current store b is 0, so b will became 1:
{ (a,5) , (b,1) } , while b<a b:=b+1
and we have to re-evaluate the while.
Go on until we reach this configuration:
{ (a,5) , (b,5) } , while b<a b:=b+1
where the value of b is 5.
At this point we stop, because the condition of the while is false in the current store, so:
{ (a,5) , (b,5) } , ε
The program terminates, and we can read the results in the store.
6. Outline
1. The WHILE programming language:
• syntax and semantics
2. Demo and structure of the interpreter
• classes and attributes
• evaluation and step-by-step execution
3. The mapping
• map interpreter UML back to the grammar
• invert it -> map grammar (and info about steps) into UML!
4. What is missing? ... a parser
8. Execution
● value eval(state)
● ASTNode step(state)
– reshapes the ASTree
9. Outline
1. The WHILE programming language:
• syntax and semantics
2. Demo and structure of the interpreter
• classes and attributes
• evaluation and step-by-step execution
3. The mapping
• map interpreter UML back to the grammar
• invert it -> map grammar (and info about steps) into UML!
4. What is missing? ... a parser
10. Map back
from classes to grammar
Rules:
class B extends A ---> A ::= B
class A{ ... i:I, j:J } ---> A ::= something I somethingelse J sometingmore
// look at toString and constructor to find syntax!
class A{ ... x:int} ---> A ::= NUMBER
External info: which symbol is the start symbol
11. Reconstructed grammar
STMT ::= SLIST | ASSIGN | WHILE | OUTPUT
COND ::= GREATER
EXPR ::= ID | NUMBER | ADD | SUB
SLIST ::= list<STMT>
ASSIGN ::= ID = EXPR
ID ::= _String_
...
12. Outline
1. The WHILE programming language:
• syntax and semantics
2. Demo and structure of the interpreter
• classes and attributes
• evaluation and step-by-step execution
3. The mapping
• map interpreter UML back to the grammar
• invert it -> map grammar (and info about steps) into UML!
4. What is missing? ... a parser
13. Parser
● The parser needs to be implemented separately
● Check out sableCC and tutorials
http://nat.truemesh.com/archives/000531.html
● Or build it manually
http://www.cs.luther.edu/~leekent/tutorials/ll1.html
● A nice animated demo of a recursive descent parser
http://ag-kastens.uni-paderborn.de/lehre/material/compiler/parsdem