• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
While interpreter
 

While interpreter

on

  • 490 views

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

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

Java code available, just contact me :)

Statistics

Views

Total Views
490
Views on SlideShare
490
Embed Views
0

Actions

Likes
0
Downloads
1
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    While interpreter While interpreter Presentation Transcript

    • WHILE interpreter How to quiclky implement a flexible OO interpreterfor an imperative programming language (with code in Java) Andrea Valente aaue.dk/~av
    • 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
    • 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 | …
    • 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
    • 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.
    • 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
    • OO implementation● Demo
    • Execution● value eval(state)● ASTNode step(state) – reshapes the ASTree
    • 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
    • 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
    • Reconstructed grammarSTMT ::= SLIST | ASSIGN | WHILE | OUTPUTCOND ::= GREATEREXPR ::= ID | NUMBER | ADD | SUBSLIST ::= list<STMT>ASSIGN ::= ID = EXPRID ::= _String_...
    • 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
    • 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