SlideShare una empresa de Scribd logo
1 de 13
Descargar para leer sin conexión
WHILE interpreter


   How to quiclky implement a flexible
             OO interpreter
for an imperative programming language
           (with code in Java)

               Andrea Valente
                aaue.dk/~av
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
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 | …
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
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.
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
OO implementation
●   Demo
Execution
●   value eval(state)
●   ASTNode step(state)
    –   reshapes the ASTree
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
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
Reconstructed grammar

STMT ::= SLIST | ASSIGN | WHILE | OUTPUT

COND ::= GREATER

EXPR ::= ID | NUMBER | ADD | SUB

SLIST ::= list<STMT>

ASSIGN ::= ID = EXPR

ID ::= _String_

...
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
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

Más contenido relacionado

La actualidad más candente

Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 

La actualidad más candente (20)

Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generation
 
Compiler Design Unit 1
Compiler Design Unit 1Compiler Design Unit 1
Compiler Design Unit 1
 
C language
C languageC language
C language
 
Code generation
Code generationCode generation
Code generation
 
Lesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th stepLesson 4.2 5th and 6th step
Lesson 4.2 5th and 6th step
 
Managing I/O operations In C- Language
Managing I/O operations In C- LanguageManaging I/O operations In C- Language
Managing I/O operations In C- Language
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Lesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving processLesson 4.1 completing the problem solving process
Lesson 4.1 completing the problem solving process
 
7. 8085 instruction set iv
7. 8085 instruction set iv7. 8085 instruction set iv
7. 8085 instruction set iv
 
Issues in the design of Code Generator
Issues in the design of Code GeneratorIssues in the design of Code Generator
Issues in the design of Code Generator
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Compiler Design Unit 4
Compiler Design Unit 4Compiler Design Unit 4
Compiler Design Unit 4
 
Lesson 1 introduction to programming
Lesson 1 introduction to programmingLesson 1 introduction to programming
Lesson 1 introduction to programming
 
Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
The security professional's guide to programming - Eric Vanderburg
The security professional's guide to programming - Eric VanderburgThe security professional's guide to programming - Eric Vanderburg
The security professional's guide to programming - Eric Vanderburg
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 

Destacado

Destacado (8)

Paper turingmachine exercises
Paper turingmachine exercisesPaper turingmachine exercises
Paper turingmachine exercises
 
Digitel 2012 presentation
Digitel 2012 presentationDigitel 2012 presentation
Digitel 2012 presentation
 
MusiCards 2008
MusiCards 2008MusiCards 2008
MusiCards 2008
 
Social exploration of 1D games
Social exploration of 1D gamesSocial exploration of 1D games
Social exploration of 1D games
 
Paper turingmachine examples
Paper turingmachine examplesPaper turingmachine examples
Paper turingmachine examples
 
Pedagogical patterns
Pedagogical patternsPedagogical patterns
Pedagogical patterns
 
The prime slaughter game
The prime slaughter gameThe prime slaughter game
The prime slaughter game
 
Design games to learn (presented at ECGBL 2014)
Design games to learn (presented at ECGBL 2014)Design games to learn (presented at ECGBL 2014)
Design games to learn (presented at ECGBL 2014)
 

Similar a While interpreter

Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
Mohammad Shaker
 

Similar a While interpreter (20)

System Programming Overview
System Programming OverviewSystem Programming Overview
System Programming Overview
 
Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++
 
c-programming
c-programmingc-programming
c-programming
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNext
 
DSA
DSADSA
DSA
 
UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operations
 
The Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhoneThe Scheme Language -- Using it on the iPhone
The Scheme Language -- Using it on the iPhone
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 function
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
Polish
PolishPolish
Polish
 
MATLAB & Image Processing
MATLAB & Image ProcessingMATLAB & Image Processing
MATLAB & Image Processing
 
c
cc
c
 
Memory Management with Java and C++
Memory Management with Java and C++Memory Management with Java and C++
Memory Management with Java and C++
 
IN4308 1
IN4308 1IN4308 1
IN4308 1
 
Unit 1.pptx
Unit 1.pptxUnit 1.pptx
Unit 1.pptx
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Matlab lec1
Matlab lec1Matlab lec1
Matlab lec1
 
Lecture1
Lecture1Lecture1
Lecture1
 
Loop Statements TanCollege Programming course.pptx
Loop Statements TanCollege Programming course.pptxLoop Statements TanCollege Programming course.pptx
Loop Statements TanCollege Programming course.pptx
 

Último

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Último (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 

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