While interpreter

517 views

Published on

How to quiclky implement a flexible OO interpreter for an imperative programming language.

Java code available, just contact me :)

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
517
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

While interpreter

  1. 1. WHILE interpreter How to quiclky implement a flexible OO interpreterfor an imperative programming language (with code in Java) Andrea Valente aaue.dk/~av
  2. 2. Outline1. The WHILE programming language: • syntax and semantics2. Demo and structure of the interpreter • classes and attributes • evaluation and step-by-step execution3. 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. 3. The WHILE languageIt 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)SyntaxStmt -> id := Expr | Stmt1;Stmt2 | while Cond StmtExpr -> id | number | Expr1 + Expr2 | …C -> Expr1 < Expr2 | …
  4. 4. Semantics – an exampleExample: a:=5; b:=0; while b<a b:=b+1Execution 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+1The first step is to evaluate the assignment a:=5, and add a to the store: { (a,5) } , b:=0; while b<a b:=b+1Then we evaluate the assignment b:=0 { (a,5) , (b,0) } , while b<a b:=b+1
  5. 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 whileIn 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+1Now 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+1and we have to re-evaluate the while.Go on until we reach this configuration: { (a,5) , (b,5) } , while b<a b:=b+1where 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. 6. Outline1. The WHILE programming language: • syntax and semantics2. Demo and structure of the interpreter • classes and attributes • evaluation and step-by-step execution3. 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
  7. 7. OO implementation● Demo
  8. 8. Execution● value eval(state)● ASTNode step(state) – reshapes the ASTree
  9. 9. Outline1. The WHILE programming language: • syntax and semantics2. Demo and structure of the interpreter • classes and attributes • evaluation and step-by-step execution3. 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. 10. Map back from classes to grammarRules:class B extends A ---> A ::= Bclass A{ ... i:I, j:J } ---> A ::= something I somethingelse J sometingmore // look at toString and constructor to find syntax!class A{ ... x:int} ---> A ::= NUMBERExternal info: which symbol is the start symbol
  11. 11. Reconstructed grammarSTMT ::= SLIST | ASSIGN | WHILE | OUTPUTCOND ::= GREATEREXPR ::= ID | NUMBER | ADD | SUBSLIST ::= list<STMT>ASSIGN ::= ID = EXPRID ::= _String_...
  12. 12. Outline1. The WHILE programming language: • syntax and semantics2. Demo and structure of the interpreter • classes and attributes • evaluation and step-by-step execution3. 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. 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

×