SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
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

Contenu connexe

Tendances

Lecture 16 17 code-generation
Lecture 16 17 code-generationLecture 16 17 code-generation
Lecture 16 17 code-generationIffat Anjum
 
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- LanguageRavindraSalunke3
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
7. 8085 instruction set iv
7. 8085 instruction set iv7. 8085 instruction set iv
7. 8085 instruction set ivsandip das
 
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 GeneratorDarshan sai Reddy
 
Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Assembly language programming_fundamentals 8086
Assembly language programming_fundamentals 8086Shehrevar Davierwala
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computerKamal Acharya
 
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 VanderburgEric Vanderburg
 

Tendances (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
 

En vedette

Paper turingmachine exercises
Paper turingmachine exercisesPaper turingmachine exercises
Paper turingmachine exercisesAndrea Valente
 
Digitel 2012 presentation
Digitel 2012 presentationDigitel 2012 presentation
Digitel 2012 presentationAndrea Valente
 
Social exploration of 1D games
Social exploration of 1D gamesSocial exploration of 1D games
Social exploration of 1D gamesAndrea Valente
 
Paper turingmachine examples
Paper turingmachine examplesPaper turingmachine examples
Paper turingmachine examplesAndrea Valente
 
The prime slaughter game
The prime slaughter gameThe prime slaughter game
The prime slaughter gameAndrea Valente
 
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)Andrea Valente
 

En vedette (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)
 

Similaire à While interpreter

Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Esoft Metro Campus - Programming with C++
Esoft Metro Campus - Programming with C++Rasan Samarasinghe
 
Functional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextFunctional Programming in JavaScript & ESNext
Functional Programming in JavaScript & ESNextUnfold UI
 
Lecture 01 variables scripts and operations
Lecture 01   variables scripts and operationsLecture 01   variables scripts and operations
Lecture 01 variables scripts and operationsSmee Kaem Chann
 
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 iPhoneJames Long
 
Rdbms chapter 1 function
Rdbms chapter 1 functionRdbms chapter 1 function
Rdbms chapter 1 functiondipumaliy
 
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
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
Loop Statements TanCollege Programming course.pptx
Loop Statements TanCollege Programming course.pptxLoop Statements TanCollege Programming course.pptx
Loop Statements TanCollege Programming course.pptxadamjackson818417
 

Similaire à 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
 

Dernier

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesBernd Ruecker
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Karmanjay Verma
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxAna-Maria Mihalceanu
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...BookNet Canada
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsYoss Cohen
 

Dernier (20)

Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
QCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architecturesQCon London: Mastering long-running processes in modern architectures
QCon London: Mastering long-running processes in modern architectures
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#Microservices, Docker deploy and Microservices source code in C#
Microservices, Docker deploy and Microservices source code in C#
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
A Glance At The Java Performance Toolbox
A Glance At The Java Performance ToolboxA Glance At The Java Performance Toolbox
A Glance At The Java Performance Toolbox
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
Transcript: New from BookNet Canada for 2024: BNC SalesData and LibraryData -...
 
Infrared simulation and processing on Nvidia platforms
Infrared simulation and processing on Nvidia platformsInfrared simulation and processing on Nvidia platforms
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